RSA 和 AES 是两种常见的加密算法,分别用于公钥加密和对称加密。下面是对这两种算法的详细介绍。
1. RSA(Rivest-Shamir-Adleman)
基础知识
-
类型:公钥加密算法
-
工作原理:RSA 是一种非对称加密算法,使用一对密钥(公钥和私钥)来加密和解密数据。
-
加密过程:
- 密钥生成:选择两个大质数
p
和q
,计算它们的乘积n
。n
是模数,用于加密和解密。计算n
的欧拉函数φ(n)
。 - 选择公钥指数
e
:e
是一个小于φ(n)
的整数,且与φ(n)
互质。通常选择值为 65537。 - 计算私钥指数
d
:d
是e
的模φ(n)
的乘法逆元素。即d
是满足d * e ≡ 1 (mod φ(n))
的整数。 - 公钥:由
n
和e
组成。 - 私钥:由
n
和d
组成。
加密数据时,使用公钥进行加密;解密数据时,使用私钥进行解密。
- 密钥生成:选择两个大质数
优点
- 安全性高:RSA 基于大数分解的困难性,具有很高的安全性。
- 密钥管理方便:只需要公开公钥,私钥保持秘密即可。
缺点
- 速度较慢:RSA 加密和解密过程较慢,通常用于加密较小的数据(如对称密钥),而不是大数据。
2. AES(Advanced Encryption Standard)
基础知识
- 类型:对称加密算法
- 工作原理:AES 使用相同的密钥进行加密和解密操作。它是一个块加密算法,将数据分成固定大小的块进行加密。
- 加密过程:
- 密钥长度:AES 支持三种密钥长度:128 位、192 位和 256 位。
- 加密模式:AES 有多种模式,如 ECB(电子密码本模式)、CBC(密码分组链接模式)、CFB(加密反馈模式)、OFB(输出反馈模式)和 GCM(Galois/Counter Mode)。
- 数据块大小:AES 加密和解密过程是基于 128 位的数据块。
优点
- 速度快:AES 是一种对称加密算法,通常比 RSA 更快。
- 安全性高:AES 被广泛认为是一种非常安全的算法,且抗量子计算机攻击的能力相对较强。
缺点
- 密钥管理复杂:对称加密算法要求加密和解密使用相同的密钥,密钥的分发和管理较为复杂。
RSA 和 AES 的结合使用
通常,RSA 和 AES 会结合使用,以充分利用它们各自的优点:
- AES 加密:使用 AES 加密数据,这样可以高效地处理大数据量。
- RSA 加密 AES 密钥:将 AES 密钥使用 RSA 公钥加密。这样可以安全地将 AES 密钥传输给接收方。
- 解密过程:
- RSA 解密:接收方使用 RSA 私钥解密 AES 密钥。
- AES 解密:使用解密后的 AES 密钥解密数据。
代码示例
以下是一个简单的 Java 代码示例,演示如何结合使用 RSA 和 AES。
RSA 密钥生成
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
public class RSAKeyPairGenerator {
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
public static void main(String[] args) throws Exception {
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
System.out.println("Public Key: " + Base64.getEncoder().encodeToString(publicKey.getEncoded()));
System.out.println("Private Key: " + Base64.getEncoder().encodeToString(privateKey.getEncoded()));
}
}
AES 加密和解密
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtils {
private static final String ALGORITHM = "AES";
// 生成 AES 密钥
public static SecretKey generateAESKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(128); // 192 或 256 位
return keyGenerator.generateKey();
}
// AES 加密
public static String encrypt(String data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
// AES 解密
public static String decrypt(String encryptedData, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decoded = Base64.getDecoder().decode(encryptedData);
byte[] decrypted = cipher.doFinal(decoded);
return new String(decrypted);
}
}
通过将这些技术结合使用,你可以在应用中实现高效且安全的数据加密传输。
详细使用方法可参考在 Spring Boot 中实现 RSA + AES 自动接口解密