欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

對稱加密算法

系統(tǒng) 1918 0

原創(chuàng)作者: snowolf

DES
DES-Data Encryption Standard,即數(shù)據(jù)加密算法。是IBM公司于1975年研究成功并公開發(fā)表的。DES算法的入口參數(shù)有三個:Key、Data、Mode。其中Key為8個字節(jié)共64位,是DES算法的工作密鑰;Data也為8個字節(jié)64位,是要被加密或被解密的數(shù)據(jù);Mode為DES的工作方式,有兩種:加密或解密。
DES算法把64位的明文輸入塊變?yōu)?4位的密文輸出塊,它所使用的密鑰也是64位。
對稱加密算法
通過java代碼實現(xiàn)如下: Coder類見 單向加密算法

Java代碼

  1. import java.security.Key;
  2. import java.security.SecureRandom;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.KeyGenerator;
  5. import javax.crypto.SecretKey;
  6. import javax.crypto.SecretKeyFactory;
  7. import javax.crypto.spec.DESKeySpec;
  8. /**
  9. * DES安全編碼組件
  10. *
  11. *
        
  12. * 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)
  13. * DES key size must be equal to 56
  14. * DESede(TripleDES) key size must be equal to 112 or 168
  15. * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
  16. * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
  17. * RC2 key size must be between 40 and 1024 bits
  18. * RC4(ARCFOUR) key size must be between 40 and 1024 bits
  19. * 具體內(nèi)容 需要關(guān)注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html
  20. *
  21. *
  22. * @author 梁棟
  23. * @version 1.0
  24. * @since 1.0
  25. */
  26. public abstract class DESCoder extends Coder {
  27. /**
  28. * ALGORITHM 算法
  29. * 可替換為以下任意一種算法,同時key值的size相應(yīng)改變。
  30. *
  31. *
        
  32. * DES key size must be equal to 56
  33. * DESede(TripleDES) key size must be equal to 112 or 168
  34. * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
  35. * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
  36. * RC2 key size must be between 40 and 1024 bits
  37. * RC4(ARCFOUR) key size must be between 40 and 1024 bits
  38. *
  39. *
  40. * 在Key toKey(byte[] key)方法中使用下述代碼
  41. * SecretKey secretKey = new SecretKeySpec(key, ALGORITHM); 替換
  42. *
  43. * DESKeySpec dks = new DESKeySpec(key);
  44. * SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  45. * SecretKey secretKey = keyFactory.generateSecret(dks);
  46. *
  47. */
  48. public static final String ALGORITHM = "DES";
  49. /**
  50. * 轉(zhuǎn)換密鑰
  51. *
  52. * @param key
  53. * @return
  54. * @throws Exception
  55. */
  56. private static Key toKey(byte[] key) throws Exception {
  57. DESKeySpec dks = new DESKeySpec(key);
  58. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  59. SecretKey secretKey = keyFactory.generateSecret(dks);
  60. // 當(dāng)使用其他對稱加密算法時,如AES、Blowfish等算法時,用下述代碼替換上述三行代碼
  61. // SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
  62. return secretKey;
  63. }
  64. /**
  65. * 解密
  66. *
  67. * @param data
  68. * @param key
  69. * @return
  70. * @throws Exception
  71. */
  72. public static byte[] decrypt(byte[] data, String key) throws Exception {
  73. Key k = toKey(decryptBASE64(key));
  74. Cipher cipher = Cipher.getInstance(ALGORITHM);
  75. cipher.init(Cipher.DECRYPT_MODE, k);
  76. return cipher.doFinal(data);
  77. }
  78. /**
  79. * 加密
  80. *
  81. * @param data
  82. * @param key
  83. * @return
  84. * @throws Exception
  85. */
  86. public static byte[] encrypt(byte[] data, String key) throws Exception {
  87. Key k = toKey(decryptBASE64(key));
  88. Cipher cipher = Cipher.getInstance(ALGORITHM);
  89. cipher.init(Cipher.ENCRYPT_MODE, k);
  90. return cipher.doFinal(data);
  91. }
  92. /**
  93. * 生成密鑰
  94. *
  95. * @return
  96. * @throws Exception
  97. */
  98. public static String initKey() throws Exception {
  99. return initKey(null);
  100. }
  101. /**
  102. * 生成密鑰
  103. *
  104. * @param seed
  105. * @return
  106. * @throws Exception
  107. */
  108. public static String initKey(String seed) throws Exception {
  109. SecureRandom secureRandom = null;
  110. if (seed != null) {
  111. secureRandom = new SecureRandom(decryptBASE64(seed));
  112. } else {
  113. secureRandom = new SecureRandom();
  114. }
  115. KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
  116. kg.init(secureRandom);
  117. SecretKey secretKey = kg.generateKey();
  118. return encryptBASE64(secretKey.getEncoded());
  119. }
  120. }

