简体   繁体   中英

AES encrypt with OpenSSL, decrypt with C# .Net

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!

CODE in VB.NET :

Public Function AES_Decrypt(ByVal prm_key As String, ByVal prm_iv As String, ByVal prm_text_to_decrypt As String)

    Dim sEncryptedString As String = prm_text_to_decrypt

    Dim myRijndael As New RijndaelManaged
    myRijndael.Padding = PaddingMode.Zeros
    myRijndael.Mode = CipherMode.CBC
    myRijndael.KeySize = 256
    myRijndael.BlockSize = 256

    Dim key() As Byte
    Dim IV() As Byte

    key = System.Text.Encoding.ASCII.GetBytes(prm_key)
    IV = System.Text.Encoding.ASCII.GetBytes(prm_iv)

    Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)

    Dim sEncrypted As Byte() = Convert.FromBase64String(sEncryptedString)

    Dim fromEncrypt() As Byte = New Byte(sEncrypted.Length) {}

    Dim msDecrypt As New MemoryStream(sEncrypted)
    Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

    csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

    Return (System.Text.Encoding.ASCII.GetString(fromEncrypt))

End Function

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 .

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM