自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開(kāi)發(fā) 后端
VB.NET字符串加密解密在程序開(kāi)發(fā)中起了一個(gè)非常大的作用。尤其是保證代碼完善,提高程序安全方面。大家可以通過(guò)我們介紹的具體方法來(lái)詳細(xì)了解這其中的技巧。

我們?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)頭。

 

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

創(chuàng)建用來(lái)封裝加密和解密方法的類(lèi)。

 

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

 

添加用來(lái)存儲(chǔ) 3DES 加密服務(wù)提供程序的私有字段。

 

  1. Visual Basic  
  2. Private TripleDes As New 
    TripleDESCryptoServiceProvider 

添加私有方法,該方法將從指定密鑰的哈希創(chuàng)建指定長(zhǎng)度的字節(jié)數(shù)組。

 

  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 

 

添加用來(lái)初始化 3DES 加密服務(wù)提供程序的構(gòu)造函數(shù)。

key 參數(shù)控制 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 

 

包裝類(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ù)的“我的文檔”文件夾。

 

  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 

 

添加一個(gè)方法,該方法將從用戶(hù)的“我的文檔”文件夾讀取加密字符串,并使用包裝器的 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 

 

添加用于調(diào)用 TestEncoding 和 TestDecoding 方法的用戶(hù)界面代碼。

運(yùn)行該應(yīng)用程序。

測(cè)試VB.NET字符串加密解密應(yīng)用程序時(shí),您將注意到:如果提供的密碼不正確,應(yīng)用程序不會(huì)解密數(shù)據(jù)。

責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2009-10-22 11:28:35

VB.NET編碼規(guī)范

2009-10-20 14:37:34

VB.NET文件操作

2009-10-16 13:04:57

VB.NET字符串?dāng)?shù)組

2009-10-26 14:06:03

2010-01-13 15:12:04

VB.NET字符串合并

2009-11-10 12:06:17

VB.NET字符串函數(shù)

2010-01-08 15:11:22

VB.NET字符串轉(zhuǎn)義

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-10-14 10:08:05

VB.NET編寫(xiě)DEC

2009-10-14 09:29:43

VB.NET加密

2009-10-29 13:46:14

VB.NET DES加

2010-01-18 16:33:57

VB.NET加密文件

2010-01-20 17:54:13

VB.NET特殊字符

2009-12-15 10:10:42

Ruby過(guò)程對(duì)象

2009-10-27 14:32:45

VB.NET類(lèi)型級(jí)命名

2010-01-08 14:50:47

VB.NET測(cè)試硬盤(pán)速

2010-01-18 14:47:42

VB.NET獲取環(huán)境變
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)