延續(xù)上一個類的實現(xiàn),我們通過MD5以及SHA對字符串加密生成密鑰,這是比較常見的密鑰生成方式。
再給出一個測試類:

Java代碼

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. /**
  4. *
  5. * @author 梁棟
  6. * @version 1.0
  7. * @since 1.0
  8. */
  9. public class DESCoderTest {
  10. @Test
  11. public void test() throws Exception {
  12. String inputStr = "DES";
  13. String key = DESCoder.initKey();
  14. System.err.println("原文:\t" + inputStr);
  15. System.err.println("密鑰:\t" + key);
  16. byte[] inputData = inputStr.getBytes();
  17. inputData = DESCoder.encrypt(inputData, key);
  18. System.err.println("加密后:\t" + DESCoder.encryptBASE64(inputData));
  19. byte[] outputData = DESCoder.decrypt(inputData, key);
  20. String outputStr = new String(outputData);
  21. System.err.println("解密后:\t" + outputStr);
  22. assertEquals(inputStr, outputStr);
  23. }
  24. }

得到的輸出內(nèi)容如下:

Console代碼

  1. 原文: DES
  2. 密鑰: f3wEtRrV6q0=
  3. 加密后: C6qe9oNIzRY=
  4. 解密后: DES

由控制臺得到的輸出,我們能夠比對加密、解密后結(jié)果一致。這是一種簡單的加密解密方式,只有一個密鑰。
其實DES有很多同胞兄弟,如DESede(TripleDES)、AES、Blowfish、RC2、RC4(ARCFOUR)。這里就不過多闡述了,大同小異,只要換掉ALGORITHM換成對應(yīng)的值,同時做一個代碼替換 SecretKey secretKey = new SecretKeySpec(key, ALGORITHM); 就可以了,此外就是密鑰長度不同了。

Java代碼

  1. /**
  2. * DES key size must be equal to 56
  3. * DESede(TripleDES) key size must be equal to 112 or 168
  4. * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
  5. * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
  6. * RC2 key size must be between 40 and 1024 bits
  7. * RC4(ARCFOUR) key size must be between 40 and 1024 bits
  8. **/

對稱加密算法


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品国产乱码久久久久久久 | 色播视频在线观看 | 成人av在线网 | 九九热线有精品视频99 | 春色www视频在线观看 | 99精品国产免费久久国语 | 日韩在线观看 | 欧美高清极品videossex | 91国在线啪| 在线三级网址 | 中文字幕网在线 | 99中文在线 | 国产在线看片 | 五月激情婷婷六月 | 一区二区久久 | 一区二区三区日韩在线观看 | 久久综合狠狠综合久久 | 天天色色色 | 三上悠亚2022最新番号 | 国产精品一码二码三码在线 | 亚洲一区二区三区免费在线观看 | www国产视频 | 日韩欧美二区在线观看 | 久久国产精品一区二区 | 国产精品2020观看久久 | 亚洲综合一二三区 | 日韩欧美三区 | 亚洲美女天堂网 | 国产精品久久久久久久四虎电影 | 中文一区二区 | 5c5c5c精品视频在线观看 | 最新日韩精品在线观看 | 亚洲 日本 欧美 中文幕 | 中文二区 | 久久久一区二区 | 久久亚洲视频 | 狠狠色噜噜狠狠狠狠2018 | 中文线码中文高清播放中 | 国内真实迷j下药在线观看 人人艹逼 | 91免费在线| 久久精精 |