日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
全方位解讀VB.NET字符串加密解密

我們在使用VB.NET進(jìn)行程序開發(fā)的時候,不但要注意其功能的強(qiáng)大,同時也應(yīng)當(dāng)注意在程序開發(fā)的過程中,安全性的問題。那么接下來大家就會看到其中VB.NET字符串加密解密的一些相關(guān)技巧,幫助大家理解其安全性操作。#t#

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、涇源網(wǎng)站維護(hù)、網(wǎng)站推廣。

本演練演示如何借助 3DES (TripleDES) 算法的加密服務(wù)提供程序 (CSP) 版本,使用 DESCryptoServiceProvider 類加密和解密字符串。首先,創(chuàng)建封裝 3DES 算法的簡單包裝器類,并將加密數(shù)據(jù)存儲為 Base-64 編碼字符串。之后,可使用該包裝器在可公開訪問的文本文件中安全地存儲私有用戶數(shù)據(jù)。

您可以使用加密來保護(hù)用戶的機(jī)密信息(如密碼),并使未經(jīng)授權(quán)的用戶無法讀取憑據(jù)。這樣可防止授權(quán)用戶的身份被盜用,從而保護(hù)用戶的資產(chǎn)并提供不可否認(rèn)性。加密還可防止未經(jīng)授權(quán)的用戶訪問用戶數(shù)據(jù)。

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

與 DES 相比,Rijndael(現(xiàn)在稱為“高級加密標(biāo)準(zhǔn)”[AES])和“三重數(shù)據(jù)加密標(biāo)準(zhǔn)”(3DES) 算法提供的安全性更高,原因是破解它們所需的計算量更大。有關(guān)更多信息,請參見 DES 和 Rijndael。

創(chuàng)建加密包裝器

將加密命名空間的導(dǎo)入語句添加到文件開頭。

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

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

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

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

 
 
 
  1. Visual Basic
  2. Private TripleDes As New 
    TripleDESCryptoServiceProvider

添加私有方法,該方法將從指定密鑰的哈希創(chuà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

添加用來初始化 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

包裝類現(xiàn)在可用來保護(hù)用戶資產(chǎn)了。在本示例中,它用于在可公開訪問的文本文件中安全地存儲私有用戶數(shù)據(jù)。

測試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

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

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

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


文章名稱:全方位解讀VB.NET字符串加密解密
轉(zhuǎn)載源于:http://www.5511xx.com/article/cosjece.html