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

https://bitbucket.org/invest/anyoption · Java · 60 lines · 48 code · 12 blank · 0 comment · 0 complexity · ed202f1c0c60bdedc21b459264c7675a MD5 · raw file

  1. package com.anyoption.common.util;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import java.net.URLEncoder;
  5. import java.nio.charset.StandardCharsets;
  6. import org.apache.commons.codec.binary.Base64;
  7. public class TCToken {
  8. private static final String TCTOKEN_SECRET_KEY = "OyY1d0RbUiNidjp9JDBUMQ==";
  9. private static final byte[] key = org.apache.commons.codec.binary.Base64.decodeBase64(TCTOKEN_SECRET_KEY.getBytes());
  10. public static final String TCTOKEN_ID = "AnyOption";
  11. public static final String TCTOKEN_SEPARATOR = ",";
  12. static public void main(String args[]) throws Exception {
  13. String testString="AnyOption,VWUaI,fr,1473936700";
  14. byte[] plainData = testString.getBytes(StandardCharsets.UTF_8);
  15. System.out.println("!!!"+new String(key, StandardCharsets.UTF_8)+"!!!");
  16. System.out.println("!!!"+new String(encrypt(plainData, key), StandardCharsets.UTF_8)+"!!!");
  17. System.out.println("!!!"+new String(decrypt(encrypt(plainData, key), key), StandardCharsets.UTF_8)+"!!!");
  18. System.out.println("!!!"+getToken(testString)+"!!!");
  19. }
  20. private static byte[] encrypt(byte[] input, byte[] key) throws Exception {
  21. Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
  22. SecretKeySpec keySpec = new SecretKeySpec(key, "Blowfish");
  23. cipher.init(Cipher.ENCRYPT_MODE, keySpec);
  24. return new Base64().encode(cipher.doFinal(input));
  25. }
  26. private static byte[] decrypt(byte[] input, byte[] key) throws Exception {
  27. Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
  28. SecretKeySpec keySpec = new SecretKeySpec(key, "Blowfish");
  29. cipher.init(Cipher.DECRYPT_MODE, keySpec);
  30. return cipher.doFinal(new Base64().decode(input));
  31. }
  32. public static synchronized String encryptStringToString(String data) throws Exception {
  33. byte[] plainData = data.getBytes(StandardCharsets.UTF_8);
  34. return new String(encrypt(plainData, key), StandardCharsets.UTF_8);
  35. }
  36. public static synchronized String decryptStringToString(String data) throws Exception {
  37. byte[] encryptedData = data.getBytes(StandardCharsets.UTF_8);
  38. return new String(decrypt(encryptedData, key), StandardCharsets.UTF_8);
  39. }
  40. public static synchronized String getToken(String parts) {
  41. try {
  42. return URLEncoder.encode(encryptStringToString(parts), "UTF-8");
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. return "unable to process token";
  46. }
  47. }
  48. }