ReciprocalUtils.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. *
  18. */
  19. package org.maxkey.crypto;
  20. import java.io.UnsupportedEncodingException;
  21. import java.security.Provider;
  22. import java.security.Security;
  23. import javax.crypto.Cipher;
  24. import javax.crypto.SecretKey;
  25. import javax.crypto.spec.SecretKeySpec;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.maxkey.util.Instance;
  28. import org.maxkey.util.StringGenerator;
  29. /**
  30. * Reciprocal cipher or Symmetric-key algorithm
  31. *
  32. * algorithm Support DES,DESede,Blowfish and AES
  33. *
  34. * default key value use ReciprocalUtils.defaultKey
  35. *
  36. * generateKey is generate random key for algorithm
  37. *
  38. * @author Crystal.Sea
  39. *
  40. */
  41. public final class ReciprocalUtils {
  42. private static final String defaultKey = "l0JqT7NvIzP9oRaG4kFc1QmD_bWu3x8E5yS2h6"; //
  43. public final class Algorithm {
  44. public static final String DES = "DES";
  45. public static final String DESede = "DESede";
  46. public static final String Blowfish = "Blowfish";
  47. public static final String AES = "AES";
  48. }
  49. static {
  50. if(System.getProperty("java.version").startsWith("1.8")) {
  51. try {
  52. Security.addProvider((Provider)Instance.newInstance("com.sun.crypto.provider.SunJCE"));
  53. }catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. //else not need to add see jdk-17\conf\security\java.security,SunJCE
  58. }
  59. public static byte[] encode(byte[] simpleBytes, SecretKey secretKey, String algorithm) {
  60. // Create the ciphers
  61. Cipher ecipher;
  62. byte[] byteFinal = null;
  63. try {
  64. ecipher = Cipher.getInstance(secretKey.getAlgorithm());
  65. // Encode the string into bytes using utf-8
  66. ecipher.init(Cipher.ENCRYPT_MODE, secretKey);
  67. // Encrypt
  68. byteFinal = ecipher.doFinal(simpleBytes);
  69. return byteFinal;
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. return null;
  74. }
  75. /**
  76. * @param simple
  77. * @param secretKey must length
  78. * @return
  79. * @throws Exception
  80. */
  81. public static byte[] encode(String simple, String secretKey, String algorithm) {
  82. if (keyLengthCheck(secretKey, algorithm)) {
  83. SecretKey key = generatorKey(secretKey, algorithm);
  84. try {
  85. return encode(simple.getBytes("UTF-8"), key, algorithm);
  86. } catch (UnsupportedEncodingException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. return null;
  91. }
  92. public static byte[] decoder(byte[] ciphersBytes, SecretKey secretKey, String algorithm) {
  93. Cipher cipher;
  94. byte[] byteFinal = null;
  95. try {
  96. cipher = Cipher.getInstance(algorithm);
  97. cipher.init(Cipher.DECRYPT_MODE, secretKey);
  98. byteFinal = cipher.doFinal(ciphersBytes);
  99. // String simple=new String(byteFinal, "UTF8" );
  100. // return simple;
  101. return byteFinal;
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. } finally {
  105. cipher = null;
  106. }
  107. return null;
  108. }
  109. public static String decoder(byte[] ciphersBytes, String secretKey, String algorithm) {
  110. if (keyLengthCheck(secretKey, algorithm)) {
  111. SecretKey key = generatorKey(secretKey, algorithm);
  112. try {
  113. return new String(decoder(ciphersBytes, key, algorithm), "UTF8");
  114. } catch (UnsupportedEncodingException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. return null;
  119. }
  120. public static byte[] encodeByDefaultKey(String simple, String algorithm) {
  121. SecretKey key = generatorDefaultKey(algorithm);
  122. return encode(simple.getBytes(), key, algorithm);
  123. }
  124. public static String encode2HexByDefaultKey(String simple, String algorithm) {
  125. byte[] byteFinal = encodeByDefaultKey(simple, algorithm);
  126. String cipherHex = HexUtils.bytes2HexString(byteFinal);
  127. return cipherHex;
  128. }
  129. public static byte[] decoderByDefaultKey(byte[] byteCiphers, String algorithm) {
  130. SecretKey key = generatorDefaultKey(algorithm);
  131. return decoder(byteCiphers, key, algorithm);
  132. }
  133. public static String decoderHexByDefaultKey(String ciphers, String algorithm) {
  134. byte[] byteSimple = HexUtils.hex2Bytes(ciphers);
  135. byte[] byteFinal = decoderByDefaultKey(byteSimple, algorithm);
  136. String simple = null;
  137. try {
  138. simple = new String(byteFinal, "UTF-8");
  139. } catch (UnsupportedEncodingException e) {
  140. // TODO Auto-generated catch block
  141. e.printStackTrace();
  142. }
  143. return simple;
  144. }
  145. public static SecretKey generatorDefaultKey(String algorithm) {
  146. try {
  147. String secretKey = defaultKey;
  148. if (algorithm.equals(Algorithm.DES)) {
  149. secretKey = defaultKey.substring(0, 8);
  150. } else if (algorithm.equals(Algorithm.AES) || algorithm.equals(Algorithm.Blowfish)) {
  151. secretKey = defaultKey.substring(0, 16);
  152. } else if (algorithm.equals(Algorithm.DESede)) {
  153. secretKey = defaultKey.substring(0, 24);
  154. }
  155. // System.out.println("defaultKey : "+secretKey);
  156. SecretKey key = new SecretKeySpec(secretKey.getBytes(), algorithm);
  157. return key;
  158. } catch (Exception e) {
  159. e.printStackTrace();
  160. }
  161. return null;
  162. }
  163. private static SecretKey generatorKey(String secretKey, String algorithm) {
  164. try {
  165. SecretKey key = new SecretKeySpec(secretKey.getBytes(), algorithm);
  166. return key;
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. }
  170. return null;
  171. }
  172. public static String encode2Hex(String simple, String secretKey, String algorithm) {
  173. if (keyLengthCheck(secretKey, algorithm)) {
  174. byte[] cipher = encode(simple, secretKey, algorithm);
  175. // Encode bytes to HEX to get a string
  176. return HexUtils.bytes2HexString(cipher);
  177. }
  178. return null;
  179. }
  180. public static String decoderHex(String ciphers, String secretKey, String algorithm) {
  181. if (keyLengthCheck(secretKey, algorithm)) {
  182. byte[] byteSimple = HexUtils.hex2Bytes(ciphers);
  183. return decoder(byteSimple, secretKey, algorithm);
  184. }
  185. return null;
  186. }
  187. private static boolean keyLengthCheck(String secretKey, String algorithm) {
  188. boolean lengthCheck = false;
  189. if (algorithm.equals(Algorithm.DES)) {
  190. if (secretKey.length() == 8) {
  191. lengthCheck = true;
  192. } else {
  193. LogFactory.getLog(ReciprocalUtils.class)
  194. .debug("key length is " + secretKey.getBytes().length + " ,must lequal 8");
  195. }
  196. } else if (algorithm.equals(Algorithm.DESede)) {
  197. if (secretKey.length() == 24) {
  198. lengthCheck = true;
  199. } else {
  200. LogFactory.getLog(ReciprocalUtils.class)
  201. .debug("key length is " + secretKey.getBytes().length + " ,must equal 24");
  202. }
  203. } else if (algorithm.equals(Algorithm.AES)) {
  204. if (secretKey.length() == 16) {
  205. lengthCheck = true;
  206. } else {
  207. LogFactory.getLog(ReciprocalUtils.class)
  208. .debug("key length is " + secretKey.getBytes().length + " ,must equal 16");
  209. }
  210. } else if (algorithm.equals(Algorithm.Blowfish)) {
  211. if (secretKey.length() <= 16) {
  212. lengthCheck = true;
  213. } else {
  214. LogFactory.getLog(ReciprocalUtils.class)
  215. .debug("key length is " + secretKey.getBytes().length + " ,must be less then 16");
  216. }
  217. }
  218. return lengthCheck;
  219. }
  220. /**
  221. * @param simple
  222. * @param secretKey must length is 16
  223. * @return
  224. */
  225. public static String aesEncode(String simple, String secretKey) {
  226. return encode2Hex(simple, secretKey, Algorithm.AES);
  227. }
  228. public static String aesDecoder(String ciphers, String secretKey) {
  229. return decoderHex(ciphers, secretKey, Algorithm.AES);
  230. }
  231. /**
  232. * encode by defaultKey with Algorithm.AES
  233. *
  234. * @param simple
  235. * @return Hex
  236. */
  237. public static String encode(String simple) {
  238. return encode2HexByDefaultKey(simple, Algorithm.AES);
  239. }
  240. /**
  241. * decoder by defaultKey with Algorithm.AES
  242. *
  243. * @param ciphers is HEX
  244. *
  245. * @return
  246. */
  247. public static String decoder(String ciphers) {
  248. return decoderHexByDefaultKey(ciphers, Algorithm.AES);
  249. }
  250. public static String generateKey(String algorithm) {
  251. if (algorithm.equals(Algorithm.DES)) {
  252. return (new StringGenerator(8)).randomGenerate();
  253. } else if (algorithm.equals(Algorithm.AES)) {
  254. return (new StringGenerator(16)).randomGenerate();
  255. } else if (algorithm.equals(Algorithm.Blowfish)) {
  256. return (new StringGenerator(16)).randomGenerate();
  257. } else if (algorithm.equals(Algorithm.DESede)) {
  258. return (new StringGenerator(24)).randomGenerate();
  259. } else {
  260. return (new StringGenerator()).uniqueGenerate();
  261. }
  262. }
  263. }