亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

對稱加密算法

系統(tǒng) 1836 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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久久青青 | 亚洲欧美日韩综合精品网 | 成人精品网 | 亚洲国产欧美国产综合一区 | 天天撸天天操 | 999热这里全都是精品 | 卡通动漫精选国产欧美 | 热久久这里只有精品 | 日韩在线视频一区二区三区 | 韩日一级毛片 | 国产精品美女视频 | 91视频青娱乐 | 日韩欧美亚洲国产高清在线 | 成人免费国产欧美日韩你懂的 | 欧美在线视频网站 | 日本成人不卡视频 | 91精品国产色综合久久 | 国产免费播放一区二区三区 | 久久国产精品亚洲 | 日韩特级毛片免费观看视频 | 国产成人精品永久免费视频 | 免费一级特黄视频 | 亚洲最大成人 | 最新久久精品 | 第一序列番外篇在哪里看 | 国产成人啪午夜精品网站 | 亚洲免费视频网 | 欧美日韩在线播一区二区三区 | 欧美色成人tv在线播放 | 色综合久久天天综合绕观看 | 日韩免费一级毛片 | 成年人国产视频 | a久久 | 免费国产福利 | 日韩中文在线观看 | 网色视频 | 欧美激情久久欧美激情 | 日本最新免费二区三区 | 天天干天天操天天做 | 妖精视频在线观看网站 | 天天操天天干天天射 |