一、對稱加密算法
????????對稱加密算法采用相同的密鑰來進行加密和解密操作。其優點是加密和解密速度快,不過密鑰的管理和分發存在一定的安全風險。
1.1、DES(已不推薦使用)
這是早期的對稱加密算法,密鑰長度為 56 位。但由于密鑰長度較短,如今已不太安全。
優點
- 歷史悠久,算法公開,研究較為透徹,有很多相關的實現和工具。
- 加密和解密速度相對較快,在早期計算機性能有限的情況下具有一定優勢。
缺點
- 密鑰長度較短,只有 56 位,在現代計算能力下,容易受到暴力破解攻擊。
- 安全性逐漸降低,目前已不推薦用于對安全性要求較高的場景。
加密
// DES 加密public static String desEncrypt(String plainText, String key) throws Exception {DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey secretKey = keyFactory.generateSecret(desKeySpec);Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}
解密
public static String desDecrypt(String encryptedText, String key) throws Exception {DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey secretKey = keyFactory.generateSecret(desKeySpec);Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}
結果
public static void main(String[] args) throws Exception {String plainText = "感謝關注,精華內容將持續更新!";String desKey = "66666666"; // DES 密鑰長度必須為 8 字節// DES 測試String desEncrypted = desEncrypt(plainText, desKey);String desDecrypted = desDecrypt(desEncrypted, desKey);System.out.println("DES 加密: " + desEncrypted);System.out.println("DES 解密: " + desDecrypted);}DES 加密: 7LqxGfmOgresnxwwXzlGBWqFt8hXuqd6E4BC0mLxaBAohvYPdvaZXSO45z9XA5GH
DES 解密: 感謝關注,精華內容將持續更新!
-
-
1.2、AES
-
????????目前應用廣泛的對稱加密算法,支持 128 位、192 位和 256 位的密鑰長度,安全性較高,效率也不錯。
優點
1.安全性高,支持 128 位、192 位和 256 位的密鑰長度,能夠有效抵御各種已知的攻擊。
2.加密和解密速度快,性能優越,在硬件和軟件實現上都有很好的表現。
3.被廣泛應用于各種領域,成為了對稱加密算法的主流選擇。
缺點
1.對于一些對資源要求極高的小型設備,可能會有一定的性能開銷。
加密
// AES 加密public static String aesEncrypt(String plainText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}
解密
// AES 解密public static String aesDecrypt(String encryptedText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}
結果
public static void main(String[] args) throws Exception {String plainText = "感謝關注,精華內容將持續更新!";String aesKey = "1234567812345678"; // AES 密鑰長度可以為 16、24 或 32 字節// AES 測試String aesEncrypted = aesEncrypt(plainText, aesKey);String aesDecrypted = aesDecrypt(aesEncrypted, aesKey);System.out.println("AES 加密: " + aesEncrypted);System.out.println("AES 解密: " + aesDecrypted);}AES 加密: PUI7SM6+J4XvSDnioVneLtQDkBXchZlIF7k9v3fqe5Nwk8Polh+pBxR5RQmbHa7v
AES 解密: 感謝關注,精華內容將持續更新!
-
-
1.3、3DES
-
????????它是 DES 的增強版本,通過多次使用 DES 算法來增加密鑰長度,進而提高安全性。
優點
1.在 DES 的基礎上進行了改進,通過多次使用 DES 算法,增加了密鑰長度,提高了安全性。
2.兼容性好,由于是基于 DES 算法,在一些舊系統中可以繼續使用。
缺點
1.加密和解密速度較慢,因為需要進行多次 DES 運算。
2.密鑰長度相對較長,管理和存儲成本較高。
加密
// 3DES 加密public static String tripleDesEncrypt(String plainText, String key) throws Exception {DESedeKeySpec desEdeKeySpec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");SecretKey secretKey = keyFactory.generateSecret(desEdeKeySpec);Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}
解密
// 3DES 解密public static String tripleDesDecrypt(String encryptedText, String key) throws Exception {DESedeKeySpec desEdeKeySpec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");SecretKey secretKey = keyFactory.generateSecret(desEdeKeySpec);Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}
結果
public static void main(String[] args) throws Exception {String plainText = "感謝關注,精華內容將持續更新!";String tripleDesKey = "0123456789abcdef01234567"; // 3DES 密鑰長度必須為 24 字節// 3DES 測試String tripleDesEncrypted = tripleDesEncrypt(plainText, tripleDesKey);String tripleDesDecrypted = tripleDesDecrypt(tripleDesEncrypted, tripleDesKey);System.out.println("3DES 加密: " + tripleDesEncrypted);System.out.println("3DES 解密: " + tripleDesDecrypted);}
-
-
1.4、Blowfish
-
????????這是一種可變密鑰長度的對稱分組密碼算法,密鑰長度可以從 32 位到 448 位,具有較高的加密速度和安全性,適用于對速度要求較高的場景。
優點
1.密鑰長度可變,范圍從 32 位到 448 位,可以根據不同的安全需求進行調整。
2.加密和解密速度快,尤其是在 64 位塊的加密操作上表現出色。
3.算法簡單,易于實現,沒有專利限制。
缺點
1.塊大小固定為 64 位,在處理大數據時可能效率不如其他算法。
2.應用范圍相對較窄,不如 AES 等算法普及。
加密
// Blowfish 加密public static String blowfishEncrypt(String plainText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Blowfish");Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}
解密
// Blowfish 解密public static String blowfishDecrypt(String encryptedText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Blowfish");Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}
結果
public static void main(String[] args) throws Exception {String plainText = "感謝關注,精華內容將持續更新!";String blowfishKey = "abcdefghijklmnop";// Blowfish 測試String blowfishEncrypted = blowfishEncrypt(plainText, blowfishKey);String blowfishDecrypted = blowfishDecrypt(blowfishEncrypted, blowfishKey);System.out.println("Blowfish 加密: " + blowfishEncrypted);System.out.println("Blowfish 解密: " + blowfishDecrypted);}Blowfish 加密: HEKXerp2DpkPIHeIIt/cPBxub0z7jWWYKxZImxB2VOfDWIrH/dHVNqSP7gyHwyiU
Blowfish 解密: 感謝關注,精華內容將持續更新!
-
-
1.5、Twofish
-
????????作為 Blowfish 算法的繼任者,Twofish 同樣是對稱分組加密算法。它支持 128 位、192 位和 256 位的密鑰長度,設計上考慮了安全性和性能的平衡,并且具有良好的靈活性和可擴展性。
優點
1.安全性高,設計上考慮了各種攻擊方式,具有較好的抗攻擊能力。
2.支持多種密鑰長度,包括 128 位、192 位和 256 位。
3.算法靈活性高,可擴展性好。
缺點
1.實現相對復雜,需要一定的技術成本。
2.應用不如 AES 廣泛,相關的工具和庫相對較少。
引入依賴
<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.70</version>
</dependency>
加密
// Twofish 加密(需要 Bouncy Castle 庫)public static String twofishEncrypt(String plainText, String key) throws Exception {Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Twofish");Cipher cipher = Cipher.getInstance("Twofish/ECB/PKCS5Padding", "BC");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}
解密
// Twofish 解密(需要 Bouncy Castle 庫)public static String twofishDecrypt(String encryptedText, String key) throws Exception {Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Twofish");Cipher cipher = Cipher.getInstance("Twofish/ECB/PKCS5Padding", "BC");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}
結果
public static void main(String[] args) throws Exception {String plainText = "感謝關注,精華內容將持續更新!";String twofishKey = "0123456789abcdef0123456789abcdef";// Twofish 測試String twofishEncrypted = twofishEncrypt(plainText, twofishKey);String twofishDecrypted = twofishDecrypt(twofishEncrypted, twofishKey);System.out.println("Twofish 加密: " + twofishEncrypted);System.out.println("Twofish 解密: " + twofishDecrypted);}Twofish 加密: Z9UhJb9JZT0++Do6zauZPwf5tOMc6gX2o4LE7pZLXcwTL4PJ6m4LGvobb9k7Uv1e
Twofish 解密: 感謝關注,精華內容將持續更新!
通過以上內容便可輕輕松松使用是對稱加密算法.是不是超級簡單.有任何問題歡迎留言哦!!!
重點!重點!重點!
遇到問題不用怕不如來我的知識庫找找看,也許有意想不到的收獲!!!
易網時代-易庫資源-易庫教程:.NET開發、Java開發、PHP開發、SqlServer技術、MySQL技術-開發資料大全-易網時代-易庫資源-易庫教程 (escdns.com)