You need to sign in to do that
Don't have an account?

Decrypting the base64Encoded data which is encrpyted by Java (AES256)
Hi,
First of all thanks for the time to see my question.
I have a encrypted data that comes from Java webservice(AES256-base64Encoded) :
C5j45mSEdqfz4f4GnrwR2T25tizY5gb3JoVNtWp8DiYOW5d+qvPx/ODmQIQqNXpZ1YkIjLwu5BAjDh3QmlrC4DKwTvbSrVqxh7OJdGDQy60ebp0o1cmLBCJuqzAOh2KAZC6L9v4wkwEb9dFOPesImY15DySPeyYiuLHL7n2dHmyVUMcDUrp9TAm85daCyG5mKhaAy2SP8iNBPoUBWSIDDEM7vG5Ganxu6JVzG4V6zGYQlLXAi33Ct9Fs99YE02KKR4kiw52eE1nS4hwReMZnP4vuU1FxM0BZfWAdUV8m9pQ=
Now I also have a 32 bit key to decode it (base64Encoded):
v3erqY/EGg1NI0yFHYLsMt7IuqJksxNjo1dlhsOo6Zs=
So after decryption, I should get the following data below:
http://b2bfsta.edc.cingular.net:8180/b2bservlets/TCMLoginDispatch.dyn?loginChannel=EBM&uid=B2c18e47fbc6e94ec7e5a92dece2c2684a4e3421a&sourceSystemId=iPhone6-NewOrder&profileType=PLATINUM&peDealId=o31634600562&enableDDR=true
I know if I encrypt in Apex then I can use decrypt(String, Blob, Blob, Blob) or decryptWithManagedIV(String, Blob, Blob).
But in this case, I don't why I am getting error, I tried all options available . PLease find sample code:
--------------------------------------------------------------------------------------------------------------------------------------------------------
Blob key = EncodingUtil.base64Decode('v3erqY/EGg1NI0yFHYLsMt7IuqJksxNjo1dlhsOo6Zs=');
String encodedData ='C5j45mSEdqfz4f4GnrwR2T25tizY5gb3JoVNtWp8DiYOW5d+qvPx/ODmQIQqNXpZ1YkIjLwu5BAjDh3QmlrC4DKwTvbSrVqxh7OJdGDQy60ebp0o1cmLBCJuqzAOh2KAZC6L9v4wkwEb9dFOPesImY15DySPeyYiuLHL7n2dHmyVUMcDUrp9TAm85daCyG5mKhaAy2SP8iNBPoUBWSIDDEM7vG5Ganxu6JVzG4V6zGYQlLXAi33Ct9Fs99YE02KKR4kiw52eE1nS4hwReMZnP4vuU1FxM0BZfWAdUV8m9pQ=';
Blob exampleIv = Blob.valueOf('Example of IV123');
Blob decryptedData = Crypto.decrypt('AES256', key ,exampleIv, EncodingUtil.base64Decode(encodedData));
//Blob decryptedData = Crypto.decryptWithManagedIV('AES256', key, EncodingUtil.base64Decode(encodedData));
system.debug(decryptedData );
--------------------------------------------------------------------------------------------------------------------------------------------------------
below is the java encrypted code, that can be refer:
public class EncryptionTest {
public static final String CHAR_SET = "UTF8";
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Hello World");
String key = "v3erqY/EGg1NI0yFHYLsMt7IuqJksxNjo1dlhsOo6Zs=";
// Try the below as well
byte [] mKeyBytes = Base64.decodeBase64(key);
//byte [] mKeyBytes = decodeToByteArray(key.getBytes());
javax.crypto.Cipher mDecryptCipher = null;
javax.crypto.Cipher mEncryptCipher = null;
//byte[] mKeyBytes;
try {
// Create the SecretKeySpec using the key in mKeyBytes
final SecretKeySpec skeySpec = new SecretKeySpec(mKeyBytes, getAlgorithmName());
// Create a new Cipher instance to do decryption using ALGORITHM
final javax.crypto.Cipher decryptCipher = javax.crypto.Cipher.getInstance(getAlgorithmName());
// Initialize this Cipher as a decryption cipher and pass in the key
// spec to use
decryptCipher.init(javax.crypto.Cipher.DECRYPT_MODE, skeySpec);
// If the previous two steps succeeded, set this new decryption
// Cipher to be this class's member decryption Cipher
mDecryptCipher = decryptCipher;
// Create a new Cipher instance to do encryption using ALGORITHM
final javax.crypto.Cipher encryptCipher = javax.crypto.Cipher.getInstance(getAlgorithmName());
// Initialize this Cipher as an encryption cipher and pass in the
// key spec to us
encryptCipher.init(javax.crypto.Cipher.ENCRYPT_MODE, skeySpec);
// If the previous two steps succeeded, set this new encryption
// Cipher to be this class's member encryption Cipher
mEncryptCipher = encryptCipher;
} catch (InvalidKeyException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NoSuchPaddingException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
String pStringToDecrypt = "C5j45mSEdqfz4f4GnrwR2T25tizY5gb3JoVNtWp8DiYOW5d+qvPx/ODmQIQqNXpZ1YkIjLwu5BAjDh3QmlrC4DKwTvbSrVqxh7OJdGDQy60ebp0o1cmLBCJuqzAOh2KAZC6L9v4wkwEb9dFOPesImY15DySPeyYiuLHL7n2dHmyVUMcDUrp9TAm85daCyG5mKhaAy2SP8iNBPoUBWSIDDEM7vG5Ganxu6JVzG4V6zGYQlLXAi33Ct9Fs99YE02KKR4kiw52eE1nS4hwReMZnP4vuU1FxM0BZfWAdUV8m9pQ=";
//System.out.println("Hello");
String decryptedString;
final byte[] encryptedBytes;
final byte[] decryptedBytes;
try {
encryptedBytes = Base64.decodeBase64(pStringToDecrypt.getBytes(CHAR_SET));
decryptedBytes = decrypt(mDecryptCipher, encryptedBytes);
decryptedString = new String(decryptedBytes, CHAR_SET);
System.out.println(decryptedString);
} catch (UnsupportedEncodingException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
protected static final byte[] decrypt(javax.crypto.Cipher decryptCipher, byte[] pBytesToDecrypt) throws Exception {
try {
return decryptCipher.doFinal(pBytesToDecrypt);
} catch (IllegalBlockSizeException ibse) {
ibse.printStackTrace();
throw new Exception("Failed to decrypt: ", ibse);
} catch (BadPaddingException bpe) {
bpe.printStackTrace();
throw new Exception("Failed to decrypt: ", bpe);
}
}
public static String getAlgorithmName() {
return "AES";
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
The problem is that the encrypted code is not getting decrypted in apex using standard way . Please help me out.
First of all thanks for the time to see my question.
I have a encrypted data that comes from Java webservice(AES256-base64Encoded) :
C5j45mSEdqfz4f4GnrwR2T25tizY5gb3JoVNtWp8DiYOW5d+qvPx/ODmQIQqNXpZ1YkIjLwu5BAjDh3QmlrC4DKwTvbSrVqxh7OJdGDQy60ebp0o1cmLBCJuqzAOh2KAZC6L9v4wkwEb9dFOPesImY15DySPeyYiuLHL7n2dHmyVUMcDUrp9TAm85daCyG5mKhaAy2SP8iNBPoUBWSIDDEM7vG5Ganxu6JVzG4V6zGYQlLXAi33Ct9Fs99YE02KKR4kiw52eE1nS4hwReMZnP4vuU1FxM0BZfWAdUV8m9pQ=
Now I also have a 32 bit key to decode it (base64Encoded):
v3erqY/EGg1NI0yFHYLsMt7IuqJksxNjo1dlhsOo6Zs=
So after decryption, I should get the following data below:
http://b2bfsta.edc.cingular.net:8180/b2bservlets/TCMLoginDispatch.dyn?loginChannel=EBM&uid=B2c18e47fbc6e94ec7e5a92dece2c2684a4e3421a&sourceSystemId=iPhone6-NewOrder&profileType=PLATINUM&peDealId=o31634600562&enableDDR=true
I know if I encrypt in Apex then I can use decrypt(String, Blob, Blob, Blob) or decryptWithManagedIV(String, Blob, Blob).
But in this case, I don't why I am getting error, I tried all options available . PLease find sample code:
--------------------------------------------------------------------------------------------------------------------------------------------------------
Blob key = EncodingUtil.base64Decode('v3erqY/EGg1NI0yFHYLsMt7IuqJksxNjo1dlhsOo6Zs=');
String encodedData ='C5j45mSEdqfz4f4GnrwR2T25tizY5gb3JoVNtWp8DiYOW5d+qvPx/ODmQIQqNXpZ1YkIjLwu5BAjDh3QmlrC4DKwTvbSrVqxh7OJdGDQy60ebp0o1cmLBCJuqzAOh2KAZC6L9v4wkwEb9dFOPesImY15DySPeyYiuLHL7n2dHmyVUMcDUrp9TAm85daCyG5mKhaAy2SP8iNBPoUBWSIDDEM7vG5Ganxu6JVzG4V6zGYQlLXAi33Ct9Fs99YE02KKR4kiw52eE1nS4hwReMZnP4vuU1FxM0BZfWAdUV8m9pQ=';
Blob exampleIv = Blob.valueOf('Example of IV123');
Blob decryptedData = Crypto.decrypt('AES256', key ,exampleIv, EncodingUtil.base64Decode(encodedData));
//Blob decryptedData = Crypto.decryptWithManagedIV('AES256', key, EncodingUtil.base64Decode(encodedData));
system.debug(decryptedData );
--------------------------------------------------------------------------------------------------------------------------------------------------------
below is the java encrypted code, that can be refer:
public class EncryptionTest {
public static final String CHAR_SET = "UTF8";
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Hello World");
String key = "v3erqY/EGg1NI0yFHYLsMt7IuqJksxNjo1dlhsOo6Zs=";
// Try the below as well
byte [] mKeyBytes = Base64.decodeBase64(key);
//byte [] mKeyBytes = decodeToByteArray(key.getBytes());
javax.crypto.Cipher mDecryptCipher = null;
javax.crypto.Cipher mEncryptCipher = null;
//byte[] mKeyBytes;
try {
// Create the SecretKeySpec using the key in mKeyBytes
final SecretKeySpec skeySpec = new SecretKeySpec(mKeyBytes, getAlgorithmName());
// Create a new Cipher instance to do decryption using ALGORITHM
final javax.crypto.Cipher decryptCipher = javax.crypto.Cipher.getInstance(getAlgorithmName());
// Initialize this Cipher as a decryption cipher and pass in the key
// spec to use
decryptCipher.init(javax.crypto.Cipher.DECRYPT_MODE, skeySpec);
// If the previous two steps succeeded, set this new decryption
// Cipher to be this class's member decryption Cipher
mDecryptCipher = decryptCipher;
// Create a new Cipher instance to do encryption using ALGORITHM
final javax.crypto.Cipher encryptCipher = javax.crypto.Cipher.getInstance(getAlgorithmName());
// Initialize this Cipher as an encryption cipher and pass in the
// key spec to us
encryptCipher.init(javax.crypto.Cipher.ENCRYPT_MODE, skeySpec);
// If the previous two steps succeeded, set this new encryption
// Cipher to be this class's member encryption Cipher
mEncryptCipher = encryptCipher;
} catch (InvalidKeyException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NoSuchPaddingException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
String pStringToDecrypt = "C5j45mSEdqfz4f4GnrwR2T25tizY5gb3JoVNtWp8DiYOW5d+qvPx/ODmQIQqNXpZ1YkIjLwu5BAjDh3QmlrC4DKwTvbSrVqxh7OJdGDQy60ebp0o1cmLBCJuqzAOh2KAZC6L9v4wkwEb9dFOPesImY15DySPeyYiuLHL7n2dHmyVUMcDUrp9TAm85daCyG5mKhaAy2SP8iNBPoUBWSIDDEM7vG5Ganxu6JVzG4V6zGYQlLXAi33Ct9Fs99YE02KKR4kiw52eE1nS4hwReMZnP4vuU1FxM0BZfWAdUV8m9pQ=";
//System.out.println("Hello");
String decryptedString;
final byte[] encryptedBytes;
final byte[] decryptedBytes;
try {
encryptedBytes = Base64.decodeBase64(pStringToDecrypt.getBytes(CHAR_SET));
decryptedBytes = decrypt(mDecryptCipher, encryptedBytes);
decryptedString = new String(decryptedBytes, CHAR_SET);
System.out.println(decryptedString);
} catch (UnsupportedEncodingException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
protected static final byte[] decrypt(javax.crypto.Cipher decryptCipher, byte[] pBytesToDecrypt) throws Exception {
try {
return decryptCipher.doFinal(pBytesToDecrypt);
} catch (IllegalBlockSizeException ibse) {
ibse.printStackTrace();
throw new Exception("Failed to decrypt: ", ibse);
} catch (BadPaddingException bpe) {
bpe.printStackTrace();
throw new Exception("Failed to decrypt: ", bpe);
}
}
public static String getAlgorithmName() {
return "AES";
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
The problem is that the encrypted code is not getting decrypted in apex using standard way . Please help me out.
Thanks for the suggestion. But it is not working for my inputs.
This is opposite of what you want to do but you will atleast get idea on how it works in both environments for encode and decode.
I appreciate your effort, however we don't have MCRYPT function (or any default c variables) in apex. The situation is lots different here.
I am facing similar issue. can you please help me with solution?
Thanks & Regards,
Siva.