VB.NET中的AES加密字符串

问题描述 投票:17回答:4

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

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

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

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

vb.net aes
4个回答
30
投票
快速的Google搜索会带来以下功能:我尚未测试过它们产生的输出是否正确,但是它们确实可以正确编译。

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

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

7
投票
编辑:感谢Artjom B.通知我,由于我错误地假设IV字符串始终以==结尾(在Base64中),发生了AES-CBC解密错误。感谢Kanky提出了这个话题,也感谢Miserythis link中提供了解决方案。请注意,我没有亲自检查他/她的修复程序,但希望它是正确的。

以下是使用AES加密字符串的两种解决方案。第一个使用带有自动生成的IV的CBC模式,并且更加安全。 IV与CBC模式一起使用可确保即使是相同的明文密钥对也会导致截然不同的密文,从而使其更加安全。第二个使用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

下面的这是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


0
投票
您可以使用CBC模式相对轻松地加密文件。使用uuencode / uudecode将二进制文件转换为文本表示形式并返回。在此处查看相关信息:http://www.nullskull.com/a/237/uuencode-and-uudecode-in-vbnet-and-c.aspx此处:https://social.msdn.microsoft.com/Forums/vstudio/en-US/5d4eaed8-1984-4639-aebb-bb2afddbfb8a/how-to-uuencodeuudecode-file-in-vbnet?forum=vbgeneral

0
投票
以下是AES和3DES加密的两个示例。这些使用字节数组作为输入和输出,但是您可以轻松修改以处理字符串。作为一种好习惯,您应该始终生成初始化向量(IV),并将其放在输出文件中以进行解密。这有助于保护您的KEY免受暴力攻击。加密模式最好使用CBC而不是EBC。

Imports System.Security.Cryptography Imports System.Text Public Class CAes '************************************************************************************************ 'Functions for AES Encryption '************************************************************************************************ Public Function AES_Encrypt(ByVal value As Byte(), ByVal key As String) As Byte() Dim AES As New RijndaelManaged Dim SHA256 As New SHA256Cng Dim output() As Byte AES.GenerateIV() Dim iv() As Byte = AES.IV AES.Key = SHA256.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key)) AES.Mode = CipherMode.CBC Dim AESEncrypter As ICryptoTransform = AES.CreateEncryptor Dim Buffer As Byte() = value output = AESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length) 'Copy the IV as the first 16 bytes of the output then copy encrypted bytes Dim ivAndOutput(output.Length - 1 + 16) As Byte Array.Copy(iv, ivAndOutput, 16) Array.Copy(output, 0, ivAndOutput, 16, output.Length) Return ivAndOutput End Function Public Function AES_Decrypt(ByVal value As Byte(), ByVal key As String) As Byte() Dim AES As New RijndaelManaged Dim SHA256 As New SHA256Cng Dim output() As Byte Dim iv(15) As Byte Dim Buffer(value.Length - 1 - 16) As Byte 'Extract first 16 bytes of input stream as IV. Copy remaining bytes into encrypted buffer Array.Copy(value, iv, 16) Array.Copy(value, 16, Buffer, 0, value.Length - 16) AES.Key = SHA256.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key)) AES.IV = iv AES.Mode = CipherMode.CBC Dim AESDecrypter As ICryptoTransform = AES.CreateDecryptor output = AESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length) Return output End Function End Class Public Class C3Des '************************************************************************************************ 'Functions for 3DES Encryption '************************************************************************************************ Public Function DES_Encrypt(ByVal value As Byte(), ByVal key As String) As Byte() Dim m As MD5 = New MD5CryptoServiceProvider Dim d As TripleDES = New TripleDESCryptoServiceProvider Dim encryptBytes() As Byte d.Key = m.ComputeHash(Encoding.Unicode.GetBytes(key)) d.GenerateIV() Dim c As ICryptoTransform = d.CreateEncryptor Dim input() As Byte = value encryptBytes = c.TransformFinalBlock(input, 0, input.Length) Dim outBytes(encryptBytes.Length + d.IV.Length - 1) As Byte Array.Copy(d.IV, outBytes, d.IV.Length) Array.Copy(encryptBytes, 0, outBytes, 8, encryptBytes.Length) Return outBytes End Function Public Function DES_Decrypt(ByVal value As Byte(), ByVal key As String) As Byte() Dim m As MD5 = New MD5CryptoServiceProvider Dim d As TripleDES = New TripleDESCryptoServiceProvider Dim encryptBytes(value.Length - 9), iv(7) As Byte Array.Copy(value, 0, iv, 0, 8) Array.Copy(value, 8, encryptBytes, 0, value.Length - 8) d.Key = m.ComputeHash(Encoding.Unicode.GetBytes(key)) d.IV = iv Dim b As Byte() = encryptBytes Dim c As ICryptoTransform = d.CreateDecryptor Dim output() As Byte = c.TransformFinalBlock(b, 0, b.Length) Return output End Function End Class


0
投票
非常感谢,是否有与android studio相同的用Java语言编写的代码。谢谢
© www.soinside.com 2019 - 2024. All rights reserved.