当前位置:   article > 正文

Java 对称加密AES、DES的实现_aes java

aes java

1.AES、DES的介绍

1.1.AES介绍

AES(Advanced Encryption Standard,高级加密标准)的出现,是因为以前使用的DES算法密钥长度较短,已经不适应当今数据加密安全性的要求,因此2000年10月2日,美国政府宣布将比利时密码学家Joan Daemen和Vincent Rijmen提出的密码算法RIJNDAEL作为高级加密标准。2001年11月26日,美国政府正式颁布AES为美国国家标准(编号为FIST PUBS 197)。这是密码史上的又一个重要事件。目前,AES已经被一些国际标准化组织,如OSO、IETF、IEEE 802.11等采纳,作为标准。

1.2.DES介绍

早先,为了满足对计算机数据安全性越来越高的需求,美国国家标准局(NBS)于1973年征用了IBM公司提交的一种加密算法,并经过一段时间的试用和征求意见,于1977年1月5日颁布,作为数据加密标准(Data Encryption Standard,DES),其设计目的是用于加密保护静态存储和传输信道中的数据。DES算法为密码体制中的对称密码体制,又称为美国数据加密标准。

2.AES与DES的区别与联系

AES和DES都是对称的块密码,AES是一个更复杂的版本,旨在纠正DES的缺点。

  • 加密速度:AES加密速度较快,比DES加密速度更快。
  • 加密安全性:AES加密安全性更高,更难被暴力破解。
  • 密钥长度:AES支持128位、192位和256位密钥长度,而DES只支持64位密钥长度。
  • 数据长度:AES可以加密和解密任意长度的数据,而DES只能加密64位的数据块。
  • 实现方式:AES是面向密码学的(比较复杂的情况),而DES是基于密码学协议的(一般用于信息通讯加密,身份验证等比较简单的情况)。

总的来说,AES比DES更安全、更快速、支持更多的密钥长度和数据长度,并且更易于实现。因此,在实际应用中,更推荐使用AES加密算法。

3.JAVA实现代码

3.1.AES加密/解密实现

  1. package com.gnss.common.utils;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.Base64;
  6. /**
  7. * AES加密
  8. * @author Mr.Li
  9. * @date 2023-02-15
  10. */
  11. public class AesUtils {
  12. /**
  13. * 密钥长度必须为16位
  14. */
  15. private static final String PASSWORD = "0123456789123456";
  16. private static final String ALGORITHM = "AES";
  17. private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
  18. /**
  19. * AES加密字符串
  20. *
  21. * @param message 需要被加密的字符串
  22. * @return base64 密文
  23. */
  24. public static String encrypt(String message) throws Exception {
  25. if (message == null ){
  26. return null;
  27. }
  28. // AES专用密钥
  29. SecretKeySpec secretKey = new SecretKeySpec(PASSWORD.getBytes(), ALGORITHM);
  30. // 创建密码器
  31. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  32. byte[] byteContent = message.getBytes(StandardCharsets.UTF_8);
  33. // 初始化为加密模式的密码器
  34. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  35. // 加密后的byte数据
  36. byte[] bytes = cipher.doFinal(byteContent);
  37. // 转base64
  38. return Base64.getEncoder().encodeToString(bytes);
  39. }
  40. /**
  41. * AES加密字符串
  42. * @param message 待加密的字符串
  43. * @param key 密钥
  44. * @return base64 密文
  45. * @throws Exception
  46. */
  47. public static String encrypt(String message,String key)throws Exception {
  48. if (message == null ){
  49. return null;
  50. }
  51. // AES专用密钥
  52. SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
  53. // 创建密码器
  54. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  55. byte[] byteContent = message.getBytes(StandardCharsets.UTF_8);
  56. // 初始化为加密模式的密码器
  57. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  58. // 加密后的byte数据
  59. byte[] bytes = cipher.doFinal(byteContent);
  60. // 转base64
  61. return Base64.getEncoder().encodeToString(bytes);
  62. }
  63. /**
  64. * 解密AES加密过的字符串
  65. *
  66. * @param encryptMessage AES加密过过的内容
  67. * @return 明文
  68. */
  69. public static String decrypt(String encryptMessage)throws Exception {
  70. if (encryptMessage == null){
  71. return null;
  72. }
  73. byte[] decodeArray = Base64.getDecoder().decode(encryptMessage);
  74. // AES专用密钥
  75. SecretKeySpec secretKey = new SecretKeySpec(PASSWORD.getBytes(), ALGORITHM);
  76. // 创建密码器
  77. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  78. // 初始化为解密模式的密码器
  79. cipher.init(Cipher.DECRYPT_MODE, secretKey);
  80. // 明文数组
  81. byte[] bytes = cipher.doFinal(decodeArray);
  82. return new String(bytes, StandardCharsets.UTF_8);
  83. }
  84. /**
  85. * 解密AES加密过的字符串
  86. * @param encryptMessage 带解密的字符串
  87. * @param key 密钥
  88. * @return 解密后的字符串
  89. * @throws Exception
  90. */
  91. public static String decrypt(String encryptMessage,String key)throws Exception {
  92. if (encryptMessage == null){
  93. return null;
  94. }
  95. byte[] decodeArray = Base64.getDecoder().decode(encryptMessage);
  96. // AES专用密钥
  97. SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
  98. // 创建密码器
  99. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  100. // 初始化为解密模式的密码器
  101. cipher.init(Cipher.DECRYPT_MODE, secretKey);
  102. // 明文数组
  103. byte[] bytes = cipher.doFinal(decodeArray);
  104. return new String(bytes, StandardCharsets.UTF_8);
  105. }
  106. }

3.2.DES加密/解密实现

  1. package com.gnss.common.utils;
  2. import javax.crypto.*;
  3. import javax.crypto.spec.DESKeySpec;
  4. import java.security.spec.KeySpec;
  5. import lombok.extern.slf4j.Slf4j;
  6. /**
  7. * @author Mr.Li
  8. * @Description : 使用DES算法的加密、解密工具
  9. * @date 20230319
  10. */
  11. @Slf4j
  12. public class DesUtils {
  13. /**
  14. * 加密算是是des
  15. */
  16. private static final String ALGORITHM = "DES";
  17. /**
  18. * 转换格式
  19. */
  20. private static final String TRANSFORMATION = "DES/ECB/NoPadding";
  21. /**
  22. * 利用8个字节64位的key给src加密
  23. * @param src
  24. * @param key
  25. * @return
  26. */
  27. public static byte[] encrypt(byte[] src, byte[] key) {
  28. try {
  29. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  30. SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  31. KeySpec keySpec = new DESKeySpec(key);
  32. SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
  33. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  34. byte[] enMsgBytes = cipher.doFinal(src);
  35. return enMsgBytes;
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. return null;
  40. }
  41. /**
  42. * 利用8个字节64位的key给src解密
  43. * @param encryptBytes
  44. * @param key
  45. * @return
  46. */
  47. public static byte[] decrypt(byte[] encryptBytes, byte[] key) {
  48. try {
  49. Cipher deCipher = Cipher.getInstance(TRANSFORMATION);
  50. SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  51. KeySpec deKeySpec = new DESKeySpec(key);
  52. SecretKey deSecretKey = secretKeyFactory.generateSecret(deKeySpec);
  53. deCipher.init(Cipher.DECRYPT_MODE, deSecretKey);
  54. byte[] deMsgBytes = deCipher.doFinal(encryptBytes);
  55. return deMsgBytes;
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. return null;
  60. }
  61. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/717463
推荐阅读
相关标签
  

闽ICP备14008679号