I am trying to integrate payment gateway into salesforce site, i have used the AES128 Encryption and same is use for decryption but while decryption i have used java code where i got the decrypt value as below
Output value :- "1\u000e\u0018\u001a7y&6-u:^@&/$3d87caf26f3daa75fbbef54a3ca48480676c828950be0db54JN5"
Expected Value:-
"4697a3e0a09423933d87caf26f3daa75fbbef54a3ca48480676c828950be0db54JN5"
Apex code for encryption:-
public String encrypt() {
String encryptedString = '';
Blob exampleIv = Blob.valueOf('Example of IV123');
Blob crypto128Key = Blob.valueOf('r2AAAh9Z8AXvdey9');
Blob dataToBeEncryptedAsBlob = Blob.valueOf('4697a3e0a09423933d87caf26f3daa75fbbef54a3ca48480676c828950be0db54JN5');
try {
Blob returnCipher = Crypto.encrypt('AES128', crypto128Key, exampleIv, dataToBeEncryptedAsBlob);
encryptedString = EncodingUtil.base64Encode(returnCipher);
Blob encryptedVal = EncodingUtil.base64Decode(encryptedString);
} catch (Exception e) {
System.debug('Error in encrypt -->'+e);
}
return encryptedString;
}
Java code for decryption: -
private final BASE64Encoder base64Encoder = new BASE64Encoder();
private final BASE64Decoder base64Decoder = new BASE64Decoder();
private final byte[] ivParamBytes = { 64, 64, 64, 64,
38, 38, 38, 38,
35, 35, 35, 35,
36, 36, 36, 36 };
public String decrypt(String toDecrypt, String key) throws Exception
{
String decryptedValue = "";
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING", "SunJCE");
cipher.init(2, new SecretKeySpec(key.getBytes(), "AES"), new IvParameterSpec(ivParamBytes));
decryptedValue = new String(cipher.doFinal(base64Decoder.decodeBuffer(toDecrypt)));
return decryptedValue;
}