全方位解读VB.NET字符串加密解密

开发 后端
VB.NET字符串加密解密在程序开发中起了一个非常大的作用。尤其是保证代码完善,提高程序安全方面。大家可以通过我们介绍的具体方法来详细了解这其中的技巧。

我们在使用VB.NET进行程序开发的时候,不但要注意其功能的强大,同时也应当注意在程序开发的过程中,安全性的问题。那么接下来大家就会看到其中VB.NET字符串加密解密的一些相关技巧,帮助大家理解其安全性操作。#t#

本演练演示如何借助 3DES (TripleDES) 算法的加密服务提供程序 (CSP) 版本,使用 DESCryptoServiceProvider 类加密和解密字符串。首先,创建封装 3DES 算法的简单包装器类,并将加密数据存储为 Base-64 编码字符串。之后,可使用该包装器在可公开访问的文本文件中安全地存储私有用户数据。

您可以使用加密来保护用户的机密信息(如密码),并使未经授权的用户无法读取凭据。这样可防止授权用户的身份被盗用,从而保护用户的资产并提供不可否认性。加密还可防止未经授权的用户访问用户数据。

VB.NET字符串加密解密的安全说明:

与 DES 相比,Rijndael(现在称为“高级加密标准”[AES])和“三重数据加密标准”(3DES) 算法提供的安全性更高,原因是破解它们所需的计算量更大。有关更多信息,请参见 DES 和 Rijndael。

创建加密包装器

将加密命名空间的导入语句添加到文件开头。

 

  1. Visual Basic  
  2. Imports System.
    Security.Cryptography 

创建用来封装加密和解密方法的类。

 

  1. Visual Basic  
  2. Public NotInheritable 
    Class Simple3Des  
  3. End Class 

 

添加用来存储 3DES 加密服务提供程序的私有字段。

 

  1. Visual Basic  
  2. Private TripleDes As New 
    TripleDESCryptoServiceProvider 

添加私有方法,该方法将从指定密钥的哈希创建指定长度的字节数组。

 

  1. Visual Basic  
  2. Private Function TruncateHash( _  
  3. ByVal key As String, _  
  4. ByVal length As Integer) _  
  5. As Byte()  
  6. Dim sha1 As New SHA1Crypto
    ServiceProvider  
  7. ' Hash the key.  
  8. Dim keyBytes() As Byte = _ 
  9. System.Text.Encoding.Unicode.
    GetBytes(key)  
  10. Dim hash() As Byte = sha1.
    ComputeHash(keyBytes)  
  11. ' Truncate or pad the hash.  
  12. ReDim Preserve hash(length - 1)  
  13. Return hash  
  14. End Function 

 

添加用来初始化 3DES 加密服务提供程序的构造函数。

key 参数控制 EncryptData 和 DecryptData 方法。

 

  1. Visual Basic  
  2. Sub New(ByVal key As String)  
  3. ' Initialize the crypto
     provider.  
  4. TripleDes.Key = TruncateHash
    (key, TripleDes.KeySize \ 8)  
  5. TripleDes.IV = TruncateHash
    ("", TripleDes.BlockSize \ 8)  
  6. End Sub 

 

添加VB.NET字符串加密解密之加密的公共方法。

 

  1. Visual Basic  
  2. Public Function EncryptData( _  
  3. ByVal plaintext As String) _  
  4. As String  
  5. ' Convert the plaintext 
    string to a byte array.  
  6. Dim plaintextBytes() As Byte = _ 
  7. System.Text.Encoding.Unicode.
    GetBytes(plaintext)  
  8. ' Create the stream.  
  9. Dim ms As New System.IO.MemoryStream  
  10. ' Create the encoder to 
    write to the stream.  
  11. Dim encStream As New CryptoStream(ms, _  
  12. TripleDes.CreateEncryptor(), _  
  13. System.Security.Cryptography.
    CryptoStreamMode.Write)  
  14. ' Use the crypto stream to write 
    the byte array to the stream.  
  15. encStream.Write(plaintextBytes, 0, 
    plaintextBytes.Length)  
  16. encStream.FlushFinalBlock()  
  17. ' Convert the encrypted stream 
    to a printable string.  
  18. Return Convert.ToBase64String
    (ms.ToArray)  
  19. End Function 

 

#p#

