fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static final byte[] SALT = { (byte) 0xC7, (byte) 0xB5, (byte) 0x99,
  11. (byte) 0xF4, (byte) 0x4B, (byte) 0x7C, (byte) 0x81, (byte) 0x77 };
  12. private static final int ITERATION_COUNT = 65536;
  13. private static final int KEY_LENGTH = 256;
  14. private static final int IV_LENGTH = 16;
  15. private Cipher eCipher;
  16. private Cipher dCipher;
  17. private byte[] encrypt;
  18. private byte[] iv;
  19.  
  20. public CryptoUtils(String passPhrase) {
  21. try {
  22. SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  23. KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
  24. SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
  25. SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");
  26.  
  27. eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  28. eCipher.init(Cipher.ENCRYPT_MODE, secretKey);
  29.  
  30. dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  31. iv = eCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
  32. dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
  33. } catch (Exception ex) {
  34. ex.printStackTrace();
  35. }
  36. }
  37.  
  38. public CryptoUtils(String passPhrase, String encryptedString) {
  39. try {
  40. System.out.println("Encrypted String : " + encryptedString);
  41. SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  42. KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
  43. SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
  44. SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(),"AES");
  45.  
  46. encrypt = Base64.decodeBase64(encryptedString.toString());
  47.  
  48. System.out.println("Encrypt : " + encrypt);
  49. eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  50. eCipher.init(Cipher.ENCRYPT_MODE, secretKey);
  51.  
  52. byte[] iv = extractIV();
  53. System.out.println("HEX - 1 : " + Hex.encodeHexString("xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=".getBytes("UTF8")));
  54. System.out.println( "HEX : " + Hex.encodeHexString( iv ) );
  55. System.out.println("IV : " + Base64.encodeBase64(iv));
  56. dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  57. dCipher.init(Cipher.DECRYPT_MODE, secretKey,
  58. new IvParameterSpec(iv));
  59. } catch (Exception ex) {
  60. ex.printStackTrace();
  61. }
  62. }
  63.  
  64. public String encrypt(String encrypt) {
  65. String encStr = null;
  66.  
  67. try {
  68. byte[] bytes = encrypt.getBytes("UTF8");
  69. byte[] encrypted = encrypt(bytes);
  70. byte[] cipherText = new byte[encrypted.length + iv.length];
  71. System.arraycopy(iv, 0, cipherText, 0, iv.length);
  72. System.arraycopy(encrypted, 0, cipherText, iv.length,
  73. encrypted.length);
  74. encStr = new String(Base64.encodeBase64(cipherText));
  75. } catch (Exception ex) {
  76. ex.printStackTrace();
  77. }
  78.  
  79. return encStr;
  80. }
  81.  
  82. public byte[] encrypt(byte[] plain) throws Exception {
  83. return eCipher.doFinal(plain);
  84. }
  85.  
  86. private byte[] extractIV() {
  87. byte[] iv = new byte[IV_LENGTH];
  88. System.arraycopy(encrypt, 0, iv, 0, iv.length);
  89. return iv;
  90. }
  91.  
  92. public String decrypt() {
  93. String decStr = null;
  94.  
  95. try {
  96. byte[] bytes = extractCipherText();
  97.  
  98. byte[] decrypted = decrypt(bytes);
  99. decStr = new String(decrypted, "UTF8");
  100. } catch (Exception ex) {
  101. ex.printStackTrace();
  102. }
  103.  
  104. return decStr;
  105. }
  106.  
  107. private byte[] extractCipherText() {
  108. byte[] ciphertext = new byte[encrypt.length - IV_LENGTH];
  109. System.arraycopy(encrypt, 16, ciphertext, 0, ciphertext.length);
  110. return ciphertext;
  111. }
  112.  
  113. public byte[] decrypt(byte[] encrypt) throws Exception {
  114. return dCipher.doFinal(encrypt);
  115. }
  116.  
  117. public static String generateNewKey() {
  118. String newKey = null;
  119.  
  120. try {
  121. // Get the KeyGenerator
  122. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  123. kgen.init(256); // 128, 192 and 256 bits available
  124.  
  125. // Generate the secret key specs.
  126. SecretKey skey = kgen.generateKey();
  127. byte[] raw = skey.getEncoded();
  128.  
  129. newKey = new String(Base64.encodeBase64(raw));
  130. } catch (Exception ex) {
  131. ex.printStackTrace();
  132. }
  133.  
  134. return newKey;
  135. }
  136.  
  137. public static String generateNewSalt() {
  138. String newSalt = null;
  139.  
  140. try {
  141. byte[] salt = new byte[8];
  142.  
  143. // Generate random salt
  144. SecureRandom rnd = new SecureRandom();
  145. rnd.nextBytes(salt);
  146.  
  147. newSalt = Hex.encodeHexString(salt);
  148. } catch (Exception ex) {
  149. ex.printStackTrace();
  150. }
  151.  
  152. return newSalt;
  153. }
  154. public static void main (String[] args) throws java.lang.Exception
  155. {
  156. try {
  157. Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
  158. field.setAccessible(true);
  159. field.set(null, java.lang.Boolean.FALSE);
  160. } catch (Exception ex) {
  161. ex.printStackTrace();
  162. }
  163.  
  164. String newKey = generateNewKey();
  165. System.out.println("New Key: " + newKey);
  166.  
  167. String newSalt = generateNewSalt();
  168. System.out.println("New Salt: " + newSalt);
  169.  
  170. String key = "wV7AzhvrZDhlIJt4UZ1wJQZ+qgtU1dWZ+7Ia6Nld4wk=";
  171. String str = "xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=";
  172. System.out.println("Original String: " + str);
  173.  
  174. String s1 = "7morqFA4B2/xtn8LXOH3wFvozYJpO+os+x9xgo/LQf299GKAQoTxP7s57Bd769Y7a/jLMX8tqsA4IVDCAC245t3SD2F/oGikrfdxQCr1Z0uytZIHctuWzYpN+ZR7suFh";
  175. byte[] bytes = Base64.decodeBase64(s1);
  176. String s2 = new String(bytes);
  177. //Base64.decodeBase64(s1);
  178. System.out.println("the string " + s2);
  179. /*AES256 aesEncrypt = new AES256(key);
  180. String encryptedStr = aesEncrypt.encrypt(str);
  181. System.out.println("Encrypted String: " + encryptedStr);
  182.  
  183. AES256 aesDecrypt = new AES256(key, encryptedStr);
  184. String decryptedStr = aesDecrypt.decrypt();
  185. System.out.println("Decrypted String: " + decryptedStr);
  186.  
  187. String key = "xh7G5+KBmA57N7RkXxhO6l1GpJWc/VHbiwCcN2O7vLM=";
  188. String str = "Hello World!";
  189. System.out.println("Original String: " + str);*/
  190.  
  191. String merchantKey = "xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=";
  192. String msg = "ag|171120150003|12345|15000|IND|INR|Online|1|15000|Electronics|MerchantSuccess.html|MerchantFailure.html|IFrame";
  193. CryptoUtils aesEncrypt = new CryptoUtils(merchantKey);
  194. String encryptedStr = aesEncrypt.encrypt(msg);
  195. System.out.println("Encrypted String: " + encryptedStr);
  196. //"usy9Z/6QkJyQVcufN/J391ZCAv 4IJ17e8M3BTj1GYhqv964zrDXMa0TCkXHOZyJQ5FVLrK3hd6i/1EfekbZJ8 0JstgX3wMVsThsd8blIHKG0M B21jmPK5qal/g2wR0EPjPSDc9ean8at9NtBJ8qkTZmhHTEyjHAENAJAJ/uU="
  197. //"jRcR+NL2y4Csq59YIyd1E+UcdXl+FKDgaOwCnqRmGRHEQ6ZcSYpxEGO+JZx1dQyuRQVsxjeLI3ILoUjNUtn8I3okuPRMR5KPzXcI5iI4FzwwoBnGQMAdliKejAgkPV2JFZe5mBcWi1Z7TW8xlAHUg3eZVR9RmiuU8j4fez6wMVo="
  198. /*String eString = "xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=";
  199. CryptoUtils aesDecrypt = new CryptoUtils(eString, "U2FsdGVkX19pw/KVybNcOL/34QyeTrSQoUVkhqCyTsizXX9N94bBGOdy/EmVN1aKcyvbzpOAiP5KA4Qr+lBi3aprudeAJZp6xu6EfESQuPLlcbbSgYedic4PyQk0Jfwo+NP2BLDfc8bIuD/w8PcC5A==");
  200. String decryptedStr = aesDecrypt.decrypt();
  201. System.out.println("Decrypted String: " + decryptedStr);*/
  202.  
  203. //String eString = "xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=";
  204. CryptoUtils aesDecrypt = new CryptoUtils(merchantKey, "usy9Z/6QkJyQVcufN/J391ZCAv 4IJ17e8M3BTj1GYhqv964zrDXMa0TCkXHOZyJQ5FVLrK3hd6i/1EfekbZJ8 0JstgX3wMVsThsd8blIHKG0M B21jmPK5qal/g2wR0EPjPSDc9ean8at9NtBJ8qkTZmhHTEyjHAENAJAJ/uU=");
  205. //CryptoUtils aesDecrypt = new CryptoUtils(merchantKey, "Fz7flXHzjzHdxe5Ct6S1xQUL7Ni9XcnxarO/KljjNEwOlMcazib1oi3rUuIjoJOaWB308krkeMBG52IuD1sezqEdd/MLcWj5iuyzzNXvBlW1WoaqBGo+joZQyArAOTPBzp+miKXfYZXS1kwKfvv8ZQ==");
  206.  
  207. String decryptedStr = aesDecrypt.decrypt();
  208. System.out.println("Decrypted String: " + decryptedStr);
  209. }
  210. }
