AES encrypt with OpenSSL, decrypt with C# .Net

后端 未结 2 855
小蘑菇
小蘑菇 2020-12-22 00:01

I need to know how to encrypt a message in AES-OpenSSL and decrypt in .NET (C# or VB) OR Know what is the difference between AES-OPENSSL and AES-.NET

Thank you!

相关标签:
2条回答
  • 2020-12-22 00:25

    AES is AES. There are NIST test vectors that make sure that implementations are compatible, and the byte order has been specified as well. So it comes down to choosing the correct mode (e.g. CBC or the authenticated GCM mode) and padding mode (PKCS#7 for CBC and "none" for GCM). Choose the correct key and IV and you are on your way. Take extra care to understand the input of AES, especially make sure you understand encoding and character-encoding and random number generation.

    0 讨论(0)
  • 2020-12-22 00:37

    In your comment, you ask for a way to encrypt in C# and Decrypt in OpenSSL. Here's a good implementation of EVP_BytesToKey in C#.

    Now you just have to generate a random byte array in C#, then use these functions (EVP on OpenSSL side and the second one in C#) on both sides with your common random byte array.

    Beware though, you have to use the same hash algorithm: in the given link, MD5 is used. You might have to change it to SHA1 depending on the one EVP_BytesToKey is using (or the other way round). The same way, you have to adapt the key and iv size in the Derive algorithm given in the post depending on your needs, here 32 and 32.

    Hope that helped.

    EDIT 1: I forgot. As owlstead said in his comment, Rijndael allows you to use a block size of 256 bits. However, AES block size is always fixed to 128 bits, so your block size MUST be 128 bits and your iv 16 bytes.

    There is also a catch when you wish to use salt. OpenSSL prepends your encrypted byte array with a base64 encryption of "Salt__" and the actual salt array. You can find an example in this post.

    EDIT 2: OpenSSL 1.1.0c changed the digest algorithm used in some internal components. Formerly, MD5 was used, and 1.1.0 switched to SHA256. Be careful the change is not affecting you in both EVP_BytesToKey and commands like openssl enc.

    0 讨论(0)
提交回复
热议问题