Guest User

Untitled

a guest
Oct 23rd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. package expressCheckout;
  2.  
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.spec.IvParameterSpec;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. /**
  12. * DecryptParameters decryption class
  13. *
  14. * @author Mula
  15. */
  16. public class DecryptParameters {
  17. /**
  18. * Define the your IV and Secret Key.
  19. */
  20. static String IV = "******";
  21. static String encryptionKey = "*******";
  22.  
  23. /**
  24. * Main function.
  25. *
  26. * @param args the command line arguments
  27. */
  28. public static void main(String[] args) {
  29. try {
  30. // Get the encrypted string from the payload during the callback.
  31. String encryptedString = "**********************";
  32.  
  33. // Instead consume the parameter object as you may wish.
  34. System.out.println(decrypt(hexToBytes(encryptedString), encryptionKey));
  35. } catch (JSONException ex) {
  36. Logger.getLogger(DecryptParameters.class.getName()).log(Level.SEVERE, null, ex);
  37. } catch (Exception ex) {
  38. Logger.getLogger(DecryptParameters.class.getName()).log(Level.SEVERE, null, ex);
  39. }
  40. }
  41.  
  42. /**
  43. * Convert string to byte
  44. *
  45. * @param string String
  46. *
  47. * @return data byte[]
  48. */
  49. public static byte[] hexToBytes(String string) {
  50. if (string == null) {
  51. return null;
  52. } else if (string.length() < 2) {
  53. return null;
  54. } else {
  55. int len = string.length() / 2;
  56. byte[] buffer = new byte[len];
  57. for (int i = 0; i < len; i++) {
  58. buffer[i] = (byte) Integer.parseInt(string.substring(i * 2, i * 2 + 2), 16);
  59. }
  60. return buffer;
  61. }
  62. }
  63.  
  64. /**
  65. * Decrypt the encrypted bytes to string
  66. *
  67. * @param cipherText byte[]
  68. * @param encryptionKey String
  69. *
  70. * @return string String
  71. * @throws java.lang.Exception
  72. */
  73. public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception {
  74. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
  75. SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
  76. cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
  77. return new String(cipher.doFinal(cipherText), "UTF-8");
  78. }
  79. }
Add Comment
Please, Sign In to add comment