Recently, I had to deal with some encryption, for which the documentation included some piece of code in VB, which used DESCryptoServiceProvider class for encryption. After spending almost a day trying to understand the implementation, I have come up with the equivalent implementation in C with openssl.
The stupidest thing, which I had to scratch my head for majority of the time spent on was the problem that, why is the VB code generating a 16-byte encrypted data for an input of length 8-bytes. I found the answer to this here.
Until now, I had been using the DES_* functions. After realizing that 8-byte padding is required, I had to use low-level EVP_* functions.
Some piece of code is given here:
EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); EVP_EncryptInit_ex(&ctx, EVP_des_cbc(), NULL, key, iv); EVP_CIPHER_CTX_set_padding(&ctx, 8); //output_length and output_length1 need to be different. EVP_EncryptUpdate(&ctx, output, &output_length, input, input_length); EVP_EncryptFinal_ex(&ctx, output+output_length, &output_length1); output_length += output_length1;
Phew...!!