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

對(duì)稱加密算法

系統(tǒng) 2094 0

原創(chuàng)作者: snowolf

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

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. * 可替換為以下任意一種算法,同時(shí)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)使用其他對(duì)稱加密算法時(shí),如AES、Blowfish等算法時(shí),用下述代碼替換上述三行代碼
  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ù)上一個(gè)類的實(shí)現(xiàn),我們通過(guò)MD5以及SHA對(duì)字符串加密生成密鑰,這是比較常見(jiàn)的密鑰生成方式。
再給出一個(gè)測(cè)試類:

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

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

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. **/

對(duì)稱加密算法


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 奇米影视小说 | 国产三级做爰高清视频a | 国产精品边做奶水狂喷小说 | 在线欧美一级毛片免费观看 | 操操碰 | 国产成+人+亚洲+欧美综合 | 免费久久久久 | 国产成人综合网 | 国产欧美专区在线观看 | 色爱区综合五月激情 | 伊人俺去久久涩五月综合 | 视频一区色眯眯视频在线 | 成人精品一区二区三区中文字幕 | 99视频国产热精品视频 | 自拍 欧美 在线 综合 另类 | 国产视频精品久久 | 中文字幕久久精品波多野结 | 久久8| 亚洲偷图色综合色就色 | 久久亚洲精品专区蓝色区 | 国产短视频精品区第一页 | 欧美韩国日本在线观看 | 狠狠色噜噜狠狠米奇777 | 日日lu| 国产精品久久久久999 | 国内在线播放 | 91久久精品 | 一级毛片免费不卡 | 日韩国产一区二区 | 亚洲精品区在线播放一区二区 | 久久www免费人成精品香蕉 | 久久亚洲国产伦理 | 五月天婷婷视频 | 97久久人人 | 日韩 欧美 | 在线观看国产一区二区三区 | 伊人婷 | 在线成人亚洲 | 国产在线精品香蕉综合网一区 | 伊人久久综合谁合综合久久 | 久久免费高清 |