该文章包含3DES和DES加密解密示例
注意3DES密钥长度必须是8的倍数,而DES密钥长度必须是8字节
下面是使用3DES加密解密的过程示例
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* @author token AES算法
* @date 2021年1月26日
*/
public class Encryp3DES {
// 密钥(3DES加密和解密过程中,密钥长度都必须是8的倍数)
private final static String secretKey = "zdy12345";
// 加解密统一使用的编码方式
private final static String encoding = "utf-8";
public static void main(String[] args) throws Exception {
String input="巅峰小苏";
String en=encode(input);
System.out.println("加密:"+en);
String de=decode(en);
System.out.println("解密:"+de);
}
/**
* 3DES加密
* @param plainText 普通文本
* @return
* @throws Exception
*/
public static String encode(String plainText) throws Exception {
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(secretKey.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//DES加密和解密过程中,密钥长度都必须是8的倍数
SecretKey secretKey = keyFactory.generateSecret(dks);
// using DES in ECB mode
Cipher cipher = Cipher.getInstance("DES/ECB/pkcs5padding");
// 用密匙初始化Cipher对象
//cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// 执行加密操作
byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
return Base64.getEncoder().encodeToString(encryptData);
}
/**
* 3DES解密
* @param encryptText 加密文本
* @return
* @throws Exception
*/
public static String decode(String encryptText) throws Exception {
// DES算法要求有一个可信任的随机数源
//SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(secretKey.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(dks);
// using DES in ECB mode
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, secretKey);
// 正式执行解密操作
byte[] decryptData = cipher.doFinal(Base64.getDecoder().decode(encryptText));
return new String(decryptData, encoding);
}
}
下面是使用DES加密解密的过程示例
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class DES {
public static void main(String[] args)throws Exception {
String input="巅峰小苏";//明文
String key="zdy12345";//密钥
String transformation="DES";//算法
String algorithm="DES";//加密类型
String encode=encryptDES(input, key, transformation, algorithm);
System.out.println("加密:"+encode);
String ret=decryptDES(encode, key, transformation, algorithm);
System.out.println("解密:"+ret);
}
/**
* 加密
* @param input 原文
* @param key 密钥
* @param transformation 算法
* @param algorithm 加密类型
* @return
* @throws Exception
*/
private static String encryptDES(String input,String key,String transformation,String algorithm)throws Exception {
//创建加密对象
Cipher cipher=Cipher.getInstance(transformation);
//创建加密规则
SecretKeySpec secretKeySpec=new SecretKeySpec(key.getBytes(), algorithm);
//第一个参数是加密模式,解密模式
//第二个参数表示加密规则
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
//调用加密方法,参数表示原文的字节数组
byte[] bytes=cipher.doFinal(input.getBytes());
//字节数组转换成base64编码
String encode=Base64.encodeBase64String(bytes);
return encode;
}
/**
* 解密
* @param encryptDES 密文
* @param key
* @param transformation
* @param algorithm
* @return
* @throws Exception
*/
private static String decryptDES(String encryptDES,String key,String transformation,String algorithm)throws Exception {
Cipher cipher=Cipher.getInstance(transformation);
SecretKeySpec secretKeySpec=new SecretKeySpec(key.getBytes(),algorithm);
//Cipher.DECRYPT_MODE表示解密
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] bytes=cipher.doFinal(Base64.decodeBase64(encryptDES));
return new String(bytes);
}
}
欢迎大家阅读,本人见识有限,写的博客难免有错误或者疏忽的地方,还望各位大佬指点,在此表示感谢。如果觉得本文章有帮助到的,点个赞呗,让更多的阅读者看到