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

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

新聞中心

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

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

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

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

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

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

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

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

將加密命名空間的導入語句添加到文件開頭。

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

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

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

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

 
 
 
  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 加密服務提供程序的構造函數(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)在可用來保護用戶資產了。在本示例中,它用于在可公開訪問的文本文件中安全地存儲私有用戶數(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

添加用于調用 TestEncoding 和 TestDecoding 方法的用戶界面代碼。

運行該應用程序。

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


網(wǎng)站標題:全方位解讀VB.NET字符串加密解密
地址分享:http://m.5511xx.com/article/cosjece.html