這個(gè)是我之前寫(xiě)的。在需要時(shí)調(diào)用即可。
創(chuàng)新互聯(lián)建站專注于企業(yè)成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、大箐山網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、商城網(wǎng)站定制開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為大箐山等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
Public Shared Function Encrypt(ByVal Text As String, ByVal sKey As String) As String
Dim provider As New DESCryptoServiceProvider()
Dim bytes As Byte() = Encoding.[Default].GetBytes(Text)
provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
Dim stream As New MemoryStream()
Dim stream2 As New CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write)
stream2.Write(bytes, 0, bytes.Length)
stream2.FlushFinalBlock()
Dim builder As New StringBuilder()
For Each num As Byte In stream.ToArray()
builder.AppendFormat("{0:X2}", num)
Next
Return builder.ToString()
End Function
希望能幫到你
下面是完整的類,可以設(shè)置任意密碼
'DES及md5加密解密----添加引用中添加對(duì)system.web的引用。
Imports?System.Security.Cryptography
Imports?System
Imports?System.Text
Imports?System.Web
'''?summary
'''?DES加密類
'''?/summary
'''?remarks/remarks
Public?Class?DESEncrypt
Public?Sub?DESEncrypt()
End?悶帶Sub
Public?Shared?Function?Encrypt(ByVal?Text?As?String)?As?String
Return?Encrypt(Text,?"12345678")
End?Function
Public?Shared?Function?Encrypt(ByVal?Text?As?String,?ByVal?sKey?As?String)?As?String
Dim?des?As?New?DESCryptoServiceProvider()
Dim?inputByteArray?As?Byte()
inputByteArray?=?Encoding.Default.GetBytes(Text)
des.Key?=?ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,?"md5").Substring(0,?8))
des.IV?=?ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,?"md5").Substring(0,?8))
Dim?ms?As?New?System.IO.MemoryStream()
Dim?cs?As?New?CryptoStream(ms,?des.CreateEncryptor(),?CryptoStreamMode.Write)
cs.Write(inputByteArray,?0,?inputByteArray.Length)
cs.FlushFinalBlock()
Dim?ret?As?New?StringBuilder()
Dim?b?As?Byte
For?Each?b?In?ms.ToArray()
ret.AppendFormat("{0:X2}",?b)
斗罩搜????Next
Return?ret.ToString()
End?Function
Public?Shared?Function?Decrypt(ByVal?Text?As?String)?As?String
Return?Decrypt(Text,?"12345678")
End?Function
Public?Shared?Function?Decrypt(ByVal?Text?As?String,?ByVal?sKey?As?空歷String)?As?String
Dim?des?As?New?DESCryptoServiceProvider()
Dim?len?As?Integer
len?=?Text.Length?/?2
Dim?inputByteArray(len?-?1)?As?Byte
Dim?x,?i?As?Integer
For?x?=?0?To?len?-?1
i?=?Convert.ToInt32(Text.Substring(x?*?2,?2),?16)
inputByteArray(x)?=?CType(i,?Byte)
Next
des.Key?=?ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,?"md5").Substring(0,?8))
des.IV?=?ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,?"md5").Substring(0,?8))
Dim?ms?As?New?System.IO.MemoryStream()
Dim?cs?As?New?CryptoStream(ms,?des.CreateDecryptor(),?CryptoStreamMode.Write)
cs.Write(inputByteArray,?0,?inputByteArray.Length)
cs.FlushFinalBlock()
Return?Encoding.Default.GetString(ms.ToArray())
End?Function
End?Class
'以下是調(diào)用方法
Public?Class?Form1
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click?'加密
Dim?str_Encrypt?As?String?=?DESEncrypt.Encrypt("你要加密的文本,可以是任意長(zhǎng)度",?"密碼,可以很長(zhǎng),如果省略這個(gè)參數(shù)就是默認(rèn)的12345678")
End?Sub
Private?Sub?Button2_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button2.Click?'解密
Dim?str_Decrypt?As?String?=?DESEncrypt.Decrypt("你要解密的文本,?可以是任意長(zhǎng)度",?"加密時(shí)用到的密碼,如果省略這個(gè)參數(shù)就是默認(rèn)的12345678")
End?Sub
using System;\x0d\x0ausing System.Text;\x0d\x0ausing System.Security.Cryptography;\x0d\x0a\x0d\x0anamespace MD5\x0d\x0a{\x0d\x0a public class MD5\x0d\x0a {\x0d\x0a // 32 位\x0d\x0a public static String Encrypt(String s)\x0d\x0a {\x0d\x0a MD5 md5 = new MD5CryptoServiceProvider();\x0d\x0a byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);\x0d\x0a bytes = md5.ComputeHash(bytes);\x0d\x0a md5.Clear();\x0d\x0a\x0d\x0a string ret = "";\x0d\x0a for (int i = 0; i
回答于?2022-12-11