Public Function md5(ByVal a As String) As String
創(chuàng)新互聯(lián)公司是一家專業(yè)的成都網(wǎng)站建設公司,我們專注網(wǎng)站設計、網(wǎng)站制作、網(wǎng)絡營銷、企業(yè)網(wǎng)站建設,外鏈,一元廣告為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設計到用戶體驗提高,創(chuàng)新互聯(lián)力求做到盡善盡美。
Dim tempmd5 As System.Security.Cryptography.MD5 = New System.Security.Cryptography.MD5CryptoServiceProvider()
Dim bytResult() As Byte = tempmd5.ComputeHash(System.Text.Encoding.Default.GetBytes(a))
Dim strResult As String = BitConverter.ToString(bytResult)
strResult = strResult.Replace("-", "")
Return strResult
End Function
如果要計算文件的就把參數(shù)改成字節(jié)數(shù)組就可以了,然后獲取文件GetBytes()傳進去就可以了。
下面是完整的類,可以設置任意密碼
'DES及md5加密解密----添加引用中添加對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("你要加密的文本,可以是任意長度",?"密碼,可以很長,如果省略這個參數(shù)就是默認的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("你要解密的文本,?可以是任意長度",?"加密時用到的密碼,如果省略這個參數(shù)就是默認的12345678")
End?Sub
VB可使用DriveListBox 控件,DirListBox 控件和FileListBox 控件組合使用獲取文件夾里的文件數(shù)量(包括文件夾)。 DriveListBox 控件 在運行時,由于有 DriveListBox 控件,所以可選擇一個有效的磁盤驅(qū)動器。
這個是我之前寫的。在需要時調(diào)用即可。
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
希望能幫到你