网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

VB.NET中怎么实现字符串加密解密

这篇文章给大家介绍VB.NET中怎么实现字符串加密解密,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联于2013年成立,先为海城等服务建站,海城等地企业,进行企业商务咨询服务。为海城企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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 

添加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中怎么实现字符串加密解密就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


标题名称:VB.NET中怎么实现字符串加密解密
文章源于:http://bjjierui.cn/article/pggppi.html

其他资讯