VB. NET implements MD5 Algorithm encryption (32-bit)

Source: Internet
Author: User

Function md5f (byval tempstr as string, byval w as string, byval X as string, byval y as string, byval Z as string, byval Xin as string, byval qdata as string, byval rots as integer)

Md5f = bigmod32add (rotleft (bigmod32add (bigmod32add (W, tempstr), bigmod32add (Xin, qdata), rots), X)

End Function

Sub md5f1 (byref w as string, byval X as string, byval y as string, byval Z as string, byval Xin as string, byval qdata as string, byval rots as integer)

Dim tempstr as string
Tempstr = bigor (bigand (x, y), bigand (bignot (x), Z ))

W = md5f (tempstr, W, X, Y, Z, Xin, qdata, rots)

End sub

 

Sub md5f2 (byref w as string, byval X as string, byval y as string, byval Z as string, byval Xin as string, byval qdata as string, byval rots as integer)

Dim tempstr as string

Tempstr = bigor (bigand (x, z), bigand (Y, bignot (z )))

'Tempstr = bigxor (Y, bigand (z, bigxor (x, y )))

W = md5f (tempstr, W, X, Y, Z, Xin, qdata, rots)

End sub

 

Sub md5f3 (byref w as string, byval X as string, byval y as string, byval Z as string, byval Xin as string, byval qdata as string, byval rots as integer)

Dim tempstr as string

Tempstr = bigxor (x, y), Z)

'Tempstr = bigxor (x, bigxor (y, z ))

W = md5f (tempstr, W, X, Y, Z, Xin, qdata, rots)

End sub

 

Sub md5f4 (byref w as string, byval X as string, byval y as string, byval Z as string, byval Xin as string, byval qdata as string, byval rots as integer)

Dim tempstr as string

Tempstr = bigxor (Y, bigor (x, bignot (z )))

W = md5f (tempstr, W, X, Y, Z, Xin, qdata, rots)

End sub

 

Function md5_calc (byval hashthis as string) as string

Dim Buf (4) as string

Dim Xin (16) as string

Dim tempnum as integer, tempnum2 as integer, loopit as integer, loopouter as integer, loopinner as integer

Dim A as string, B as string, C as string, D as string, tempstr99 as string

Tempnum = 8 * Len (hashthis)

Hashthis = hashthis + CHR (128) 'add binary 10000000

Tempnum2 = 56-len (hashthis) mod 64

If tempnum2 <0 then

Tempnum2 = 64 + tempnum2

End if

'Hashthis = hashthis + String (tempnum2, CHR (0 ))

For loopit = 1 to tempnum2

Hashthis = hashthis + CHR (0)

Next loopit

For loopit = 1 to 8

Hashthis = hashthis + CHR (tempnum mod 256)

Tempnum = tempnum-tempnum mod 256.

Tempnum = tempnum/256

Next loopit

Buf (0) = "67452301"

Buf (1) = "efcdab89"

Buf (2) = "98 badcfe"

Buf (3) = "10325476"

For loopouter = 0 to (LEN (hashthis)/64-1)

A = Buf (0)

B = Buf (1)

C = Buf (2)

D = Buf (3)

'Get the 512 bits

For loopit = 0 to 15

Xin (loopit) = ""

For loopinner = 1 to 4

'Tempstr99 = hex $ (ASC (mid $ (hashthis, 64 * loopouter + 4 * loopit + loopinner, 1 )))

'If Len (tempstr99) <2 then tempstr99 = "0" + tempstr99

'Xin (loopit) = Xin (loopit) + tempstr99

