Archived Forum Post

Index of archived forum posts

Question:

How to replicate this openssl sequence in Chilkat 9.4.0.0

Apr 02 '14 at 11:46

We are running in .NET Framework 2 (I know, I know) using Chilkat.dll (v9.4.0.0)

We are trying to create private communications of arbitrary length using aes256 and passing random keys encrypted with a key pair.

We generate a key pair by doing:

Private key - we keep openssl genrsa -out priv.pem 2048 Public Key - we hand out to a client openssl rsa -pubout -in priv.pem -out pub.pem

Next the client generates an random key openssl rand 32 -out key.out

and encrypts it (the key.out) with their public key openssl rsautl -encrypt -pubin -inkey pub.pem -in key.out -out key.enc then we encode it in base64 openssl base64 -in key.enc -out key.b64

Let's say the private message unencrypted is "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." (without the double quotes)

and is stored in message.txt

so the client encrypts it thusly: openssl enc -aes-256-cbc -e -a -salt -in message.txt -out message.b64 -pass file:key.out

the encrypted key and the message.enc are then sent to me for decryption.

//Start of section to be automated in Chilkat

When we get the message.b64 and key.b64 in our webservice, we reverse everything

First we use out priv.pem to decode the key.b64

openssl base64 -d -in key.b64 -out key.enc openssl rsautl -inkey priv.pem -in key.enc -out key.out

then we do the same to the message.b64 openssl enc -aes-256-cbc -d -a -salt -out decrypted_data.txt -in message.b64 -pass file:key.out

This yields the same message as what was originally encoded.

//End of Chilkat automated section.

I just need to figure out how to decrypt the message.

I've written this much in C# so far...

        string encKeyb64 = [the file read into a string];
        string encPayLoadb64 = [the file read into a string];

        Chilkat.PrivateKey privkey = new Chilkat.PrivateKey();
        privkey.LoadPemFile("priv.pem");
        Chilkat.Rsa rsa = new Chilkat.Rsa();
        var success = rsa.UnlockComponent("unlock_code");
        rsa.LittleEndian = false;
        rsa.EncodingMode = "base64";
        rsa.ImportPrivateKey(privkey.GetXml());

        string aesKey = rsa.DecryptStringENC(encKeyb64 , true);

        Chilkat.Crypt2 crypto = new Chilkat.Crypt2();
        success = crypto.UnlockComponent("unlock_code");
        crypto.CryptAlgorithm = "aes";
        crypto.CipherMode = "cbc";
        crypto.KeyLength = 256;

        and then I'm stuck.

and help would be much appreciated.

Update 2014/4/2

It appears that the upper portion of the code isn't right either. I wrote the aesKey to a file via streamWriter and the size of the file differs by about 6 bytes from the one generated by openssl...