成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

本演練演示如何借助 3DES (TripleDES) 算法的加密服務(wù)提供程序 (CSP) 版本,使用 DESCryptoServiceProvider 類加密和解密字符串。首先,創(chuàng)建封裝 3DES 算法的簡(jiǎn)單包裝器類,并將加密數(shù)據(jù)存儲(chǔ)為 Base-64 編碼字符串。之后,可使用該包裝器在可公開訪問的文本文件中安全地存儲(chǔ)私有用戶數(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)在稱為“高級(jí)加密標(biāo)準(zhǔn)”[AES])和“三重?cái)?shù)據(jù)加密標(biāo)準(zhǔn)”(3DES) 算法提供的安全性更高,原因是破解它們所需的計(jì)算量更大。有關(guān)更多信息,請(qǐng)參見 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 

 

添加用來存儲(chǔ) 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)了。在本示例中,它用于在可公開訪問的文本文件中安全地存儲(chǔ)私有用戶數(shù)據(jù)。

測(cè)試VB.NET字符串加密解密包裝器

在其他類中添加一個(gè)方法,該方法將使用包裝器的 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 

 

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

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

責(zé)任編輯:曹凱 來源: 博客園
相關(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-08 15:11:22

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

2009-11-10 12:06:17

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

2010-01-13 15:12:04

VB.NET字符串合并

2010-01-13 09:31:39

VB.NET窗體打印

2010-01-14 09:55:06

VB.NET IEnu

2010-01-05 09:57:34

.NET Framew

2010-01-18 16:58:29

VB.NET Over

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-10-27 14:32:45

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

2010-01-08 10:48:05

VB.NET多線程

2010-01-19 17:03:25

VB.NET可執(zhí)行語句

2010-01-08 10:37:50

VB.NET數(shù)據(jù)庫

2009-10-14 10:08:05

VB.NET編寫DEC

2009-10-14 09:29:43

VB.NET加密
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 综合久久综合久久 | 欧美一区二区免费电影 | 日韩成人在线视频 | 亚洲性爰 | 精品欧美一区二区三区 | 九九热在线视频免费观看 | 欧美专区在线 | 视频一区二区在线观看 | 天堂久久av| a中文在线视频 | 国产一二三区在线 | 奇色影视 | 91精品国产手机 | 国产精品久久久久久久久久三级 | a看片 | av一二三区 | 久久综合一区 | 免费福利视频一区二区三区 | 欧美精产国品一二三区 | 亚洲不卡视频 | 啪啪综合网 | 羞羞的视频免费在线观看 | 欧洲国产精品视频 | 亚洲美女av网站 | 成人做爰9片免费看网站 | 欧美激情综合网 | a a毛片 | 在线看av的网址 | 国产精品成人一区二区三区 | 久久国产区 | 日韩中文电影 | 一区二区在线免费观看视频 | 国产精品1区2区 | 国产一级电影在线观看 | 91亚洲国产成人久久精品网站 | 日韩 欧美 综合 | 欧美在线一区二区三区 | 五月激情六月婷婷 | 日韩精品在线视频 | 国产精品久久久久久久久久久免费看 | 久久免费精品 |