Xin (loopit) = hex $ (ASC (mid $ (hashthis, 64 * loopouter + 4 * loopit + loopinner, 1) + Xin (loopit)

If Len (Xin (loopit) mod 2 then Xin (loopit) = "0" + Xin (loopit)

Next loopinner

Next loopit

'First round of Loop

Md5f1 (a, B, c, d, Xin (0), "d76aa478", 7)

Md5f1 (D, a, B, c, Xin (1), "e8c7b756", 12)

Md5f1 (c, d, A, B, Xin (2), "242070db", 17)

Md5f1 (B, c, d, A, Xin (3), "c1bdceee", 22)

Md5f1 (a, B, c, d, Xin (4), "f57c0faf", 7)

Md5f1 (D, a, B, c, Xin (5), "4787c62a", 12)

Md5f1 (c, d, A, B, Xin (6), "a8304613", 17)

Md5f1 (B, c, d, A, Xin (7), "fd469501", 22)

Md5f1 (a, B, c, d, Xin (8), "698098d8", 7)

Md5f1 (D, a, B, c, Xin (9), "8b44f7af", 12)

Md5f1 (c, d, A, B, Xin (10), "ffff5bb1", 17)

Md5f1 (B, c, d, A, Xin (11), "895cd7be", 22)

Md5f1 (a, B, c, d, Xin (12), "6b901122", 7)

Md5f1 (D, a, B, c, Xin (13), "fd987193", 12)

Md5f1 (c, d, A, B, Xin (14), "a679438e", 17)

Md5f1 (B, c, d, A, Xin (15), "49b40821", 22)

 

'Second cycle

Md5f2 (a, B, c, d, Xin (1), "f61e2562", 5)

Md5f2 (D, a, B, c, Xin (6), "c040b340", 9)

Md5f2 (c, d, A, B, Xin (11), "265e5a51", 14)

Md5f2 (B, c, d, A, Xin (0), "e9b6c7aa", 20)

Md5f2 (a, B, c, d, Xin (5), "d62f105d", 5)

Md5f2 (D, a, B, c, Xin (10), "02441453", 9)

Md5f2 (c, d, A, B, Xin (15), "d8a1e681", 14)

Md5f2 (B, c, d, A, Xin (4), "e7d3fbc8", 20)

Md5f2 (a, B, c, d, Xin (9), "21e1cde6", 5)

Md5f2 (D, a, B, c, Xin (14), "c33707d6", 9)

Md5f2 (c, d, A, B, Xin (3), "f4d50d87", 14)

Md5f2 (B, c, d, A, Xin (8), "455a14ed", 20)

Md5f2 (a, B, c, d, Xin (13), "a9e3e905", 5)

Md5f2 (D, a, B, c, Xin (2), "fcefa3f8", 9)

Md5f2 (c, d, A, B, Xin (7), "676f02d9", 14)

Md5f2 (B, c, d, A, Xin (12), "8d2a4c8a", 20)

 

'Round 3

Md5f3 (a, B, c, d, Xin (5), "fffa3942", 4)

Md5f3 (D, a, B, c, Xin (8), "8771f681", 11)

Md5f3 (c, d, A, B, Xin (11), "6d9d6122", 16)

Md5f3 (B, c, d, A, Xin (14), "fde5380c", 23)

Md5f3 (a, B, c, d, Xin (1), "a4beea44", 4)

Md5f3 (D, a, B, c, Xin (4), "4bdecfa9", 11)

Md5f3 (c, d, A, B, Xin (7), "f6bb4b60", 16)

Md5f3 (B, c, d, A, Xin (10), "bebfbc70", 23)

Md5f3 (a, B, c, d, Xin (13), "289b7ec6", 4)

Md5f3 (D, a, B, c, Xin (0), "eaa450fa", 11)

Md5f3 (c, d, A, B, Xin (3), "d4ef3085", 16)

Md5f3 (B, c, d, A, Xin (6), "04881d05", 23)

Md5f3 (a, B, c, d, Xin (9), "d9d4d039", 4)

Md5f3 (D, a, B, c, Xin (12), "e6db99e5", 11)

Md5f3 (c, d, A, B, Xin (15), "1fa27cf8", 16)

Md5f3 (B, c, d, A, Xin (2), "c4ac5665", 23)

 

'Fourth round of Loop

Md5f4 (a, B, c, d, Xin (0), "f4292244", 6)

Md5f4 (D, a, B, c, Xin (7), "432aff97", 10)

Md5f4 (c, d, A, B, Xin (14), "ab9423a7", 15)

Md5f4 (B, c, d, A, Xin (5), "fc93a039", 21)

Md5f4 (a, B, c, d, Xin (12), "655b59c3", 6)

Md5f4 (D, a, B, c, Xin (3), "8f0ccc92", 10)

Md5f4 (c, d, A, B, Xin (10), "ffeff47d", 15)

Md5f4 (B, c, d, A, Xin (1), "85845dd1", 21)

Md5f4 (a, B, c, d, Xin (8), "6fa87e4f", 6)

Md5f4 (D, a, B, c, Xin (15), "fe2ce6e0", 10)

Md5f4 (c, d, A, B, Xin (6), "a3014314", 15)

Md5f4 (B, c, d, A, Xin (13), "4e0811a1", 21)

Md5f4 (a, B, c, d, Xin (4), "f7537e82", 6)

Md5f4 (D, a, B, c, Xin (11), "bd3af235", 10)

Md5f4 (c, d, A, B, Xin (2), "2ad7d2bb", 15)

Md5f4 (B, c, d, A, Xin (9), "eb86d391", 21)

 

Buf (0) = bigadd (BUF (0),)

Buf (1) = bigadd (BUF (1), B)

Buf (2) = bigadd (BUF (2), c)

Buf (3) = bigadd (BUF (3), d)

Next loopouter

Hashthis = ""

For loopit = 0 to 3

For loopinner = 3 to 0 step-1

Hashthis = hashthis + mid (BUF (loopit), 1 + 2 * loopinner, 2)

Next loopinner

Next loopit

Md5_calc = hashthis

End Function

 

Function bigmod32add (byval value1 as string, byval value2 as string) as string

Bigmod32add = right $ (bigadd (value1, value2), 8)

End Function

Public Function bigadd (byval value1 as string, byval value2 as string) as string

Dim valueans as string

Dim loopit as integer, tempnum as integer

Tempnum = Len (value1)-len (value2)

If tempnum <0 then

Value1 = space $ (math. Abs (tempnum) + value1

Elseif tempnum> 0 then

Value2 = space $ (math. Abs (tempnum) + value2

End if

Tempnum = 0

For loopit = Len (value1) to 1 step-1

Tempnum = tempnum + val ("& H" + mid $ (value1, loopit, 1) + val ("& H" + mid $ (value2, loopit, 1 ))

Valueans = hex $ (tempnum mod 16) + valueans

Tempnum = int (tempnum/16)

Next loopit

If tempnum <> 0 then

Valueans = hex $ (tempnum) + valueans

End if

 

Bigadd = right (valueans, 8)

End Function

Public Function rotleft (byval value1 as string, byval rots as integer) as string

Dim tempstr as string

Dim loopit as integer, loopinner as integer

Dim tempnum as integer

Rots = rots mod 32

If rots = 0 then

Rotleft = value1

Exit Function

End if

Value1 = right $ (value1, 8)

'Tempstr = string $ (8-len (value1), "0") + value1

For loopit = 1 to 8-len (value1)

Tempstr = tempstr + "0"

Next loopit

Tempstr = tempstr + value1

Value1 = ""

'Convert to binary format

For loopit = 1 to 8

Tempnum = Val ("& H" + mid $ (tempstr, loopit, 1 ))

For loopinner = 3 to 0 step-1

If tempnum and 2 ^ loopinner then

Value1 = value1 + "1"

Else

Value1 = value1 + "0"

End if

Next loopinner

Next loopit

Tempstr = mid $ (value1, rots + 1) + left $ (value1, rots)

'Convert to hexadecimal

Value1 = ""

For loopit = 0 to 7

Tempnum = 0

For loopinner = 0 to 3

If Val (mid $ (tempstr, 4 * loopit + loopinner + 1, 1) then

Tempnum = tempnum + 2 ^ (3-loopinner)

End if

Next loopinner

Value1 = value1 + hex $ (tempnum)

Next loopit

Rotleft = right (value1, 8)

End Function

Function bigand (byval value1 as string, byval value2 as string) as string

Dim valueans as string

Dim loopit as integer, tempnum as integer

Tempnum = Len (value1)-len (value2)

If tempnum <0 then

Value2 = mid $ (value2, math. Abs (tempnum) + 1)

Elseif tempnum> 0 then

Value1 = mid $ (value1, tempnum + 1)

End if

For loopit = 1 to Len (value1)

Valueans = valueans + hex $ (Val ("& H" + mid $ (value1, loopit, 1) and Val ("& H" + mid $ (value2, loopit, 1 )))

Next loopit

Bigand = valueans

End Function

Function bignot (byval value1 as string) as string

Dim valueans as string

Dim loopit as integer

Dim tempstr as string

Value1 = right $ (value1, 8)

'Value1 = string $ (8-len (value1), "0") + value1

For loopit = 1 to 8-len (value1)

Tempstr = tempstr + "0"

Next loopit

Tempstr = tempstr + value1

Value1 = tempstr

For loopit = 1 to 8

Valueans = valueans + hex $(15 XOR Val ("& H" + mid $ (value1, loopit, 1 )))

Next loopit

Bignot = valueans

End Function

 

Function bigor (byval value1 as string, byval value2 as string) as string

Dim valueans as string

Dim loopit as integer, tempnum as integer

Tempnum = Len (value1)-len (value2)

If tempnum <0 then

Valueans = left $ (value2, math. Abs (tempnum ))

Value2 = mid $ (value2, math. Abs (tempnum) + 1)

Elseif tempnum> 0 then

Valueans = left $ (value1, math. Abs (tempnum ))

Value1 = mid $ (value1, tempnum + 1)

End if

For loopit = 1 to Len (value1)

Valueans = valueans + hex $ (Val ("& H" + mid $ (value1, loopit, 1) or val ("& H" + mid $ (value2, loopit, 1 )))

Next loopit

Bigor = valueans

End Function

Function bigxor (byval value1 as string, byval value2 as string) as string

Dim valueans as string

Dim loopit as integer, tempnum as integer

Tempnum = Len (value1)-len (value2)

If tempnum <0 then

Valueans = left $ (value2, math. Abs (tempnum ))

Value2 = mid $ (value2, math. Abs (tempnum) + 1)

Elseif tempnum> 0 then

Valueans = left $ (value1, math. Abs (tempnum ))

Value1 = mid $ (value1, tempnum + 1)

End if

For loopit = 1 to Len (value1)

Valueans = valueans + hex $ (Val ("& H" + mid $ (value1, loopit, 1) XOR Val ("& H" + mid $ (value2, loopit, 1 )))

Next loopit

Bigxor = right (valueans, 8)

End Function

Use the md5_calc (string) function to obtain the MD5 code of the string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.