VB.NET DEC 加解密

1.添加模塊

2.復(fù)制代碼

Imports System.Security.Cryptography

Imports System.Text

Imports System.IO

Module gFunction

? ? 'DES加密

? ? Public Function Encrypt(ByVal pToEncrypt As String, ByVal sKey As String) As String

? ? ? ? Dim des As New DESCryptoServiceProvider 'DES算法

? ? ? ? Dim inputByteArray As Byte()

? ? ? ? inputByteArray = Encoding.Default.GetBytes(pToEncrypt)

? ? ? ? des.Key = Encoding.UTF8.GetBytes(sKey) 'myKey DES用8個(gè)字符,TripleDES要24個(gè)字符

? ? ? ? des.IV = Encoding.UTF8.GetBytes(sKey)

? ? ? ? Dim ms As New MemoryStream

? ? ? ? Dim cs As New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)

? ? ? ? Dim sw As New StreamWriter(cs)

? ? ? ? sw.Write(pToEncrypt)

? ? ? ? sw.Flush()

? ? ? ? cs.FlushFinalBlock()

? ? ? ? ms.Flush()

? ? ? ? Encrypt = Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)

? ? End Function

? ? 'DES解密

? ? Public Function Decrypt(ByVal pToDecrypt As String, ByVal sKey As String) As String

? ? ? ? Dim des As New DESCryptoServiceProvider 'DES算法

? ? ? ? des.Key = Encoding.UTF8.GetBytes(sKey) 'myKey DES用8個(gè)字符,TripleDES要24個(gè)字符

? ? ? ? des.IV = Encoding.UTF8.GetBytes(sKey)

? ? ? ? Dim buffer As Byte() = Convert.FromBase64String(pToDecrypt)

? ? ? ? Dim ms As New MemoryStream(buffer)

? ? ? ? Dim cs As New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read)

? ? ? ? Dim sr As New StreamReader(cs)

? ? ? ? Decrypt = sr.ReadToEnd()

? ? End Function

End Module

3.調(diào)用

加密:Encrypt(Label1.Text, "qclghf21")

解密:Decrypt("原字符串", "12345678")

注意其中des.Key與des.IV 可以不同 我這里為了省事就直接這樣進(jìn)行了

可參考此資料????https://blog.csdn.net/lpwmm/article/details/50816153

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容