I have an integration with another system which I should use the AES Algorithm.The Encryption is made in SF side - and sent in the header of an HTTP request. This made to recognize SF in internal services which won't whitelist 1M IPs. They use ECB Algorithm but I understood that SF does not support this method.
I have two methods that I can use to encrypt:
- with initialization vector (IV):
Blob cryptoKey = Blob.valueOf('1234567891234567');
String binaryString = EncodingUtil.base64Encode(cryptoKey);
System.debug(EncodingUtil.base64Encode(cryptoKey));
Blob data = Blob.valueOf('some Text to encrypt');
Blob MY_IV = Blob.valueOf('0987654321098765');
Blob encrypted = Crypto.encrypt('AES128', cryptoKey,MY_IV, data);
- with managed IV:
Blob cryptoKey = Blob.valueOf('1234567891234567');
Blob data = Blob.valueOf('some Text to encryped');
Blob encrypted = Crypto.encryptWithManagedIV('AES128', cryptoKey, data);
My questions are:
If I am using the second method with the managed IV, I get the plaintext with kind of gibberish at the beginning of the text when decrypted. Why it happens and how can I solve it?
is there another algorithm that can serve my needs instead of AES?
Is there any link or example of how can I make my own encryption?
Is there any workaround to make the ECB in apex?
many thanks.
encryptWithManagedIVon SF side, the other system will need to extract the IV from ciphertext by removing the first 16 bytes after base64 decoding the string into its binary represenation. The linked example in bullet 1 above shows how this can be done in Salesforce. – identigral Sep 05 '19 at 16:00Crypto.encrypt(...)method) - since ciphertext no longer contains the IV, you don't need to manipulate the ciphertext. The other party will decrypt it with your IV (that you'll send them) and the key. – identigral Sep 18 '19 at 14:41