本文介绍了用于VB.NET的AES加密的标准库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于VB.NET的AES加密的标准库?我想用一个静态私钥加密一个字符串。



我google了,发现了很多变体。我不知道如何确定哪些算法是安全的。

解决方案

系统。 Security.Cryptography 命名空间包含执行大多数标准加密任务所需的所有类。不幸的是,由于加密是一个相当复杂的课题,所以这些课程有些困难 - 特别是对于初学者来说。有时候很难找到一个简单的工作实例来开始。但是,由于我很好,我会为您提供一个简单的例子,您可以玩和改进:)



您可能想要使用的类是称为 RijndaelManaged 。那就是实现典型AES加密的类。这是一个使用它在纯文本字符串和字节数组之间转换的示例类:

 公共类Aes256Encrypter 
公共函数加密(ByVal plainText As String,ByVal secretKey As String)As Byte()
Dim encryptedPassword As Byte()
使用outputStream As MemoryStream = New MemoryStream()
Dim算法作为RijndaelManaged = getAlgorithm secretKey)
使用cryptoStream作为CryptoStream =新的CryptoStream(outputStream,algorithm.CreateEncryptor(),CryptoStreamMode.Write)
Dim inputBuffer()As Byte = Encoding.Unicode.GetBytes(plainText)
cryptoStream .Write(inputBuffer,0,inputBuffer.Length)
cryptoStream.FlushFinalBlock()
encryptedPassword = outputStream.ToArray()
结束使用
结束使用
返回encryptedPassword
结束函数

公共函数Decryp t(ByVal encryptedBytes As Byte(),ByVal secretKey As String)As String
Dim plainText As String = Nothing
使用inputStream As MemoryStream = New MemoryStream(encryptedBytes)
Dim算法作为RijndaelManaged = getAlgorithm (secretKey)
使用cryptoStream作为CryptoStream =新的CryptoStream(inputStream,algorithm.CreateDecryptor(),CryptoStreamMode.Read)
Dim outputBuffer(0 To CType(inputStream.Length - 1,Integer))As Byte
Dim readBytes As Integer = cryptoStream.Read(outputBuffer,0,CType(inputStream.Length,Integer))
plainText = Encoding.Unicode.GetString(outputBuffer,0,readBytes)
结束使用
结束使用
返回plainText
结束函数

私有函数getAlgorithm(ByVal secretKey As String)As RijndaelManaged
Const salt As String =把你的盐放在这里
Const keySize As Integer = 256

Dim keyBuilder As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(secretKey,Encoding.Unicode.GetBytes(salt))
Dim算法作为RijndaelManaged = New RijndaelManaged()
algorithm.KeySize = keySize
algorithm.IV = keyBuilder.GetBytes(CType(algorithm.BlockSize / 8,Integer))
algorithm.Key = keyBuilder.GetBytes(CType(algorithm.KeySize / 8,Integer))
algorithm.Padding = PaddingMode.PKCS7
返回算法
结束函数
结束类

您应该将 salt 常数更改为其他值。理想情况下,它不会是一个常数,因为,为了使其尽可能安全,每次执行加密时,您应该使用不同的盐,但这是另一个主题。



如果要将加密值返回为字符串而不是字节数组,则可以使用Base-64编码将字节数组转换为字符串和从字符串转换,如下所示: $加密密码As String = Nothing
使用outputStream As MemoryStream = New MemoryStream()
Dim算法作为RijndaelManaged = getAlgorithm(secretKey)
使用cryptoStream作为CryptoStream =新的CryptoStream(outputStream,algorithm.CreateEncryptor() CryptoStreamMode.Write)
Dim inputBuffer()As Byte = Encoding.Unicode.GetBytes(plainText)
cryptoStream .Write(inputBuffer,0,inputBuffer.Length)
cryptoStream.FlushFinalBlock()
encryptedPassword = Convert.ToBase64String(outputStream.ToArray())
结束使用
结束使用
返回encryptedPassword
结束函数

公共函数解密(ByVal encryptedBytes As String,ByVal secretKey As String)As String
Dim plainText As String = Nothing
使用inputStream作为MemoryStream = New MemoryStream(Convert.FromBase64String(encryptedBytes))
Dim算法作为RijndaelManaged = getAlgorithm(secretKey)
使用cryptoStream作为CryptoStream =新的CryptoStream(inputStream,algorithm.CreateDecryptor(),CryptoStreamMode.Read)
Dim outputBuffer(0 To CType(inputStream.Length - 1,Integer))As Byte
Dim readBytes As Integer = cryptoStream.Read(outputBuffer,0,CType(inputStream.Length,Integer))$ b $基点lainText = Encoding.Unicode.GetString(outputBuffer,0,readBytes)
结束使用
结束使用
返回plainText
结束函数

私有函数getAlgorithm ByVal secretKey As String)As RijndaelManaged
Const salt As String =把你的盐放在这里
Const keySize As Integer = 256

Dim keyBuilder As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(secretKey, Encoding.Unicode.GetBytes(salt))
Dim算法作为RijndaelManaged = New RijndaelManaged()
algorithm.KeySize = keySize
algorithm.IV = keyBuilder.GetBytes(CType(algorithm.BlockSize / 8 ,Integer))
algorithm.Key = keyBuilder.GetBytes(CType(algorithm.KeySize / 8,Integer))
algorithm.Padding = PaddingMode.PKCS7
返回算法
结束函数
结束类

如果您将加密值存储在文本文件,XML文件中,甚至一个数据库,通常更容易使用Base-64,像这样。


