这篇文章给大家介绍VB.NET中怎么实现字符串加密解密,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
VB.NET字符串加密解密的安全说明:
与 DES 相比,Rijndael(现在称为“高级加密标准”[AES])和“三重数据加密标准”(3DES) 算法提供的安全性更高,原因是破解它们所需的计算量更大。有关更多信息,请参见 DES 和 Rijndael。
创建加密包装器
将加密命名空间的导入语句添加到文件开头。
Visual Basic
Imports System.
Security.Cryptography
创建用来封装加密和解密方法的类。
Visual Basic
Public NotInheritable
Class Simple3Des
End Class
添加用来存储 3DES 加密服务提供程序的私有字段。
Visual Basic
Private TripleDes As New
TripleDESCryptoServiceProvider
添加私有方法,该方法将从指定密钥的哈希创建指定长度的字节数组。
Visual Basic
Private Function TruncateHash( _
ByVal key As String, _
ByVal length As Integer) _
As Byte()
Dim sha1 As New SHA1Crypto
ServiceProvider
' Hash the key.
Dim keyBytes() As Byte = _
System.Text.Encoding.Unicode.
GetBytes(key)
Dim hash() As Byte = sha1.
ComputeHash(keyBytes)
' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
End Function
添加用来初始化 3DES 加密服务提供程序的构造函数。
key 参数控制 EncryptData 和 DecryptData 方法。
Visual Basic
Sub New(ByVal key As String)
' Initialize the crypto
provider.
TripleDes.Key = TruncateHash
(key, TripleDes.KeySize \ 8)
TripleDes.IV = TruncateHash
("", TripleDes.BlockSize \ 8)
End Sub
添加VB.NET字符串加密解密之加密的公共方法。
Visual Basic
Public Function EncryptData( _
ByVal plaintext As String) _
As String
' Convert the plaintext
string to a byte array.
Dim plaintextBytes() As Byte = _
System.Text.Encoding.Unicode.
GetBytes(plaintext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the encoder to
write to the stream.
Dim encStream As New CryptoStream(ms, _
TripleDes.CreateEncryptor(), _
System.Security.Cryptography.
CryptoStreamMode.Write)
' Use the crypto stream to write
the byte array to the stream.
encStream.Write(plaintextBytes, 0,
plaintextBytes.Length)
encStream.FlushFinalBlock()
' Convert the encrypted stream
to a printable string.
Return Convert.ToBase64String
(ms.ToArray)
End Function
添加VB.NET字符串加密解密之解密的公共方法。
Visual Basic
Public Function DecryptData( _
ByVal encryptedtext As String) _
As String
' Convert the encrypted text
string to a byte array.
Dim encryptedBytes() As Byte =
Convert.FromBase64String(encryptedtext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms, _
TripleDes.CreateDecryptor(), _
System.Security.Cryptography.
CryptoStreamMode.Write)
' Use the crypto stream to write
the byte array to the stream.
decStream.Write(encryptedBytes, 0,
encryptedBytes.Length)
decStream.FlushFinalBlock()
' Convert the plaintext stream to a string.
Return System.Text.Encoding.Unicode.
GetString(ms.ToArray)
End Function
包装类现在可用来保护用户资产了。在本示例中,它用于在可公开访问的文本文件中安全地存储私有用户数据。
测试VB.NET字符串加密解密包装器
在其他类中添加一个方法,该方法将使用包装器的 EncryptData 方法为字符串加密,并将它写入用户的“我的文档”文件夹。
Visual Basic
Sub TestEncoding()
Dim plainText As String =
InputBox("Enter the plain text:")
Dim password As String =
InputBox("Enter the password:")
Dim wrapper As New Simple3Des
(password)
Dim cipherText As String =
wrapper.EncryptData(plainText)
MsgBox("The cipher text is: " &
cipherText)
My.Computer.FileSystem.WriteAllText( _
My.Computer.FileSystem.Special
Directories.MyDocuments & _
"\cipherText.txt", cipherText, False)
End Sub
添加一个方法,该方法将从用户的“我的文档”文件夹读取加密字符串,并使用包装器的 DecryptData 方法为字符串解密。
Visual Basic
Sub TestDecoding()
Dim cipherText As String =
My.Computer.FileSystem.ReadAllText( _
My.Computer.FileSystem.Special
Directories.MyDocuments & _
"\cipherText.txt")
Dim password As String =
InputBox("Enter the password:")
Dim wrapper As New Simple3Des
(password)
' DecryptData throws if the
wrong password is used.
Try
Dim plainText As String =
wrapper.DecryptData(cipherText)
MsgBox("The plain text is: "
& plainText)
Catch ex As System.Security.
Cryptography.CryptographicException
MsgBox("The data could not be
decrypted with the password.")
End Try
End Sub
添加用于调用 TestEncoding 和 TestDecoding 方法的用户界面代码。
关于VB.NET中怎么实现字符串加密解密就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。