Could you please help how to decrypt the data in java. It was encrypted by SFDC Apex (AES 256)

Blob cryptoKey = Crypto.generateAesKey(256);
Blob data = Blob.valueOf('Test data to encrypted');
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);

Please provide some code, how to decrypt using java.

I didn't understand Crypto.generateAesKey(256) how do we generate key in java to decrypt?

Thanks in Advance!!

  • To decrypt in Java you will have to export the same cryptoKey bytes used to encrypt in Apex. Code like gist.github.com/bricef/2436364 shows you what the Java should look like. But be aware that the 256 key size is not one of the "required" Ciphers for a JDK (docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html) so you may need to obtain a 3rd party security provider. Its somewhat awkward Java code to get right so if you are lucky someone may post a complete example. – Keith C Jun 10 '14 at 8:43
  • Just noticed this Sample Java class to decrypt AES128 encryption. – Keith C Jun 10 '14 at 8:47
  • Thanks a lot @KeithC... Crypto.generateAesKey(256) is this statement does random key generation? do i need to ask Apex team to export this cryptoKey? to decrypt in java or any possibility of generating same key in java itself. – Jagadeesh Jun 10 '14 at 10:52
  • You can store the blob and extract it yourself. (Or you could present the blob as base64 text in a Visualforce page and cut and paste.) Such keys have seed values and a massive amount of randomness built in; if they didn't other people could decrypt. So you will have to use the same key bytes in Java as your do in Apex. – Keith C Jun 10 '14 at 10:56
  • hi @Jagadeesh, did you find a way to get the key string from blob? – anxiousAvocado Mar 10 '17 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.