全方位解讀VB.NET字符串加密解密
我們?cè)谑褂?a >VB.NET進(jìn)行程序開(kāi)發(fā)的時(shí)候,不但要注意其功能的強(qiáng)大,同時(shí)也應(yīng)當(dāng)注意在程序開(kāi)發(fā)的過(guò)程中,安全性的問(wèn)題。那么接下來(lái)大家就會(huì)看到其中VB.NET字符串加密解密的一些相關(guān)技巧,幫助大家理解其安全性操作。#t#
本演練演示如何借助 3DES (TripleDES) 算法的加密服務(wù)提供程序 (CSP) 版本,使用 DESCryptoServiceProvider 類(lèi)加密和解密字符串。首先,創(chuàng)建封裝 3DES 算法的簡(jiǎn)單包裝器類(lèi),并將加密數(shù)據(jù)存儲(chǔ)為 Base-64 編碼字符串。之后,可使用該包裝器在可公開(kāi)訪問(wèn)的文本文件中安全地存儲(chǔ)私有用戶(hù)數(shù)據(jù)。
您可以使用加密來(lái)保護(hù)用戶(hù)的機(jī)密信息(如密碼),并使未經(jīng)授權(quán)的用戶(hù)無(wú)法讀取憑據(jù)。這樣可防止授權(quán)用戶(hù)的身份被盜用,從而保護(hù)用戶(hù)的資產(chǎn)并提供不可否認(rèn)性。加密還可防止未經(jīng)授權(quán)的用戶(hù)訪問(wèn)用戶(hù)數(shù)據(jù)。
VB.NET字符串加密解密的安全說(shuō)明:
與 DES 相比,Rijndael(現(xiàn)在稱(chēng)為“高級(jí)加密標(biāo)準(zhǔn)”[AES])和“三重?cái)?shù)據(jù)加密標(biāo)準(zhǔn)”(3DES) 算法提供的安全性更高,原因是破解它們所需的計(jì)算量更大。有關(guān)更多信息,請(qǐng)參見(jiàn) DES 和 Rijndael。
創(chuàng)建加密包裝器
將加密命名空間的導(dǎo)入語(yǔ)句添加到文件開(kāi)頭。
- Visual Basic
- Imports System.
Security.Cryptography
創(chuàng)建用來(lái)封裝加密和解密方法的類(lèi)。
- Visual Basic
- Public NotInheritable
Class Simple3Des- End Class
添加用來(lái)存儲(chǔ) 3DES 加密服務(wù)提供程序的私有字段。
- Visual Basic
- Private TripleDes As New
TripleDESCryptoServiceProvider
添加私有方法,該方法將從指定密鑰的哈希創(chuàng)建指定長(zhǎng)度的字節(jié)數(shù)組。
- 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
添加用來(lái)初始化 3DES 加密服務(wù)提供程序的構(gòu)造函數(shù)。
key 參數(shù)控制 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
#p#
添加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
包裝類(lèi)現(xiàn)在可用來(lái)保護(hù)用戶(hù)資產(chǎn)了。在本示例中,它用于在可公開(kāi)訪問(wèn)的文本文件中安全地存儲(chǔ)私有用戶(hù)數(shù)據(jù)。
測(cè)試VB.NET字符串加密解密包裝器
在其他類(lèi)中添加一個(gè)方法,該方法將使用包裝器的 EncryptData 方法為字符串加密,并將它寫(xiě)入用戶(hù)的“我的文檔”文件夾。
- 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
添加一個(gè)方法,該方法將從用戶(hù)的“我的文檔”文件夾讀取加密字符串,并使用包裝器的 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
添加用于調(diào)用 TestEncoding 和 TestDecoding 方法的用戶(hù)界面代碼。
運(yùn)行該應(yīng)用程序。
測(cè)試VB.NET字符串加密解密應(yīng)用程序時(shí),您將注意到:如果提供的密碼不正確,應(yīng)用程序不會(huì)解密數(shù)據(jù)。