PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/anyoption_common_merge/src/com/anyoption/common/util/AESUtil.java

https://bitbucket.org/invest/anyoption
Java | 148 lines | 130 code | 13 blank | 5 comment | 10 complexity | 554d854dc7ad2c9abb7dd3e08f2d3952 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. package com.anyoption.common.util;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.UnsupportedEncodingException;
  6. import java.security.InvalidAlgorithmParameterException;
  7. import java.security.InvalidKeyException;
  8. import java.security.KeyStore;
  9. import java.security.KeyStoreException;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.security.UnrecoverableKeyException;
  12. import java.security.cert.CertificateException;
  13. import javax.crypto.BadPaddingException;
  14. import javax.crypto.Cipher;
  15. import javax.crypto.IllegalBlockSizeException;
  16. import javax.crypto.NoSuchPaddingException;
  17. import javax.crypto.spec.IvParameterSpec;
  18. import org.apache.log4j.Logger;
  19. import org.bouncycastle.crypto.CryptoException;
  20. //"C:\Program Files\Java\jdk1.8.0_102\jre\bin\keytool.exe" -genseckey -alias "encryption_key" -keyalg "AES" -keysize 256 -storetype "jceks" -keystore "test_keystore" -storepass "123456"
  21. //"C:\Program Files\Java\jdk1.8.0_102\jre\bin\keytool.exe" -list -keystore "C:\work\tools\tomcat-7.0.12\conf\test_keystore" -storetype "jceks" -alias "encryption_key" -storepass "123456"
  22. public class AESUtil {
  23. private static final Logger log = Logger.getLogger(AESUtil.class);
  24. private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
  25. private static final IvParameterSpec IV = new IvParameterSpec(new byte[] { 53, 65, -127, 34, 94, -27, -2, 98, 93, -90, -53, 12, -121,
  26. -47, 93, -45});
  27. // private static final String secretKey = "dh3dilb68";
  28. private static final String PASSWORD = "lpzU5d9LwTkmFrsjk2ZL82AeFLGCijUtc8B9VO4AA6we";
  29. private static final String KEY_ALIAS = "encryption_key";
  30. private static final String KEYSTORE_FILE_PATH = "keystore.file.path";
  31. private static final String SYSTEM_ENCRYPTION = "system.encryption";
  32. private static java.security.Key secretKeySpec;
  33. static {
  34. InputStream stream = null;
  35. try {
  36. KeyStore store = KeyStore.getInstance("JCEKS");
  37. String keyStorePath = System.getProperty(KEYSTORE_FILE_PATH);
  38. if (keyStorePath == null) { // default place
  39. keyStorePath = System.getProperty("catalina.base") + "/conf/keystore";
  40. }
  41. stream = new FileInputStream(keyStorePath);
  42. store.load(stream, PASSWORD.toCharArray());
  43. secretKeySpec = store.getKey(KEY_ALIAS, PASSWORD.toCharArray());
  44. } catch (NoSuchAlgorithmException | IOException | CertificateException | KeyStoreException | UnrecoverableKeyException e) {
  45. log.error("Unable to load keystore or key", e);
  46. } finally {
  47. try {
  48. stream.close();
  49. } catch (IOException e) {
  50. log.warn("Unable to close file stream", e);
  51. }
  52. }
  53. }
  54. private static SupportedEncryption systemEncryption;
  55. static {
  56. try {
  57. String encryption = System.getProperty(SYSTEM_ENCRYPTION);
  58. if (encryption != null) {
  59. systemEncryption = SupportedEncryption.valueOf(encryption);
  60. } else {
  61. systemEncryption = SupportedEncryption.AES;
  62. }
  63. } catch (IllegalArgumentException e) {
  64. log.error("Unable to load system encryption. Initializing default AES encryption", e);
  65. systemEncryption = SupportedEncryption.AES;
  66. }
  67. }
  68. private enum SupportedEncryption {
  69. AES, BLOWFISH;
  70. }
  71. @SuppressWarnings("deprecation")
  72. public static String encrypt(String plainText) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
  73. IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException,
  74. CryptoException, InvalidAlgorithmParameterException {
  75. switch (systemEncryption) {
  76. case BLOWFISH:
  77. return Encryptor.encryptStringToString(plainText);
  78. case AES:
  79. /* falls through */
  80. default:
  81. return encryptRaw(plainText);
  82. }
  83. }
  84. public static String encrypt(long number) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
  85. IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException,
  86. CryptoException, InvalidAlgorithmParameterException {
  87. return encrypt(String.valueOf(number));
  88. }
  89. @SuppressWarnings("deprecation")
  90. public static String decrypt(String encryptedString) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
  91. NoSuchAlgorithmException, NoSuchPaddingException, CryptoException,
  92. InvalidAlgorithmParameterException {
  93. switch (systemEncryption) {
  94. case BLOWFISH:
  95. return Encryptor.decryptStringToString(encryptedString);
  96. case AES:
  97. /* falls through */
  98. default:
  99. return decryptRaw(encryptedString);
  100. }
  101. }
  102. private static String encryptRaw(String plainText) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
  103. IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException,
  104. InvalidAlgorithmParameterException {
  105. Cipher cipher = Cipher.getInstance(ALGORITHM);
  106. cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, IV);
  107. byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
  108. return HexUtil.byteArrayToHexString(encrypted);
  109. }
  110. private static String decryptRaw(String encryptedString) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
  111. NoSuchAlgorithmException, NoSuchPaddingException,
  112. InvalidAlgorithmParameterException {
  113. Cipher cipher = Cipher.getInstance(ALGORITHM);
  114. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, IV);
  115. byte[] original = cipher.doFinal(HexUtil.hexStringToByteArray(encryptedString));
  116. return new String(original);
  117. }
  118. @SuppressWarnings("deprecation")
  119. public static String migrateEncryption(String encryptedString) throws InvalidKeyException, NoSuchAlgorithmException,
  120. NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException,
  121. UnsupportedEncodingException, CryptoException,
  122. InvalidAlgorithmParameterException {
  123. return encryptRaw(Encryptor.decryptStringToString(encryptedString));
  124. }
  125. public static void main(String[] args) throws Exception {
  126. if (args.length == 1) {
  127. String str = encrypt(args[0]);
  128. System.out.println("System encryption: " + systemEncryption);
  129. System.out.println(args[0] + ":" + str);
  130. System.out.println(str + ":" + decrypt(str));
  131. } else {
  132. System.out.println("USAGE: java AESUtil string-to-encrypt");
  133. }
  134. }
  135. }