DESCoder.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * FileName:DESCoder.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util.coder;
  20. import cn.com.lzt.common.util.security.SecurityCoder;
  21. import javax.crypto.*;
  22. import javax.crypto.spec.DESKeySpec;
  23. import java.security.InvalidKeyException;
  24. import java.security.Key;
  25. import java.security.NoSuchAlgorithmException;
  26. import java.security.SecureRandom;
  27. import java.security.spec.InvalidKeySpecException;
  28. /**
  29. * DES安全编码组件
  30. *
  31. * @author ShenHuaJie
  32. * @version 1.0
  33. * @since 1.0
  34. */
  35. public abstract class DESCoder extends SecurityCoder {
  36. /**
  37. * 密钥算法 <br>
  38. * Java 6 只支持56bit密钥 <br>
  39. * Bouncy Castle 支持64bit密钥
  40. */
  41. public static final String KEY_ALGORITHM = "DES";
  42. /**
  43. * 加密/解密算法 / 工作模式 / 填充方式
  44. */
  45. public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5PADDING";
  46. /**
  47. * 转换密钥
  48. *
  49. * @param key 二进制密钥
  50. * @return Key 密钥
  51. * @throws InvalidKeyException
  52. * @throws NoSuchAlgorithmException
  53. * @throws InvalidKeySpecException
  54. * @throws Exception
  55. */
  56. private static Key toKey(byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
  57. // 实例化DES密钥材料
  58. DESKeySpec dks = new DESKeySpec(key);
  59. // 实例化秘密密钥工厂
  60. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
  61. // 生成秘密密钥
  62. SecretKey secretKey = keyFactory.generateSecret(dks);
  63. return secretKey;
  64. }
  65. /**
  66. * 解密
  67. *
  68. * @param data 待解密数据
  69. * @param key 密钥
  70. * @return byte[] 解密数据
  71. * @throws InvalidKeySpecException
  72. * @throws NoSuchAlgorithmException
  73. * @throws InvalidKeyException
  74. * @throws NoSuchPaddingException
  75. * @throws BadPaddingException
  76. * @throws IllegalBlockSizeException
  77. * @throws Exception
  78. */
  79. public static byte[] decrypt(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException,
  80. InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
  81. // 还原密钥
  82. Key k = toKey(key);
  83. // 实例化
  84. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
  85. // 初始化,设置为解密模式
  86. cipher.init(Cipher.DECRYPT_MODE, k);
  87. // 执行操作
  88. return cipher.doFinal(data);
  89. }
  90. /**
  91. * 加密
  92. *
  93. * @param data 待加密数据
  94. * @param key 密钥
  95. * @return byte[] 加密数据
  96. * @throws NoSuchPaddingException
  97. * @throws NoSuchAlgorithmException
  98. * @throws InvalidKeyException
  99. * @throws BadPaddingException
  100. * @throws IllegalBlockSizeException
  101. * @throws InvalidKeySpecException
  102. * @throws Exception
  103. */
  104. public static byte[] encrypt(byte[] data, byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException,
  105. InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
  106. // 还原密钥
  107. Key k = toKey(key);
  108. // 实例化
  109. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
  110. // 初始化,设置为加密模式
  111. cipher.init(Cipher.ENCRYPT_MODE, k);
  112. // 执行操作
  113. return cipher.doFinal(data);
  114. }
  115. /**
  116. * 生成密钥 <br>
  117. * Java 6 只支持56bit密钥 <br>
  118. * Bouncy Castle 支持64bit密钥 <br>
  119. *
  120. * @return byte[] 二进制密钥
  121. * @throws NoSuchAlgorithmException
  122. * @throws Exception
  123. */
  124. public static byte[] initKey() throws NoSuchAlgorithmException {
  125. /*
  126. * 实例化密钥生成器
  127. *
  128. * 若要使用64bit密钥注意替换 将下述代码中的KeyGenerator.getInstance(CIPHER_ALGORITHM);
  129. * 替换为KeyGenerator.getInstance(CIPHER_ALGORITHM, "BC");
  130. */
  131. KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
  132. /*
  133. * 初始化密钥生成器 若要使用64bit密钥注意替换 将下述代码kg.init(56); 替换为kg.init(64);
  134. */
  135. kg.init(56, new SecureRandom());
  136. // 生成秘密密钥
  137. SecretKey secretKey = kg.generateKey();
  138. // 获得密钥的二进制编码形式
  139. return secretKey.getEncoded();
  140. }
  141. }