Reposting the post to Salesforce forum from Network security forum:

I am encrypting a file with Crypto class and trying to Decrypt in Open SSL and facing issues. The same source file encrypted in OpenSSl can be decrypted in openssl easily. But not the Salesforce Encrypted file. I've compared the Salesforce encrypted file with Open SSL Encrypted File, the data matches except one difference - the OpenSSL's encrypted string is split across 100's of lines(split by new line chars) but the salesforce encrypted string is just returned as one long string (not multiple line). now this is causing incorrect decryption.

For one line file the code responds similarly. when i manually force a next line character(encryptedstring = encryptedstring + '\n') in the encrypted string, it works well. But i am not sure how to force the next line in a huge file with multiple rows

the spacing issue could be caused by any encoding that i am missing or salesforce is missing?

attachment plainatt = [select id,body,name,parentid,contenttype from 
                        attachment where id='00P2M00000dfjre'];        
blob data = plainatt.body;

blob key = Encodingutil.convertFromHex('mykeyinhexa');  
blob IV =  Encodingutil.convertFromHex('myIVinhexa');

blob encrypted = crypto.encrypt('AES128', key, IV, data);
string encryptedstring = Encodingutil.base64Encode(encrypted);        

attachment Encryptedfile = new attachment();
Encryptedfile.parentid = plainatt.parentid;
Encryptedfile.Body = blob.valueOf(encryptedstring);
Encryptedfile.name = enc2.name.replace('.txt','_Encrypted.txt');
insert Encryptedfile;

Command used in openssl:

enc -aes-128-cbc -d -a -K mykeyinhexa -iv myIVinhexa -in sf_enc.txt -out sf_dec.tx

1 Answer 1


The output from Salesforce should not have newlines. The newlines in ciphertext are treated by OpenSSL as allowed due to some base64 encoding schemes inserting them for legacy reasons. From OpenSSL encrypt command wiki:

-a, -A, -base64

By default the encoded file has a line break every 64 characters. To suppress this you can use in addition to -base64 the -A flag. This will produce a file with no line breaks at all. You can use these flags just for encoding Base64 without any ciphers involved

Therefore your command should look like this:

enc -aes-128-cbc -d -base64 -A -K mykeyinhexa -iv myIVinhexa -in sf_enc.txt -out sf_dec.tx
  • 1
    Yes thank you for the inputs. OpenSSL expects the data in blocks by default. there is command to specify if the input is in Single line buffer. -A. so the below code works in openssl to decrypt my Salesforce encrypted file. enc -aes-128-cbc -d -base64 -A -K mykeyinhexa -iv myIVinhexa -in sf_enc.txt -out sf_dec.tx
    – Bahu SF
    Commented Aug 30, 2019 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .