VB.NET中的AES加密字符串

Cod*_*e92 17 vb.net aes

我有一个基于Visual Basic 2010的程序.

我想使用自定义关键字和AES加密在我们公司的网站上生成注册密钥,无论软件是否连接到互联网,都可以解锁软件.

为此,我想使用我将在我的网站上构建的实用程序将某些用户信息(和验证代码)加密为AES加密字符串.然后,我希望我的程序将字符串解密为用户信息和验证代码,然后使用该信息来验证注册密钥.

这让我想到了一个问题 - 如何以编程方式加密和解密AES中的字符串?我可以在某处使用代码模板,还是内置方法?

Sha*_*and 30

一个快速的谷歌搜索带来了以下功能:我没有测试他们产生的输出是否正确,但它们看起来似乎正确编译.

Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New System.Security.Cryptography.RijndaelManaged
        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim encrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
            Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return encrypted
        Catch ex As Exception
        End Try
    End Function

Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New System.Security.Cryptography.RijndaelManaged
        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim decrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
            Dim Buffer As Byte() = Convert.FromBase64String(input)
            decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return decrypted
        Catch ex As Exception
        End Try
    End Function
Run Code Online (Sandbox Code Playgroud)

来自:http://www.l33thackers.com/Thread-VB-NET-AES-Encryption

  • 然而,非常不安全.ECB是您可以选择的最差模式,密码到密钥算法非常弱.我当然不会将此代码用于安全性很重要的任何事情. (7认同)
  • RNCryptor为Objective-C做了这些事情,并移植到C,Java,PHP,Ruby和Python(正在进行中).我已经看过将它移植到C#,但我通常不会在VB中工作.https://github.com/rnapier/RNCryptor/wiki/Data-Format但即使没有安全的实现,您至少也不应该使用ECB模式. (2认同)

Uss*_*siq 7

编辑:感谢Artjom B.,因为我错误地假设IV字符串总是以==(在Base64中)结束而错误地通知我在解密AES-CBC时出错.感谢Kanky提出这个话题并感谢Misery此链接中提供解决方案.请注意,我自己没有检查他/她的修复,但希望它是正确的.

以下是使用AES加密字符串的两种解决方案.第一个使用CBC模式和自动生成的IV,更安全.使用具有CBC模式的IV确保即使相同的明文 - 密钥对也会产生不同的密文,从而使其更安全.第二个更多地使用ECB,不应该用于重复的明文加密.AES.Key是通过用SHA256散列输入密钥字符串生成的; 所以你不必担心密钥的伪随机性.

这是AES-CBC.IV由内置函数自动生成并与密文连接,并作为加密算法的输出返回.解密算法拆分此连接文本以恢复IV和实际密文.

Private Function AESE(ByVal plaintext As String, ByVal key As String) As String
    Dim AES As New System.Security.Cryptography.RijndaelManaged
    Dim SHA256 As New System.Security.Cryptography.SHA256Cng
    Dim ciphertext As String = ""
    Try
        AES.GenerateIV()
        AES.Key = SHA256.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))

        AES.Mode = Security.Cryptography.CipherMode.CBC
        Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
        Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(plaintext)
        ciphertext = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))

       Return Convert.ToBase64String(AES.IV) & Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))

    Catch ex As Exception
        Return ex.Message
    End Try
End Function

Private Function AESD(ByVal ciphertext As String, ByVal key As String) As String
    Dim AES As New System.Security.Cryptography.RijndaelManaged
    Dim SHA256 As New System.Security.Cryptography.SHA256Cng
    Dim plaintext As String = ""
    Dim iv As String = ""
    Try
        Dim ivct = ciphertext.Split({"=="}, StringSplitOptions.None)
        iv = ivct(0) & "=="
        ciphertext = If(ivct.Length = 3, ivct(1) & "==", ivct(1))

        AES.Key = SHA256.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
        AES.IV = Convert.FromBase64String(iv)
        AES.Mode = Security.Cryptography.CipherMode.CBC
        Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
        Dim Buffer As Byte() = Convert.FromBase64String(ciphertext)
        plaintext = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Return plaintext
    Catch ex As Exception
        Return ex.Message
    End Try
End Function
Run Code Online (Sandbox Code Playgroud)

以下是AES-ECB模式.它不使用IV.相同的明文总是产生相同的密文(当然在相同的密钥下),反之亦然.

Private Function AESE(ByVal input As Byte(), ByVal key As String) As Byte()
    Dim AES As New System.Security.Cryptography.RijndaelManaged
    Dim SHA256 As New System.Security.Cryptography.SHA256Cng
    Dim ciphertext As String = ""
    Try
        AES.Key = SHA256.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
        AES.Mode = Security.Cryptography.CipherMode.ECB
        Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
        Dim Buffer As Byte() = input
        Return DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
    Catch ex As Exception
    End Try
End Function

Private Function AESD(ByVal input As Byte(), ByVal key As String) As Byte()
    Dim AES As New System.Security.Cryptography.RijndaelManaged
    Dim SHA256 As New System.Security.Cryptography.SHA256Cng
    Try
        AES.Key = SHA256.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
        AES.Mode = Security.Cryptography.CipherMode.ECB
        Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
        Dim Buffer As Byte() = input
        Return DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
    Catch ex As Exception
    End Try
End Function
Run Code Online (Sandbox Code Playgroud)