Is there a standard library to use for AES encryption for VB.NET? I want to encrypt a string with a static private key.

I googled and found a lot of variations. I don't really know how to determine which algorithms are secure or not.

解决方案

The System.Security.Cryptography namespace contains all the classes you need to perform most standard encryption tasks. Unfortunately, since encryption is a rather complicated topic, the classes are somewhat difficult to work with--especially for beginners. It's sometimes difficult to find a simple working example to start with. But, since I'm nice, I'll provide you with a simple example that you can play with and improve upon :)

The class you probably want to use is called RijndaelManaged. That is the class that implements the typical AES encryption. Here's a sample class that uses that to convert between plain text strings and byte arrays:

Public Class Aes256Encrypter
    Public Function Encrypt(ByVal plainText As String, ByVal secretKey As String) As Byte()
        Dim encryptedPassword As Byte()
        Using outputStream As MemoryStream = New MemoryStream()
            Dim algorithm As RijndaelManaged = getAlgorithm(secretKey)
            Using cryptoStream As CryptoStream = New CryptoStream(outputStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write)
                Dim inputBuffer() As Byte = Encoding.Unicode.GetBytes(plainText)
                cryptoStream.Write(inputBuffer, 0, inputBuffer.Length)
                cryptoStream.FlushFinalBlock()
                encryptedPassword = outputStream.ToArray()
            End Using
        End Using
        Return encryptedPassword
    End Function

    Public Function Decrypt(ByVal encryptedBytes As Byte(), ByVal secretKey As String) As String
        Dim plainText As String = Nothing
        Using inputStream As MemoryStream = New MemoryStream(encryptedBytes)
            Dim algorithm As RijndaelManaged = getAlgorithm(secretKey)
            Using cryptoStream As CryptoStream = New CryptoStream(inputStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read)
                Dim outputBuffer(0 To CType(inputStream.Length - 1, Integer)) As Byte
                Dim readBytes As Integer = cryptoStream.Read(outputBuffer, 0, CType(inputStream.Length, Integer))
                plainText = Encoding.Unicode.GetString(outputBuffer, 0, readBytes)
            End Using
        End Using
        Return plainText
    End Function

    Private Function getAlgorithm(ByVal secretKey As String) As RijndaelManaged
        Const salt As String = "put your salt here"
        Const keySize As Integer = 256

        Dim keyBuilder As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(secretKey, Encoding.Unicode.GetBytes(salt))
        Dim algorithm As RijndaelManaged = New RijndaelManaged()
        algorithm.KeySize = keySize
        algorithm.IV = keyBuilder.GetBytes(CType(algorithm.BlockSize / 8, Integer))
        algorithm.Key = keyBuilder.GetBytes(CType(algorithm.KeySize / 8, Integer))
        algorithm.Padding = PaddingMode.PKCS7
        Return algorithm
    End Function
End Class

You should change the salt constant to something else. Ideally, it wouldn't even be a constant, since, to make it as secure as possible, you should use a different salt each time you perform the encryption, but that's a whole other topic.

If you want to have the encrypted value returned as a string instead of as a byte array, you can use Base-64 encoding to convert the byte array to, and from, strings, like this:

Public Class Aes256Base64Encrypter
    Public Function Encrypt(ByVal plainText As String, ByVal secretKey As String) As String
        Dim encryptedPassword As String = Nothing
        Using outputStream As MemoryStream = New MemoryStream()
            Dim algorithm As RijndaelManaged = getAlgorithm(secretKey)
            Using cryptoStream As CryptoStream = New CryptoStream(outputStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write)
                Dim inputBuffer() As Byte = Encoding.Unicode.GetBytes(plainText)
                cryptoStream.Write(inputBuffer, 0, inputBuffer.Length)
                cryptoStream.FlushFinalBlock()
                encryptedPassword = Convert.ToBase64String(outputStream.ToArray())
            End Using
        End Using
        Return encryptedPassword
    End Function

    Public Function Decrypt(ByVal encryptedBytes As String, ByVal secretKey As String) As String
        Dim plainText As String = Nothing
        Using inputStream As MemoryStream = New MemoryStream(Convert.FromBase64String(encryptedBytes))
            Dim algorithm As RijndaelManaged = getAlgorithm(secretKey)
            Using cryptoStream As CryptoStream = New CryptoStream(inputStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read)
                Dim outputBuffer(0 To CType(inputStream.Length - 1, Integer)) As Byte
                Dim readBytes As Integer = cryptoStream.Read(outputBuffer, 0, CType(inputStream.Length, Integer))
                plainText = Encoding.Unicode.GetString(outputBuffer, 0, readBytes)
            End Using
        End Using
        Return plainText
    End Function

    Private Function getAlgorithm(ByVal secretKey As String) As RijndaelManaged
        Const salt As String = "put your salt here"
        Const keySize As Integer = 256

        Dim keyBuilder As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(secretKey, Encoding.Unicode.GetBytes(salt))
        Dim algorithm As RijndaelManaged = New RijndaelManaged()
        algorithm.KeySize = keySize
        algorithm.IV = keyBuilder.GetBytes(CType(algorithm.BlockSize / 8, Integer))
        algorithm.Key = keyBuilder.GetBytes(CType(algorithm.KeySize / 8, Integer))
        algorithm.Padding = PaddingMode.PKCS7
        Return algorithm
    End Function
End Class

If you are storing the encrypted value in a text file, XML file, or even a database, it's often easier to just use Base-64, like that.

这篇关于用于VB.NET的AES加密的标准库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:37