请关注微信公众号:拾荒的小海螺
博客地址:http://lsk-ww.cn/
1、简述
对称加密是一种加密算法,其中加密和解密使用相同的密钥。其主要特点是速度快、效率高,适用于大数据量的加密需求。对称加密算法通常用于保护数据的机密性和完整性,广泛应用于网络通信、数据存储等领域。
2、工作原理
对称加密的核心在于加密密钥和解密密钥相同。加密过程将明文通过加密算法和密钥转换为密文,解密过程则是将密文通过相同的密钥和算法转换回明文。其安全性依赖于密钥的保密性,如果密钥泄露,攻击者可以轻松解密加密数据。
3、加密算法
Java提供了丰富的库来实现对称加密算法,常见的加密方式有DES、AES、3DES、Blowfish和RC4等对称加密算法,以下列举了对称加密算法的实例。
3.1 DES(Data Encryption Standard)
DES 是一种早期的对称加密算法,使用56位密钥。虽然曾经被广泛使用,但由于密钥长度较短,安全性已经不足,容易被暴力破解。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class DESEncryption {
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56); // DES 密钥长度为 56 位
return keyGen.generateKey();
}
public static String encrypt(String plainText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) {
try {
SecretKey key = generateKey();
String plainText = "Hello, DES!";
String encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Original Text: " + plainText);
System.out.println("Encrypted Text: " + encryptedText);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.2 3DES(Triple DES)
为了增强DES的安全性,3DES对数据进行三次加密处理,密钥长度增加到112位或168位。尽管更安全,但处理速度较慢。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class TripleDESEncryption {
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
keyGen.init(168); // 3DES 密钥长度为 168 位
return keyGen.generateKey();
}
public static String encrypt(String plainText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) {
try {
SecretKey key = generateKey();
String plainText = "Hello, 3DES!";
String encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Original Text: " + plainText);
System.out.println("Encrypted Text: " + encryptedText);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.3 AES(Advanced Encryption Standard)
AES 是当前应用最广泛的对称加密算法,支持128位、192位和256位密钥长度。AES 具有较高的安全性和效率,已成为数据加密的标准。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESExample {
// 生成 AES 密钥
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256); // 使用256位密钥
return keyGen.generateKey();
}
// 加密
public static String encrypt(String plainText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// 解密
public static String decrypt(String encryptedText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) {
try {
SecretKey key = generateKey(); // 生成密钥
String plainText = "Hello, World!";
String encryptedText = encrypt(plainText, key); // 加密
String decryptedText = decrypt(encryptedText, key); // 解密
System.out.println("Original Text: " + plainText);
System.out.println("Encrypted Text: " + encryptedText);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.4 Blowfish
Blowfish 是一种快速的对称加密算法,支持32位到448位的可变密钥长度,适用于替代DES。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class BlowfishEncryption {
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("Blowfish");
keyGen.init(128); // Blowfish 密钥长度为 32 到 448 位,常用 128 位
return keyGen.generateKey();
}
public static String encrypt(String plainText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) {
try {
SecretKey key = generateKey();
String plainText = "Hello, Blowfish!";
String encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Original Text: " + plainText);
System.out.println("Encrypted Text: " + encryptedText);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.5 RC4
RC4 是一种流加密算法,具有简单和高效的特点,但由于其已被多次破解,安全性存疑。需要注意的是,RC4 已被认为不安全,不推荐用于实际应用。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class RC4Encryption {
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("RC4");
keyGen.init(128); // RC4 密钥长度为 40 到 2048 位,常用 128 位
return keyGen.generateKey();
}
public static String encrypt(String plainText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("RC4");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("RC4");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) {
try {
SecretKey key = generateKey();
String plainText = "Hello, RC4!";
String encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Original Text: " + plainText);
System.out.println("Encrypted Text: " + encryptedText);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、总结
对称加密算法在现代数据保护中扮演着重要角色。通过上述 对称加密解析,可以清晰地了解如何使用对称加密算法进行数据的加密和解密操作。请注意,在实际应用中,AES 是目前推荐的对称加密算法,因为它在安全性和性能方面都有较好的表现。其他算法,如DES和RC4,已经被认为不够安全,不推荐使用。