网站首页 > 文章中心 > 其它

vb.netmd5函数

作者:小编 更新时间:2023-08-29 22:29:20 浏览量:201人看过

VB.NET 获取文件MD5值

Dim strResult As String = BitConverter.ToString(bytResult)

strResult = strResult.Replace("-", "")

Return strResult

End Function

如果要计算文件的就把参数改成字节数组就可以了,然后获取文件GetBytes()传进去就可以了.

用VB实现MD5加密

vb.netmd5函数-图1

Private?Function?LShift(lValue,?iShiftBits)

If?iShiftBits?=?0?Then

LShift?=?lValue

Exit?Function

If?lValue?And?1?Then

Else

LShift?=?0

End?If

End?Function

Private?Function?RShift(lValue,?iShiftBits)

RShift?=?lValue

RShift?=?1

RShift?=?0

Private?Function?RotateLeft(lValue,?iShiftBits)

Private?Function?AddUnsigned(lX,?lY)

Dim?lResult

AddUnsigned?=?lResult

a?=?RotateLeft(a,?s)

a?=?AddUnsigned(a,?b)

End?Sub

Private?Function?ConvertToWordArray(sMessage)

Dim?lMessageLength

Dim?lNumberOfWords

Dim?lWordArray()

Dim?lBytePosition

Dim?lByteCount

Dim?lWordCount

lMessageLength?=?Len(sMessage)

lNumberOfWords?=?(((lMessageLength?+?((MODULUS_BITS?-?CONGRUENT_BITS)?\?BITS_TO_A_BYTE))?\?(MODULUS_BITS?\?BITS_TO_A_BYTE))?+?1)?*?(MODULUS_BITS?\?BITS_TO_A_WORD)

ReDim?lWordArray(lNumberOfWords?-?1)

lBytePosition?=?0

lByteCount?=?0

Do?Until?lByteCount?=?lMessageLength

lWordCount?=?lByteCount?\?BYTES_TO_A_WORD

lBytePosition?=?(lByteCount?Mod?BYTES_TO_A_WORD)?*?BITS_TO_A_BYTE

lWordArray(lWordCount)?=?lWordArray(lWordCount)?Or?LShift(Asc(Mid(sMessage,?lByteCount?+?1,?1)),?lBytePosition)

lByteCount?=?lByteCount?+?1

Loop

ConvertToWordArray?=?lWordArray

Private?Function?WordToHex(lValue)

Dim?lByte

Dim?lCount

lByte?=?RShift(lValue,?lCount?*?BITS_TO_A_BYTE)?And?m_lOnBits(BITS_TO_A_BYTE?-?1)

Next

m_lOnBits(0)?=?CLng(1)

Dim?x

Dim?k

Dim?AA

Dim?BB

Dim?CC

Dim?DD

Dim?a

Dim?b

Dim?c

Dim?d

x?=?ConvertToWordArray(sMessage)

AA?=?a

BB?=?b

CC?=?c

DD?=?d

a?=?AddUnsigned(a,?AA)

b?=?AddUnsigned(b,?BB)

c?=?AddUnsigned(c,?CC)

d?=?AddUnsigned(d,?DD)

'下面是测试代码

Sub?test()

求VB.NET的MD5算法调用

下面是完整的类,可以设置任意密码

Imports?System.Security.Cryptography

Imports?System

Imports?System.Text

Imports?System.Web

'''?summary

'''?DES加密类

'''?/summary

'''?remarks/remarks

Public?Class?DESEncrypt

Public?Sub?DESEncrypt()

Public?Shared?Function?Encrypt(ByVal?Text?As?String)?As?String

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)

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()

Return?ret.ToString()

Public?Shared?Function?Decrypt(ByVal?Text?As?String)?As?String

Public?Shared?Function?Decrypt(ByVal?Text?As?String,?ByVal?sKey?As?String)?As?String

Dim?len?As?Integer

Dim?inputByteArray(len?-?1)?As?Byte

Dim?x,?i?As?Integer

For?x?=?0?To?len?-?1

inputByteArray(x)?=?CType(i,?Byte)

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

Return?Encoding.Default.GetString(ms.ToArray())

End?Class

'以下是调用方法

Public?Class?Form1

Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click?'加密

以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章