【加密技术】对称加密算法(3):AES

论坛 期权论坛 脚本     
匿名技术用户   2020-12-22 07:25   74   0

一.AES:AES加密算法即密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院 (NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。该算法为比利时密码学家Joan DaemenVincent Rijmen所设计,结合两位作者的名字,以Rijndael之命名之,投稿高级加密标准的甄选流程。(Rijdael的发音近于 "Rhinedoll"。)


二.优缺点:AES在软件及硬件上都能快速地加解密,相对来说较易于实作,且只需要很少的存储器


三.原理:AES算法基于排列和置换运算。排列是对数据重新进行安排,置换是将一个数据单元替换为另一个。AES 使用几种不同的方法来执行排列和置换运算。AES是一个迭代的、对称密钥分组的密码,它可以使用128、192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据。与公共密钥密码使用密钥对不同,对称密钥密码使用相同的密钥加密和解密数据。通过分组密码返回的加密数据的位数与输入数据相同。迭代加密使用一个循环结构,在该循环中重复置换和替换输入数据。


四.实现方式(java):

注意!!!:如果是自定义的秘钥,必须指定秘钥的长度,DES,3DES,AES 都一样

  1. <span style="color:#333333;"> /**
  2. * AES 生成秘钥 ,系统生成默认的秘钥,自定义长度
  3. * @return
  4. * @throws Exception
  5. */
  6. public static byte [] JDK_AES_GetSecretKey() throws Exception{
  7. KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  8. keyGenerator.init(128); // new SecureRandom() 默认长度
  9. Key secretKey = keyGenerator.generateKey();
  10. return secretKey.getEncoded();
  11. }
  12. /**
  13. * AES 生成秘钥 ,自定义的秘钥,生成默认的秘钥长度
  14. * @param key
  15. * @return
  16. * @throws Exception
  17. */
  18. public static byte [] JDK_AES_GetSecretKey(String key) throws Exception{
  19. KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  20. keyGenerator.init(128,new SecureRandom(key.getBytes()));
  21. Key secretKey = keyGenerator.generateKey();
  22. return secretKey.getEncoded();
  23. }
  24. /**
  25. * 使用 AES 加密算法,对数据进行加密
  26. * @param src
  27. * @param encryptKey
  28. * @return
  29. * @throws Exception
  30. */
  31. public static byte [] JDK_AES_Encrypt(String src,byte [] encryptKey) throws Exception{
  32. // 秘钥的转换
  33. Key key = new SecretKeySpec(encryptKey, "AES");
  34. //数据加密
  35. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  36. cipher.init(Cipher.ENCRYPT_MODE, key);
  37. byte [] result = cipher.doFinal(src.getBytes());
  38. return result;
  39. }
  40. /**
  41. * 使用 AES 加密算法,对数据进行解密
  42. * @param encryptBytes
  43. * @param encryptKey
  44. * @return
  45. * @throws Exception
  46. */
  47. public static String JDK_AES_Decrypt(byte [] encryptBytes ,byte [] encryptKey) throws Exception{
  48. //key 的转换
  49. Key key = new SecretKeySpec(encryptKey, "AES");
  50. //解密
  51. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  52. cipher.init(Cipher.DECRYPT_MODE, key);
  53. String result = new String(cipher.doFinal(encryptBytes));
  54. return result;
  55. }
  56. /**
  57. * jdk AES 生成秘钥 ,系统生成默认的秘钥,自定义长度
  58. * @return
  59. * @throws Exception
  60. */
  61. public static byte [] BC_AES_GetSecretKey() throws Exception{
  62. Security.addProvider(new BouncyCastleProvider());
  63. KeyGenerator keyGenerator = KeyGenerator.getInstance("AES","BC");
  64. keyGenerator.init(128); // new SecureRandom() 默认长度
  65. Key secretKey = keyGenerator.generateKey();
  66. return secretKey.getEncoded();
  67. }
  68. /**
  69. * bc AES 生成秘钥 ,自定义的秘钥,生成默认的秘钥长度
  70. * @param key
  71. * @return
  72. * @throws Exception
  73. */
  74. public static byte [] BC_AES_GetSecretKey(String key) throws Exception{
  75. Security.addProvider(new BouncyCastleProvider());
  76. KeyGenerator keyGenerator = KeyGenerator.getInstance("AES","BC");
  77. keyGenerator.init(128,new SecureRandom(key.getBytes()));
  78. Key secretKey = keyGenerator.generateKey();
  79. return secretKey.getEncoded();
  80. }
  81. /**
  82. * bc 使用 AES 加密算法,对数据进行加密
  83. * @param src
  84. * @param encryptKey
  85. * @return
  86. * @throws Exception
  87. */
  88. public static byte [] BC_AES_Encrypt(String src,byte [] encryptKey) throws Exception{
  89. // 秘钥的转换
  90. Key key = new SecretKeySpec(encryptKey, "AES");
  91. //数据加密
  92. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  93. cipher.init(Cipher.ENCRYPT_MODE, key);
  94. byte [] result = cipher.doFinal(src.getBytes());
  95. return result;
  96. }
  97. /**
  98. * bc 使用 AES 加密算法,对数据进行解密
  99. * @param encryptBytes
  100. * @param encryptKey
  101. * @return
  102. * @throws Exception
  103. */
  104. public static String BC_AES_Decrypt(byte [] encryptBytes ,byte [] encryptKey) throws Exception{
  105. //key 的转换
  106. Key key = new SecretKeySpec(encryptKey, "AES");
  107. //解密
  108. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  109. cipher.init(Cipher.DECRYPT_MODE, key);
  110. String result = new String(cipher.doFinal(encryptBytes));
  111. return result;
  112. }</span>
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP