本演练演示如何借助 3DES (TripleDES) 算法的加密服务提供程序 (CSP) 版本,使用 DESCryptoServiceProvider 类加密和解密字符串。首先,创建封装 3DES 算法的简单包装器类,并将加密数据存储为 Base-64 编码字符串。之后,可使用该包装器在可公开访问的文本文件中安全地存储私有用户数据。
您可以使用加密来保护用户的机密信息(如密码),并使未经授权的用户无法读取凭据。这样可防止授权用户的身份被盗用,从而保护用户的资产并提供不可否认性。加密还可防止未经授权的用户访问用户数据。
有关更多信息,请参见加密概述。
|
安全注意
|
|
与 DES 相比,Rijndael(现在称为“高级加密标准”[AES])和“三重数据加密标准”(3DES) 算法提供的安全性更高,原因是破解它们所需的计算量更大。有关更多信息,请参见 DES 和 Rijndael。
|
创建加密包装器
1. 将加密命名空间的导入语句添加到文件开头。
|
Visual Basic
|
复制代码
|
|
Imports System.Security.Cryptography
|
2. 创建用来封装加密和解密方法的类。
|
Visual Basic
|
复制代码
|
|
Public NotInheritable Class Simple3Des
End Class
|
3. 添加用来存储 3DES 加密服务提供程序的私有字段。
|
Visual Basic
|
复制代码
|
|
Private TripleDes As New TripleDESCryptoServiceProvider
|
4. 添加私有方法,该方法将从指定密钥的哈希创建指定长度的字节数组。
|
Visual Basic
|
复制代码
|
|
Private Function TruncateHash( _
ByVal key As String, _
ByVal length As Integer) _
As Byte()
Dim sha1 As New SHA1CryptoServiceProvider
' 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
|
5. 添加用来初始化 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
|
6. 添加加密字符串的公共方法。
|
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
|
7. 添加解密字符串的公共方法。
|
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
|
8. 包装类现在可用来保护用户资产了。在本示例中,它用于在可公开访问的文本文件中安全地存储私有用户数据。
测试加密包装器
1. 在其他类中添加一个方法,该方法将使用包装器的 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.SpecialDirectories.MyDocuments & _
"\cipherText.txt", cipherText, False)
End Sub
|
2. 添加一个方法,该方法将从用户的“我的文档”文件夹读取加密字符串,并使用包装器的 DecryptData 方法为字符串解密。
|
Visual Basic
|
复制代码
|
|
Sub TestDecoding()
Dim cipherText As String = My.Computer.FileSystem.ReadAllText( _
My.Computer.FileSystem.SpecialDirectories.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
|
3. 添加用于调用 TestEncoding 和 TestDecoding 方法的用户界面代码。
4. 运行该应用程序。
测试应用程序时,您将注意到:如果提供的密码不正确,应用程序不会解密数据。
注:上述程序可以用来加密程序的敏感数据,然后写入程序数据库中。如用户密码等
posted @ 2006-10-02 09:19
天使爱比目鱼 阅读(493)
评论(0) 编辑 收藏 网摘 所属分类:
学习资料