添加VB.NET字符串加密解密之解密的公共方法。

 

  1. Visual Basic  
  2. Public Function DecryptData( _  
  3. ByVal encryptedtext As String) _  
  4. As String  
  5. ' Convert the encrypted text 
    string to a byte array.  
  6. Dim encryptedBytes() As Byte = 
    Convert.FromBase64String(encryptedtext)  
  7. ' Create the stream.  
  8. Dim ms As New System.IO.MemoryStream  
  9. ' Create the decoder to write to the stream.  
  10. Dim decStream As New CryptoStream(ms, _  
  11. TripleDes.CreateDecryptor(), _  
  12. System.Security.Cryptography.
    CryptoStreamMode.Write)  
  13. ' Use the crypto stream to write 
    the byte array to the stream.  
  14. decStream.Write(encryptedBytes, 0, 
    encryptedBytes.Length)  
  15. decStream.FlushFinalBlock()  
  16. ' Convert the plaintext stream to a string.  
  17. Return System.Text.Encoding.Unicode.
    GetString(ms.ToArray)  
  18. End Function 

 

包装类现在可用来保护用户资产了。在本示例中,它用于在可公开访问的文本文件中安全地存储私有用户数据。

测试VB.NET字符串加密解密包装器

在其他类中添加一个方法,该方法将使用包装器的 EncryptData 方法为字符串加密,并将它写入用户的“我的文档”文件夹。

 

  1. Visual Basic  
  2. Sub TestEncoding()  
  3. Dim plainText As String = 
    InputBox("Enter the plain text:")  
  4. Dim password As String = 
    InputBox("Enter the password:")  
  5. Dim wrapper As New Simple3Des
    (password)  
  6. Dim cipherText As String = 
    wrapper.EncryptData(plainText)  
  7. MsgBox("The cipher text is: " & 
    cipherText)  
  8. My.Computer.FileSystem.WriteAllText( _  
  9. My.Computer.FileSystem.Special
    Directories.MyDocuments & _  
  10. "\cipherText.txt", cipherText, False)  
  11. End Sub 

 

添加一个方法,该方法将从用户的“我的文档”文件夹读取加密字符串,并使用包装器的 DecryptData 方法为字符串解密。

 

  1. Visual Basic  
  2. Sub TestDecoding()  
  3. Dim cipherText As String = 
    My.Computer.FileSystem.ReadAllText( _  
  4. My.Computer.FileSystem.Special
    Directories.MyDocuments & _  
  5. "\cipherText.txt")  
  6. Dim password As String = 
    InputBox("Enter the password:")  
  7. Dim wrapper As New Simple3Des
    (password)  
  8. ' DecryptData throws if the 
    wrong password is used.  
  9. Try  
  10. Dim plainText As String = 
    wrapper.DecryptData(cipherText)  
  11. MsgBox("The plain text is: " 
    & plainText)  
  12. Catch ex As System.Security.
    Cryptography.CryptographicException  
  13. MsgBox("The data could not be
     decrypted with the password.")  
  14. End Try  
  15. End Sub 

 

添加用于调用 TestEncoding 和 TestDecoding 方法的用户界面代码。

运行该应用程序。

测试VB.NET字符串加密解密应用程序时,您将注意到:如果提供的密码不正确,应用程序不会解密数据。

责任编辑:曹凯 来源: 博客园
相关推荐

2009-10-22 11:28:35

VB.NET编码规范

2009-10-20 14:37:34

VB.NET文件操作

2009-10-26 14:06:03

2009-10-16 13:04:57

VB.NET字符串数组

2010-01-08 15:11:22

VB.NET字符串转义

2010-01-13 15:12:04

VB.NET字符串合并

2009-11-10 12:06:17

VB.NET字符串函数

2010-01-13 09:31:39

VB.NET窗体打印

2010-01-05 09:57:34

.NET Framew

2010-01-18 16:58:29

VB.NET Over

2010-01-14 09:55:06

VB.NET IEnu

2009-12-15 10:10:42

Ruby过程对象

2009-10-14 10:08:05

VB.NET编写DEC

2009-10-14 09:29:43

VB.NET加密

2010-01-18 16:33:57

VB.NET加密文件

2009-10-29 13:46:14

VB.NET DES加

2010-01-20 17:54:13

VB.NET特殊字符

2010-01-08 14:50:47

VB.NET测试硬盘速

2010-01-18 14:47:42

VB.NET获取环境变

2009-10-27 14:32:45

VB.NET类型级命名
点赞
收藏

51CTO技术栈公众号