PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/common/src/main/java/com/sishuok/es/common/utils/security/DESCoder.java

https://gitlab.com/0072016/es
Java | 132 lines | 36 code | 18 blank | 78 comment | 1 complexity | 279f7d812fe478387920709252ee7aa5 MD5 | raw file
  1. package com.sishuok.es.common.utils.security;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import java.security.Key;
  6. /**
  7. * DES安全编码组件
  8. * <p/>
  9. * <pre>
  10. * 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)
  11. * DES key size must be equal to 56
  12. * DESede(TripleDES) key size must be equal to 112 or 168
  13. * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
  14. * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
  15. * RC2 key size must be between 40 and 1024 bits
  16. * RC4(ARCFOUR) key size must be between 40 and 1024 bits
  17. * 具体内容 需要关注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html
  18. * </pre>
  19. *
  20. * @version 1.0
  21. * @since 1.0
  22. */
  23. public abstract class DESCoder extends Coder {
  24. /**
  25. * ALGORITHM 算法 <br>
  26. * 可替换为以下任意一种算法,同时key值的size相应改变。
  27. * <p/>
  28. * <pre>
  29. * DES key size must be equal to 56
  30. * DESede(TripleDES) key size must be equal to 112 or 168
  31. * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
  32. * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
  33. * RC2 key size must be between 40 and 1024 bits
  34. * RC4(ARCFOUR) key size must be between 40 and 1024 bits
  35. * </pre>
  36. * <p/>
  37. * 在Key toKey(byte[] key)方法中使用下述代码
  38. * <code>SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);</code> 替换
  39. * <code>
  40. * DESKeySpec dks = new DESKeySpec(key);
  41. * SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  42. * SecretKey secretKey = keyFactory.generateSecret(dks);
  43. * </code>
  44. */
  45. public static final String ALGORITHM = "TripleDES";
  46. /**
  47. * 转换密钥<br>
  48. *
  49. * @param key
  50. * @return
  51. * @throws Exception
  52. */
  53. private static Key toKey(byte[] key) throws Exception {
  54. // DESKeySpec dks = new DESKeySpec(key);
  55. // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  56. // SecretKey secretKey = keyFactory.generateSecret(dks);
  57. // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码
  58. SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
  59. return secretKey;
  60. }
  61. /**
  62. * 解密
  63. *
  64. * @param data
  65. * @param key
  66. * @return
  67. * @throws Exception
  68. */
  69. public static byte[] decrypt(byte[] data, String key) throws Exception {
  70. Key k = toKey(decryptBASE64(key));
  71. Cipher cipher = Cipher.getInstance(ALGORITHM);
  72. cipher.init(Cipher.DECRYPT_MODE, k);
  73. return cipher.doFinal(data);
  74. }
  75. /**
  76. * 加密
  77. *
  78. * @param data
  79. * @param key
  80. * @return
  81. * @throws Exception
  82. */
  83. public static byte[] encrypt(byte[] data, String key) throws Exception {
  84. Key k = toKey(decryptBASE64(key));
  85. Cipher cipher = Cipher.getInstance(ALGORITHM);
  86. cipher.init(Cipher.ENCRYPT_MODE, k);
  87. return cipher.doFinal(data);
  88. }
  89. /**
  90. * 生成密钥
  91. *
  92. * @return
  93. * @throws Exception
  94. */
  95. public static String initKey() throws Exception {
  96. return initKey(null);
  97. }
  98. /**
  99. * 生成密钥
  100. *
  101. * @param seed
  102. * @return
  103. * @throws Exception
  104. */
  105. public static String initKey(String seed) throws Exception {
  106. seed = seed + "1234567890987654321012345678901234567890";
  107. byte[] bytes = seed.getBytes();
  108. byte[] result = new byte[24];
  109. for (int i = 0; i < 24; i++) {
  110. result[i] = bytes[i];
  111. }
  112. return encryptBASE64(result);
  113. }
  114. }