AES使用OpenSSL加密,使用C#.Net解密 [英] AES encrypt with OpenSSL, decrypt with C# .Net

查看:170
本文介绍了AES使用OpenSSL加密,使用C#.Net解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何在AES-OpenSSL中加密消息并在.NET(C#或VB)中解密,或者知道AES-OPENSSL和AES-.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

谢谢!

代码 :

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

推荐答案

在您的评论中,您要求使用C#加密和OpenSSL解密的方法.这是在C#中实现EVP_BytesToKey的良好实现.

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#.

现在,您只需要在C#中生成一个随机字节数组,然后将这两个函数(OpenSSL端的EVP和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.

请注意,您必须使用相同的哈希算法:在给定的链接中,使用了MD5.您可能必须将其更改为SHA1,具体取决于使用的是一个EVP_BytesToKey(或相反).同样,您必须根据您的需要(此处为32和32)调整帖子中给出的Derive算法的键和iv大小.

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.

希望有帮助.

我忘了.正如owlstead在他的评论中所说,Rijndael允许您使用256位的块大小.但是, AES块大小始终固定为128位,因此您的块大小必须为128位,iv为16个字节.

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.

当您希望使用盐时,也有一个陷阱. OpenSSL将为您的加密字节数组添加base64加密的"Salt__"和实际的salt数组.您可以在此帖子.

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.

OpenSSL 1.1.0c更改了某些内部组件中使用的摘要算法.以前使用的是MD5,而1.1.0切换到SHA256.请注意,更改不会同时影响EVP_BytesToKeyopenssl enc之类的命令.

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使用OpenSSL加密,使用C#.Net解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