PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/zzsh-common/WEB/src/com/common/demo/DESCoder.java

https://gitlab.com/BGCX262/zzsh-svn-to-git
Java | 165 lines | 62 code | 26 blank | 77 comment | 3 complexity | a74a4e54102201d01337c29e970d499d MD5 | raw file
  1. package com.common.demo;
  2. import static org.junit.Assert.assertArrayEquals;
  3. import static org.junit.Assert.assertEquals;
  4. import java.math.BigInteger;
  5. import java.security.Key;
  6. import java.security.SecureRandom;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.KeyGenerator;
  9. import javax.crypto.SecretKey;
  10. import javax.crypto.SecretKeyFactory;
  11. import javax.crypto.spec.DESKeySpec;
  12. /**
  13. * DES安全编码组件
  14. *
  15. * <pre>
  16. * 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)
  17. * DES key size must be equal to 56
  18. * DESede(TripleDES) key size must be equal to 112 or 168
  19. * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
  20. * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
  21. * RC2 key size must be between 40 and 1024 bits
  22. * RC4(ARCFOUR) key size must be between 40 and 1024 bits
  23. * 具体内容 需要关注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html
  24. * </pre>
  25. *
  26. * @author 梁栋
  27. * @version 1.0
  28. * @since 1.0
  29. */
  30. public abstract class DESCoder extends Coder {
  31. /**
  32. * ALGORITHM 算法 <br>
  33. * 可替换为以下任意一种算法,同时key值的size相应改变。
  34. *
  35. * <pre>
  36. * DES key size must be equal to 56
  37. * DESede(TripleDES) key size must be equal to 112 or 168
  38. * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
  39. * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
  40. * RC2 key size must be between 40 and 1024 bits
  41. * RC4(ARCFOUR) key size must be between 40 and 1024 bits
  42. * </pre>
  43. *
  44. * 在Key toKey(byte[] key)方法中使用下述代码
  45. * <code>SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);</code> 替换
  46. * <code>
  47. * DESKeySpec dks = new DESKeySpec(key);
  48. * SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  49. * SecretKey secretKey = keyFactory.generateSecret(dks);
  50. * </code>
  51. */
  52. public static final String ALGORITHM = "DES";
  53. /**
  54. * 转换密钥<br>
  55. *
  56. * @param key
  57. * @return
  58. * @throws Exception
  59. */
  60. private static Key toKey(byte[] key) throws Exception {
  61. DESKeySpec dks = new DESKeySpec(key);
  62. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  63. SecretKey secretKey = keyFactory.generateSecret(dks);
  64. // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码
  65. // SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
  66. return secretKey;
  67. }
  68. /**
  69. * 解密
  70. *
  71. * @param data
  72. * @param key
  73. * @return
  74. * @throws Exception
  75. */
  76. public static byte[] decrypt(byte[] data, String key) throws Exception {
  77. Key k = toKey(decryptBASE64(key));
  78. Cipher cipher = Cipher.getInstance(ALGORITHM);
  79. cipher.init(Cipher.DECRYPT_MODE, k);
  80. return cipher.doFinal(data);
  81. }
  82. /**
  83. * 加密
  84. *
  85. * @param data
  86. * @param key
  87. * @return
  88. * @throws Exception
  89. */
  90. public static byte[] encrypt(byte[] data, String key) throws Exception {
  91. Key k = toKey(decryptBASE64(key));
  92. Cipher cipher = Cipher.getInstance(ALGORITHM);
  93. cipher.init(Cipher.ENCRYPT_MODE, k);
  94. return cipher.doFinal(data);
  95. }
  96. /**
  97. * 生成密钥
  98. *
  99. * @return
  100. * @throws Exception
  101. */
  102. public static String initKey() throws Exception {
  103. return initKey(null);
  104. }
  105. /**
  106. * 生成密钥
  107. *
  108. * @param seed
  109. * @return
  110. * @throws Exception
  111. */
  112. public static String initKey(String seed) throws Exception {
  113. SecureRandom secureRandom = null;
  114. seed = "hrpweb30";
  115. if (seed != null) {
  116. secureRandom = new SecureRandom(decryptBASE64(seed));
  117. } else {
  118. secureRandom = new SecureRandom();
  119. }
  120. KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
  121. kg.init(secureRandom);
  122. SecretKey secretKey = kg.generateKey();
  123. return encryptBASE64(secretKey.getEncoded());
  124. }
  125. public static void main(String[] arges) throws Exception{
  126. String inputStr = "<LEFTDAYS>-1</LEFTDAYS><ProductNo>CQOL5eDYhVvyfgZNqT6guuaTDcIEJEnO</ProductNo><UPGRADETYPE>2</UPGRADETYPE><LOCKNO>64168</LOCKNO><FIRSTDATE>2012-04-13</FIRSTDATE><CurDatabasePlat>1</CurDatabasePlat><VER>60</VER><VER_S>1</VER_S><GRP_BPS_BASIC>1</GRP_BPS_BASIC><BAMA>1</BAMA><RSYD>1</RSYD><HTGL>1</HTGL><SAMA>1</SAMA><BXFL>1</BXFL><BBGL>1</BBGL><BGGJ>1</BGGJ><CGZS>1</CGZS><GZZD>1</GZZD><FZJC>1</FZJC><JXGL>1</JXGL><GXGL>1</GXGL><GRP_BPS_BUSSINESS>1</GRP_BPS_BUSSINESS><REMA>1</REMA><PRMA>1</PRMA><WBOC>1</WBOC><CC>1</CC><MBO>1</MBO><TRMA>1</TRMA><ATMA>1</ATMA><RMGL>1</RMGL><DAGL>1</DAGL><BSGZ>0</BSGZ><ZCZM>1</ZCZM><GZRZ>1</GZRZ><YDHW>1</YDHW><DTGH>1</DTGH><COMP>1</COMP><ZXXX>1</ZXXX><ZXKS>1</ZXKS><GRP_ESS_EBASIC>1</GRP_ESS_EBASIC><GRP_ESS_MBASIC>1</GRP_ESS_MBASIC><SELFSEVICEFLAG>2</SELFSEVICEFLAG><GRP_ESS_EBASIC>1</GRP_ESS_EBASIC><EMSS>1</EMSS><GRP_ESS_MBASIC>1</GRP_ESS_MBASIC><MASS>1</MASS><TRSS>1</TRSS><PRSS>1</PRSS><RESS>1</RESS><ATSS>1</ATSS><EATSS>1</EATSS><ZPMH>1</ZPMH>";
  127. String key = DESCoder.initKey();
  128. System.err.println("原文:\t" + inputStr);
  129. System.err.println("密钥:\t" + key);
  130. byte[] inputData = inputStr.getBytes();
  131. inputData = DESCoder.encrypt(inputData, key);
  132. System.err.println("加密后:\t" + DESCoder.encryptBASE64(inputData));
  133. inputData = new String("4BEF4EE35DD47B8FD844F06EE8400D6A9F8D93A79F9D92E3100808071CF43A9DDB231326002A147AEF5CC740E13DD4085FD152B37FD05AFC42CB52E63DE34BA6DB22034F093CCC0F43D046CA6EB894E309310A2C1A3BE2046DD569D77DDC6EF071D051E250F027B334113D09221435B332E95AE062F64FB4C087A180B465D11260F449E242F02758A995A896BF8AD93F5CE46BE86DC560BC331414171C05244BA0B680B35EB59BCF74BD52C340C073EC7E9AAAAB90DA7CC20521F0150106054B80B857C84AED48ADC27DC970D771C22A5FD96AD65AFA2FBD21371A26221D0061F02C18241C0B39ABDD46EB3B0A2EE31E51E76BD770D247A4341E1C1F092E0D53AC87B783B29681F10059E767DE45F0010F23072AF45CF81965FC3DE04DF451BE37E359D563F14C48A09C91AD9CBD7387E44EFE400B5FD0215EF744F044F9225DABB482C173AE96D1203404360A33137783DA6FC17EAF8FF80B3AF34EFB48E62557F64CE652EB4DA732ED5EFF4B1F0074FE3A0A360A01394EBC85B392AE6CA4DB2A0E26F04CEA4B4FBB82BA65D879D5215BDB51ED5BE94440BA7BB08CBD9C81EA022616370456FF6B82A696A29E9587D52810110035EF2147A086B97DA780D32362C166C651F6254B9AB478BB78A490E60A35014DE84BFA0F7DC671D054FC285AA88CAE71C96ECE335FE645E742E647B03812132613213F5D84AE8FA297B69BED1C25133202203C4BA882A391B594A1DD10370733F0261161AF88B6959E9580F20155E449E146E62B57EE42E643E1424DAF94A985B480DE3DBF65DE5BEE4EF00178AC98B982D47B8FE65BEB58ED240950BE86B0AF86BC66F30256C276A791ADED1A21093CE14AEA146BAD72BC8DDB7A9AD144E44FFA52C93DB565D173D86DA4DB1403331F140D364DBB84B291A293B8324014250426133357A3BA8FAE8CB995E20D0D171F2A3E1874FE3FE54EFB5DD51D58C370D179CD4A91EC281420100735A7DC58D949F959F87FF854E062E97CA1C021E85CDF44C66E83F4201B2E1B321B5E9E95A88BA96CDB2D5ED951E454E07F9DD44FED3DF52C1D4FAC86B579B59DB9C62A0721171D073FBBCE7CB08CBA67C7CA370E1328032C1D519788B98EA38AA9C332063FE145C1658BEE26192B1B0B364AB881CF8EA29183D1271217063C1B3EA2DB77C774CE79AE315FF847063CEC2246B498AD70B772D51A5DD450E253C36F818FA896B58BB86BF90F0A2FFE3B183BBD3712222E0C28195FADB680DF5AFE30B0DE73B578BC75D1305CE34AEC37EF4CBB3CE648EB5DEB4A4AAA949598A1B8699DC07CC889B284B4CA2EE95AE65FD648A8D660EE6ED848F97E8CA097A88BBB6D98F95EFF350130E10674D841E158C566A4D068B071C977D7").getBytes();
  134. byte[] outputData = DESCoder.decrypt(inputData, key);
  135. String outputStr = new String(outputData);
  136. System.err.println("解密后:\t" + outputStr);
  137. assertEquals(inputStr, outputStr);
  138. }
  139. }