加密算法是用于保护数据安全的核心技术,通过对数据进行加密,确保只有授权方能够访问原始数据。加密算法主要分为以下几类:
1. 对称加密算法
对称加密算法使用相同的密钥进行加密和解密。优点是加解密速度快,适合处理大量数据;缺点是密钥分发和管理较为复杂。
(1)AES(Advanced Encryption Standard)
-
密钥长度:128 位、192 位、256 位。
-
特点:
-
目前最常用的对称加密算法,安全性高。
-
性能优异,适合加密大量数据。
-
被广泛应用于文件加密、网络通信(如 HTTPS)、数据库加密等场景。
-
-
Java 实现:
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 void main(String[] args) throws Exception { String plainText = "Hello, AES!"; SecretKey secretKey = generateKey(); // 生成密钥 String encryptedText = encrypt(plainText, secretKey); // 加密 String decryptedText = decrypt(encryptedText, secretKey); // 解密 System.out.println("Plain Text: " + plainText); System.out.println("Encrypted Text: " + encryptedText); System.out.println("Decrypted Text: " + decryptedText); } // 生成 AES 密钥 public static SecretKey generateKey() throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); // 128 位密钥 return keyGen.generateKey(); } // 加密 public static String encrypt(String plainText, SecretKey secretKey) throws Exception { 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, SecretKey secretKey) throws Exception { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); return new String(decryptedBytes); } }
(2)DES(Data Encryption Standard)
-
密钥长度:56 位(64 位中 8 位用于奇偶校验)。
-
特点:
-
较老的对称加密算法,安全性较低。
-
已被 AES 取代,但仍用于兼容旧系统。
-
-
Java 实现:
java
复制
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.util.Base64; public class DESExample { public static void main(String[] args) throws Exception { String plainText = "Hello, DES!"; SecretKey secretKey = generateKey(); // 生成密钥 String encryptedText = encrypt(plainText, secretKey); // 加密 String decryptedText = decrypt(encryptedText, secretKey); // 解密 System.out.println("Plain Text: " + plainText); System.out.println("Encrypted Text: " + encryptedText); System.out.println("Decrypted Text: " + decryptedText); } // 生成 DES 密钥 public static SecretKey generateKey() throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("DES"); keyGen.init(56); // 56 位密钥 return keyGen.generateKey(); } // 加密 public static String encrypt(String plainText, SecretKey secretKey) throws Exception { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } // 解密 public static String decrypt(String encryptedText, SecretKey secretKey) throws Exception { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); return new String(decryptedBytes); } }
2. 非对称加密算法
非对称加密算法使用一对密钥:公钥和私钥。公钥用于加密,私钥用于解密。优点是安全性高,适合密钥分发;缺点是加解密速度较慢,适合加密少量数据。
(1)RSA
-
密钥长度:1024 位、2048 位、4096 位(推荐使用 2048 位以上)。
-
特点:
-
最常用的非对称加密算法。
-
适合加密小数据(如加密对称密钥)或数字签名。
-
-
Java 实现:
java
复制
import javax.crypto.Cipher; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.util.Base64; public class RSAExample { public static void main(String[] args) throws Exception { String plainText = "Hello, RSA!"; KeyPair keyPair = generateKeyPair(); // 生成密钥对 String encryptedText = encrypt(plainText, keyPair.getPublic()); // 加密 String decryptedText = decrypt(encryptedText, keyPair.getPrivate()); // 解密 System.out.println("Plain Text: " + plainText); System.out.println("Encrypted Text: " + encryptedText); System.out.println("Decrypted Text: " + decryptedText); } // 生成 RSA 密钥对 public static KeyPair generateKeyPair() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); // 2048 位密钥 return keyGen.generateKeyPair(); } // 加密 public static String encrypt(String plainText, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } // 解密 public static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); return new String(decryptedBytes); } }
(2)ECC(Elliptic Curve Cryptography)
-
密钥长度:256 位、384 位、521 位。
-
特点:
-
安全性高,密钥长度短,性能优于 RSA。
-
适合资源受限的环境(如移动设备、物联网设备)。
-
-
适用场景:数字签名、密钥交换。
3. 哈希算法
哈希算法将任意长度的数据映射为固定长度的哈希值。哈希值不可逆,常用于数据完整性校验和密码存储。
(1)SHA-256
-
输出长度:256 位。
-
特点:
-
安全性高,广泛用于数字签名和数据完整性校验。
-
是 SHA-2 系列算法的一部分。
-
-
Java 实现:
java
复制
import java.security.MessageDigest; import java.util.Base64; public class SHA256Example { public static void main(String[] args) throws Exception { String plainText = "Hello, SHA-256!"; String hashValue = hash(plainText); System.out.println("Hash Value: " + hashValue); } // 计算 SHA-256 哈希值 public static String hash(String plainText) throws Exception { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hashBytes = digest.digest(plainText.getBytes()); return Base64.getEncoder().encodeToString(hashBytes); } }
(2)MD5
-
输出长度:128 位。
-
特点:
-
计算速度快,但安全性较低,容易发生碰撞。
-
不推荐用于安全敏感的场景。
-
-
适用场景:数据校验(如文件完整性校验)。
4. 混合加密
在实际应用中,通常结合对称加密和非对称加密的优点:
-
使用非对称加密(如 RSA)加密对称密钥。
-
使用对称加密(如 AES)加密实际数据。
5. 选择加密算法的建议
-
对称加密:适合加密大量数据(如文件、数据库)。
-
推荐算法:AES。
-
-
非对称加密:适合加密小数据(如密钥)或数字签名。
-
推荐算法:RSA、ECC。
-
-
哈希算法:适合数据完整性校验或密码存储。
-
推荐算法:SHA-256。
-
通过选择合适的加密算法,可以满足不同的安全需求。在实际开发中,建议使用经过广泛验证的加密库(如 Java 的 javax.crypto
或 BouncyCastle
),并遵循最佳实践以确保安全性。