I have a AES/CBC encryption code written in C. I have to convert this code in C#
C Code:
EVP_CIPHER_CTX_init(&e_ctx);
err = EVP_EncryptInit_ex(&e_ctx, EVP_aes_256_cbc(), NULL, key, NULL);
err = EVP_CIPHER_CTX_set_padding(&e_ctx,EVP_CIPH_NO_PADDING);
err = EVP_EncryptUpdate(&e_ctx,cipher,&cipher_len,plain,plain_len);
err = EVP_EncryptFinal_ex(&e_ctx, cipher+cipher_len, &f_len);
C# code:
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
//aesAlg.IV = IV;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor();
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{ using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{ using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(data);
}
byte[] encrypted = msEncrypt.ToArray();
}
}
}
But in both the case I am getting different ciphertext. While the key was the same. While IV is not used in C code. So not using in c# code as well. But I doubt there is some default IV used by the OpenSSL and .net API.