用于VB.NET的AES加密的标准库?

前端之家收集整理的这篇文章主要介绍了用于VB.NET的AES加密的标准库?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有用于VB.NET的AES加密的标准库?我想用静态私钥加密字符串.

我用Google搜索并发现了很多变化.我真的不知道如何确定哪些算法是安全的.

System.Security.Cryptography命名空间包含执行大多数标准加密任务所需的所有类.不幸的是,由于加密是一个相当复杂的主题,所以这些类很难处理 – 特别是对于初学者.有时很难找到一个简单的工作示例.但是,既然我很好,我会为你提供一个简单的例子,你可以玩和改进:)

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

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,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,CType(inputStream.Length,Integer))
                plainText = Encoding.Unicode.GetString(outputBuffer,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

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

如果要将加密值作为字符串而不是字节数组返回,则可以使用Base-64编码将字节数组转换为字符串,如下所示:

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,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,Integer))
        algorithm.Padding = PaddingMode.PKCS7
        Return algorithm
    End Function
End Class

如果要将加密值存储在文本文件,XML文件甚至数据库中,那么使用Base-64通常会更容易.

原文链接:https://www.f2er.com/vb/255941.html

猜你在找的VB相关文章