Success #stdin #stdout 0.02s 25992KB
stdin
Standard input is empty
stdout
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	private static final byte[] SALT = { (byte) 0xC7, (byte) 0xB5, (byte) 0x99,
			(byte) 0xF4, (byte) 0x4B, (byte) 0x7C, (byte) 0x81, (byte) 0x77 };
	private static final int ITERATION_COUNT = 65536;
	private static final int KEY_LENGTH = 256;
	private static final int IV_LENGTH = 16;
	private Cipher eCipher;
	private Cipher dCipher;
	private byte[] encrypt;
	private byte[] iv;

	public CryptoUtils(String passPhrase) {
		try {
			SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
			KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
			SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
			SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");

			eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

			dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			iv = eCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
			dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public CryptoUtils(String passPhrase, String encryptedString) {
		try {
			System.out.println("Encrypted String : " + encryptedString);
			SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
			KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
			SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
			SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(),"AES");

			encrypt = Base64.decodeBase64(encryptedString.toString());

			System.out.println("Encrypt : " + encrypt);
			eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

			byte[] iv = extractIV();
			System.out.println("HEX - 1 : " + Hex.encodeHexString("xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=".getBytes("UTF8")));
			System.out.println( "HEX : " + Hex.encodeHexString( iv ) );
			System.out.println("IV : " + Base64.encodeBase64(iv));
			dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			dCipher.init(Cipher.DECRYPT_MODE, secretKey,
					new IvParameterSpec(iv));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public String encrypt(String encrypt) {
		String encStr = null;

		try {
			byte[] bytes = encrypt.getBytes("UTF8");
			byte[] encrypted = encrypt(bytes);
			byte[] cipherText = new byte[encrypted.length + iv.length];
			System.arraycopy(iv, 0, cipherText, 0, iv.length);
			System.arraycopy(encrypted, 0, cipherText, iv.length,
					encrypted.length);
			encStr = new String(Base64.encodeBase64(cipherText));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return encStr;
	}

	public byte[] encrypt(byte[] plain) throws Exception {
		return eCipher.doFinal(plain);
	}

	private byte[] extractIV() {
		byte[] iv = new byte[IV_LENGTH];
		System.arraycopy(encrypt, 0, iv, 0, iv.length);
		return iv;
	}

	public String decrypt() {
		String decStr = null;

		try {
			byte[] bytes = extractCipherText();

			byte[] decrypted = decrypt(bytes);
			decStr = new String(decrypted, "UTF8");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return decStr;
	}

	private byte[] extractCipherText() {
		byte[] ciphertext = new byte[encrypt.length - IV_LENGTH];
		System.arraycopy(encrypt, 16, ciphertext, 0, ciphertext.length);
		return ciphertext;
	}

	public byte[] decrypt(byte[] encrypt) throws Exception {
		return dCipher.doFinal(encrypt);
	}

	public static String generateNewKey() {
		String newKey = null;

		try {
			// Get the KeyGenerator
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(256); // 128, 192 and 256 bits available

			// Generate the secret key specs.
			SecretKey skey = kgen.generateKey();
			byte[] raw = skey.getEncoded();

			newKey = new String(Base64.encodeBase64(raw));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return newKey;
	}

	public static String generateNewSalt() {
		String newSalt = null;

		try {
			byte[] salt = new byte[8];

			// Generate random salt
			SecureRandom rnd = new SecureRandom();
			rnd.nextBytes(salt);

			newSalt = Hex.encodeHexString(salt);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return newSalt;
	}
	public static void main (String[] args) throws java.lang.Exception
	{
		try { 
			Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
			field.setAccessible(true);
			field.set(null, java.lang.Boolean.FALSE); 
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		String newKey = generateNewKey();
		System.out.println("New Key: " + newKey);

		String newSalt = generateNewSalt();
		System.out.println("New Salt: " + newSalt);

		String key = "wV7AzhvrZDhlIJt4UZ1wJQZ+qgtU1dWZ+7Ia6Nld4wk=";
		String str = "xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=";
		System.out.println("Original String: " + str);
		
		String s1 = "7morqFA4B2/xtn8LXOH3wFvozYJpO+os+x9xgo/LQf299GKAQoTxP7s57Bd769Y7a/jLMX8tqsA4IVDCAC245t3SD2F/oGikrfdxQCr1Z0uytZIHctuWzYpN+ZR7suFh";
		byte[] bytes = Base64.decodeBase64(s1);
		String s2 = new String(bytes); 
		//Base64.decodeBase64(s1);
		System.out.println("the string " + s2);
		/*AES256 aesEncrypt = new AES256(key);
		String encryptedStr = aesEncrypt.encrypt(str);
		System.out.println("Encrypted String: " + encryptedStr);

		AES256 aesDecrypt = new AES256(key, encryptedStr);
		String decryptedStr = aesDecrypt.decrypt();
		System.out.println("Decrypted String: " + decryptedStr);
		
		String key = "xh7G5+KBmA57N7RkXxhO6l1GpJWc/VHbiwCcN2O7vLM=";
		String str = "Hello World!";
		System.out.println("Original String: " + str);*/
		
		String merchantKey = "xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=";
		String msg = "ag|171120150003|12345|15000|IND|INR|Online|1|15000|Electronics|MerchantSuccess.html|MerchantFailure.html|IFrame";
		CryptoUtils aesEncrypt = new CryptoUtils(merchantKey);
		String encryptedStr = aesEncrypt.encrypt(msg);
		System.out.println("Encrypted String: " + encryptedStr);
		//"usy9Z/6QkJyQVcufN/J391ZCAv 4IJ17e8M3BTj1GYhqv964zrDXMa0TCkXHOZyJQ5FVLrK3hd6i/1EfekbZJ8 0JstgX3wMVsThsd8blIHKG0M B21jmPK5qal/g2wR0EPjPSDc9ean8at9NtBJ8qkTZmhHTEyjHAENAJAJ/uU="
		//"jRcR+NL2y4Csq59YIyd1E+UcdXl+FKDgaOwCnqRmGRHEQ6ZcSYpxEGO+JZx1dQyuRQVsxjeLI3ILoUjNUtn8I3okuPRMR5KPzXcI5iI4FzwwoBnGQMAdliKejAgkPV2JFZe5mBcWi1Z7TW8xlAHUg3eZVR9RmiuU8j4fez6wMVo="
		/*String eString = "xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=";
		CryptoUtils aesDecrypt = new CryptoUtils(eString, "U2FsdGVkX19pw/KVybNcOL/34QyeTrSQoUVkhqCyTsizXX9N94bBGOdy/EmVN1aKcyvbzpOAiP5KA4Qr+lBi3aprudeAJZp6xu6EfESQuPLlcbbSgYedic4PyQk0Jfwo+NP2BLDfc8bIuD/w8PcC5A==");
		String decryptedStr = aesDecrypt.decrypt();
		System.out.println("Decrypted String: " + decryptedStr);*/
		
		//String eString = "xo3HErgdU5/zIvfzafD9muZrakm+457Ujr6u6vAvSDs=";
		CryptoUtils aesDecrypt = new CryptoUtils(merchantKey, "usy9Z/6QkJyQVcufN/J391ZCAv 4IJ17e8M3BTj1GYhqv964zrDXMa0TCkXHOZyJQ5FVLrK3hd6i/1EfekbZJ8 0JstgX3wMVsThsd8blIHKG0M B21jmPK5qal/g2wR0EPjPSDc9ean8at9NtBJ8qkTZmhHTEyjHAENAJAJ/uU=");
		//CryptoUtils aesDecrypt = new CryptoUtils(merchantKey, "Fz7flXHzjzHdxe5Ct6S1xQUL7Ni9XcnxarO/KljjNEwOlMcazib1oi3rUuIjoJOaWB308krkeMBG52IuD1sezqEdd/MLcWj5iuyzzNXvBlW1WoaqBGo+joZQyArAOTPBzp+miKXfYZXS1kwKfvv8ZQ==");
		
		String decryptedStr = aesDecrypt.decrypt();
		System.out.println("Decrypted String: " + decryptedStr);
	}
}