import javax.crypto.*; import javax.crypto.spec.DESKeySpec; import java.security.SecureRandom; import org.apache.commons.codec.binary.Base64; /** * @ClassName : DesEncryptUtil * @Author : Zhang_kuanlu * @Date: 2021/4/8 13:20 * @Description : 使用DES算法的加密、解密工具 */ public class DesEncryptUtil { //DES加密、解密时填入的密钥 private static final String PASS_KEY = "dareway_hdrgs"; /** * @Description: 加密 * @Param sourceStr 原始未加密明文 key 密钥,长度必须大于等于8位 * @return 加密后的字符串的base64格式 **/ public static String encryptDesToBase64(String sourceStr, String key) { try{ if(sourceStr == null || sourceStr.isEmpty()){ throw new Exception("原文不能为空!"); } if(key == null || key.isEmpty() || key.length() < 8){ throw new Exception("密钥不能为空,且密钥必须大于等于8位!"); } byte[] datasource = sourceStr.getBytes("utf-8"); SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(key.getBytes("utf-8")); //创建一个密匙工厂,然后用它把DESKeySpec转换成 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); //Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); //用密匙初始化Cipher对象,ENCRYPT_MODE用于将 Cipher 初始化为加密模式的常量 cipher.init(Cipher.ENCRYPT_MODE, securekey, random); //现在,获取数据并加密 正式执行加密操作 //按单部分操作加密或解密数据,或者结束一个多部分操作 return Base64.encodeBase64String(cipher.doFinal(datasource)); }catch(Throwable e) { System.out.println("DES 加密异常,详情:" + e.toString()); e.printStackTrace(); } return null; } /** * @Description: 解密 * @Param encodedStrBase64 加密过的密文base64形式(由加密方法返回的) key 密钥,长度必须大于等于8位 * @return 解密后的字符串 **/ public static String decrypt(String encodedStrBase64, String key) throws Exception { if(encodedStrBase64 == null || encodedStrBase64.isEmpty()){ throw new Exception("密文不能为空!"); } if(key == null || key.isEmpty() || key.length() < 8){ throw new Exception("密钥不能为空,且密钥必须大于等于8位!"); } byte[] src = Base64.decodeBase64(encodedStrBase64); // DES算法要求有一个可信任的随机数源 SecureRandom random = new SecureRandom(); // 创建一个DESKeySpec对象 DESKeySpec desKey = new DESKeySpec(key.getBytes("utf-8")); // 创建一个密匙工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//返回实现指定转换的 Cipher 对象 // 将DESKeySpec对象转换成SecretKey对象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, random); // 真正开始解密操作 return new String(cipher.doFinal(src)); } public static void main(String[] args) { String encodedBase64 = encryptDesToBase64("MR", PASS_KEY); String encodedBase642 = encryptDesToBase64("SL", PASS_KEY); String encodedBase643 = encryptDesToBase64("ALL", PASS_KEY); System.out.println("MR加密后的base64字符串:" + encodedBase64); System.out.println("SL加密后的base64字符串:" + encodedBase642); System.out.println("ALL加密后的base64字符串:" + encodedBase643); String decoded = null; String decoded2 = null; String decoded3 = null; try { decoded = decrypt(encodedBase64, PASS_KEY); decoded2 = decrypt(encodedBase642, PASS_KEY); decoded3 = decrypt(encodedBase643, PASS_KEY); } catch (Exception e) { e.printStackTrace(); } System.out.println("解密后:" + decoded); System.out.println("解密后:" + decoded2); System.out.println("解密后:" + decoded3); } }View Code
参考博客:
https://blog.csdn.net/tiancao222/article/details/107954370
标签:Java,String,DES,加解密,Cipher,key,new,加密 From: https://www.cnblogs.com/daytoy105/p/16654844.html