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