1.AES、DES的介绍
1.1.AES介绍
AES(Advanced Encryption Standard,高级加密标准)的出现,是因为以前使用的DES算法密钥长度较短,已经不适应当今数据加密安全性的要求,因此2000年10月2日,美国政府宣布将比利时密码学家Joan Daemen和Vincent Rijmen提出的密码算法RIJNDAEL作为高级加密标准。2001年11月26日,美国政府正式颁布AES为美国国家标准(编号为FIST PUBS 197)。这是密码史上的又一个重要事件。目前,AES已经被一些国际标准化组织,如OSO、IETF、IEEE 802.11等采纳,作为标准。
1.2.DES介绍
早先,为了满足对计算机数据安全性越来越高的需求,美国国家标准局(NBS)于1973年征用了IBM公司提交的一种加密算法,并经过一段时间的试用和征求意见,于1977年1月5日颁布,作为数据加密标准(Data Encryption Standard,DES),其设计目的是用于加密保护静态存储和传输信道中的数据。DES算法为密码体制中的对称密码体制,又称为美国数据加密标准。
2.AES与DES的区别与联系
AES和DES都是对称的块密码,AES是一个更复杂的版本,旨在纠正DES的缺点。
- 加密速度:AES加密速度较快,比DES加密速度更快。
- 加密安全性:AES加密安全性更高,更难被暴力破解。
- 密钥长度:AES支持128位、192位和256位密钥长度,而DES只支持64位密钥长度。
- 数据长度:AES可以加密和解密任意长度的数据,而DES只能加密64位的数据块。
- 实现方式:AES是面向密码学的(比较复杂的情况),而DES是基于密码学协议的(一般用于信息通讯加密,身份验证等比较简单的情况)。
总的来说,AES比DES更安全、更快速、支持更多的密钥长度和数据长度,并且更易于实现。因此,在实际应用中,更推荐使用AES加密算法。
3.JAVA实现代码
3.1.AES加密/解密实现
package com.gnss.common.utils;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* AES加密
* @author Mr.Li
* @date 2023-02-15
*/
public class AesUtils {
/**
* 密钥长度必须为16位
*/
private static final String PASSWORD = "0123456789123456";
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
/**
* AES加密字符串
*
* @param message 需要被加密的字符串
* @return base64 密文
*/
public static String encrypt(String message) throws Exception {
if (message == null ){
return null;
}
// AES专用密钥
SecretKeySpec secretKey = new SecretKeySpec(PASSWORD.getBytes(), ALGORITHM);
// 创建密码器
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
byte[] byteContent = message.getBytes(StandardCharsets.UTF_8);
// 初始化为加密模式的密码器
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// 加密后的byte数据
byte[] bytes = cipher.doFinal(byteContent);
// 转base64
return Base64.getEncoder().encodeToString(bytes);
}
/**
* AES加密字符串
* @param message 待加密的字符串
* @param key 密钥
* @return base64 密文
* @throws Exception
*/
public static String encrypt(String message,String key)throws Exception {
if (message == null ){
return null;
}
// AES专用密钥
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
// 创建密码器
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
byte[] byteContent = message.getBytes(StandardCharsets.UTF_8);
// 初始化为加密模式的密码器
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// 加密后的byte数据
byte[] bytes = cipher.doFinal(byteContent);
// 转base64
return Base64.getEncoder().encodeToString(bytes);
}
/**
* 解密AES加密过的字符串
*
* @param encryptMessage AES加密过过的内容
* @return 明文
*/
public static String decrypt(String encryptMessage)throws Exception {
if (encryptMessage == null){
return null;
}
byte[] decodeArray = Base64.getDecoder().decode(encryptMessage);
// AES专用密钥
SecretKeySpec secretKey = new SecretKeySpec(PASSWORD.getBytes(), ALGORITHM);
// 创建密码器
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
// 初始化为解密模式的密码器
cipher.init(Cipher.DECRYPT_MODE, secretKey);
// 明文数组
byte[] bytes = cipher.doFinal(decodeArray);
return new String(bytes, StandardCharsets.UTF_8);
}
/**
* 解密AES加密过的字符串
* @param encryptMessage 带解密的字符串
* @param key 密钥
* @return 解密后的字符串
* @throws Exception
*/
public static String decrypt(String encryptMessage,String key)throws Exception {
if (encryptMessage == null){
return null;
}
byte[] decodeArray = Base64.getDecoder().decode(encryptMessage);
// AES专用密钥
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
// 创建密码器
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
// 初始化为解密模式的密码器
cipher.init(Cipher.DECRYPT_MODE, secretKey);
// 明文数组
byte[] bytes = cipher.doFinal(decodeArray);
return new String(bytes, StandardCharsets.UTF_8);
}
}
3.2.DES加密/解密实现
package com.gnss.common.utils;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.security.spec.KeySpec;
import lombok.extern.slf4j.Slf4j;
/**
* @author Mr.Li
* @Description : 使用DES算法的加密、解密工具
* @date 20230319
*/
@Slf4j
public class DesUtils {
/**
* 加密算是是des
*/
private static final String ALGORITHM = "DES";
/**
* 转换格式
*/
private static final String TRANSFORMATION = "DES/ECB/NoPadding";
/**
* 利用8个字节64位的key给src加密
* @param src
* @param key
* @return
*/
public static byte[] encrypt(byte[] src, byte[] key) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
KeySpec keySpec = new DESKeySpec(key);
SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] enMsgBytes = cipher.doFinal(src);
return enMsgBytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 利用8个字节64位的key给src解密
* @param encryptBytes
* @param key
* @return
*/
public static byte[] decrypt(byte[] encryptBytes, byte[] key) {
try {
Cipher deCipher = Cipher.getInstance(TRANSFORMATION);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
KeySpec deKeySpec = new DESKeySpec(key);
SecretKey deSecretKey = secretKeyFactory.generateSecret(deKeySpec);
deCipher.init(Cipher.DECRYPT_MODE, deSecretKey);
byte[] deMsgBytes = deCipher.doFinal(encryptBytes);
return deMsgBytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}