在Java中,对称加密算法是一类常用的加密技术,它们使用相同的密钥进行加密和解密操作。以下是AES、DES、3DES和RC4四种对称加密算法的介绍及Java示例。
1. AES(Advanced Encryption Standard)
简介:
AES,全称为高级加密标准,是美国联邦政府采用的一种区块加密标准。它采用分组加密的方式,每个数据块进行独立加密。AES提供了三种密钥长度:128位、192位和256位,密钥长度越高,安全性越强。AES算法被广泛应用于保护电子数据的机密性,如金融交易、网络通信等。
Java示例:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESExample {
public static String encrypt(String plainText, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] originalBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(originalBytes);
return new String(decryptedBytes);
}
// 示例用法
public static void main(String[] args) throws Exception {
String key = "1234567890123456"; // AES密钥长度应为16(128位)、24(192位)或32(256位)字节
String plainText = "Hello, World!";
String encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Encrypted: " + encryptedText);
System.out.println("Decrypted: " + decryptedText);
}
}
注意:示例中未指定加密模式(如ECB、CBC等)和填充方式(如PKCS5Padding),实际应用中应根据需要选择。
2. DES(Data Encryption Standard)
简介:
DES,全称为数据加密标准,是一种使用密钥加密的块算法。DES算法使用56位密钥(实际上有64位,但其中8位用于奇偶校验)对64位的数据块进行加密。然而,由于密钥长度较短,DES已经被认为不再安全,容易受到暴力破解攻击。
Java示例:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class DESExample {
public static byte[] encrypt(String encryptString, String encryptKey) throws Exception {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
DESKeySpec desKey = new DESKeySpec(encryptKey.getBytes("UTF-8"));
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(encryptKey.getBytes("UTF-8")); // 实际应用中应避免使用密钥作为IV
cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
return cipher.doFinal(encryptString.getBytes());
}
// 解密方法类似,但使用Cipher.DECRYPT_MODE
// 示例用法略
}
注意:DES已不再推荐使用,因其安全性较低。
3. 3DES(Triple Data Encryption Standard)
简介:
3DES是对DES算法的增强和扩展,通过对数据应用DES算法三次来增加安全性。3DES使用两个或三个不同的密钥对数据进行加密和解密。3DES提供了112位或168位的密钥长度,相比单个DES的56位密钥,安全性显著提高。
Java示例:
以下是一个使用Java进行3DES加密和解密的简单示例。请注意,为了简化示例,这里假设密钥长度足够且未进行错误处理。在实际应用中,应添加适当的错误处理和密钥管理策略。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;
public class ThreeDESExample {
// 加密方法
public static String encrypt(String plainText, String key) throws Exception {
// 假设key是足够长度的字符串,这里需要转换为24字节(192位,因为3DES每个密钥56位,但通常使用8字节表示,共24字节)
byte[] keyBytes = key.getBytes("UTF-8");
if (keyBytes.length < 24) {
throw new IllegalArgumentException("Key must be at least 24 bytes long for 3DES");
}
// 截取前24字节作为密钥(实际使用中可能需要更复杂的密钥管理)
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, 0, 24, "DESede");
// 创建Cipher实例,指定算法为"DESede/ECB/PKCS5Padding"(这里使用ECB模式作为示例,但CBC模式更常用)
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
// 初始化Cipher为加密模式
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// 执行加密操作
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
// 将加密后的字节数组转换为Base64编码的字符串
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// 解密方法
public static String decrypt(String encryptedText, String key) throws Exception {
// 密钥处理与加密方法相同
byte[] keyBytes = key.getBytes("UTF-8");
if (keyBytes.length < 24) {
throw new IllegalArgumentException("Key must be at least 24 bytes long for 3DES");
}
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, 0, 24, "DESede");
// 创建并初始化Cipher实例为解密模式
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
// 将Base64编码的字符串转换回字节数组
byte[] originalBytes = Base64.getDecoder().decode(encryptedText);
// 执行解密操作
byte[] decryptedBytes = cipher.doFinal(originalBytes);
// 返回解密后的字符串
return new String(decryptedBytes, "UTF-8");
}
// 示例用法
public static void main(String[] args) throws Exception {
String key = "0123456789abcdef01234567"; // 示例密钥,应为24字节长
String plainText = "Hello, 3DES!";
String encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Encrypted: " + encryptedText);
System.out.println("Decrypted: " + decryptedText);
}
}
注意:
- 在实际应用中,应避免使用ECB模式,因为它不提供块之间的链接,可能导致某些安全漏洞。CBC模式(Cipher Block Chaining)是更常用的选择,它要求一个初始化向量(IV),该向量应与密文一起存储或传输,并在解密时使用。
- 密钥管理是一个重要的安全考虑因素,应确保密钥的机密
4. RC4
在Java中,RC4 加密算法并不是一个内置在标准Java加密扩展(Java Cryptography Extension, JCE)中的算法。因此,如果需要使用RC4进行加密和解密,可能需要自己实现RC4算法,或者使用第三方库。
不过,值得注意的是,RC4算法已经不再被认为是安全的,并且在一些应用中已经被废弃或禁止使用。因此,在选择加密算法时,建议考虑更安全的替代方案,如AES。
然而,如果确实需要RC4加密的示例,下面是一个简单的RC4加密算法的Java实现示例。请注意,这个示例仅用于教育目的,不应用于生产环境中的安全敏感应用。
public class RC4 {
private int[] S = new int[256];
private int x, y;
public RC4(byte[] key) {
for (int i = 0; i < 256; i++) {
S[i] = i;
}
int j = 0;
for (int i = 0; i < 256; i++) {
j = (j + S[i] + key[i % key.length] & 0xff) % 256;
swap(S, i, j);
}
x = y = 0;
}
private void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public byte[] encrypt(byte[] plaintext) {
byte[] ciphertext = new byte[plaintext.length];
for (int i = 0; i < plaintext.length; i++) {
x = (x + 1) % 256;
y = (S[x] + y) % 256;
swap(S, x, y);
int K = S[(S[x] + S[y]) % 256];
ciphertext[i] = (byte) (plaintext[i] ^ K);
}
return ciphertext;
}
// 注意:通常解密过程与加密过程相同,因为RC4是对称的
public byte[] decrypt(byte[] ciphertext) {
return encrypt(ciphertext); // 因为RC4是对称的,所以解密和加密使用的是相同的方法
}
// 测试代码
public static void main(String[] args) {
String keyString = "secretkey";
byte[] key = keyString.getBytes();
String plaintext = "Hello, RC4!";
byte[] plaintextBytes = plaintext.getBytes();
RC4 rc4 = new RC4(key);
byte[] ciphertext = rc4.encrypt(plaintextBytes);
byte[] decryptedtext = rc4.decrypt(ciphertext);
System.out.println("Ciphertext: " + new String(ciphertext)); // 注意:这将打印出乱码,因为直接打印字节数组通常不是可读的
System.out.println("Decrypted text: " + new String(decryptedtext)); // 这应该与原始明文相同
}
}
注意:
- 上面的代码示例中的
encrypt和decrypt方法实际上是相同的,因为RC4是对称的加密算法。 - 直接打印字节数组(如
ciphertext)通常会显示乱码,因为加密后的数据不是以文本形式存储的。你应该以某种方式(如Base64编码)将其转换为可读的字符串,或者仅在需要时(如写入文件或通过网络发送)处理原始字节。 - 在生产环境中,请避免使用RC4,并考虑使用更安全的加密算法,如AES。
1458



被折叠的 条评论
为什么被折叠?



