ASP implementation of MD5 algorithm

xiaoxiao2021-03-06  62

8 code demo coding by sunrise_chen.

Description, please enter the original code: After the MD5 conversion is:

Overview of 8 Code Coding by Sunrise_Chen.

The full name of Description MD5 is a Message-Digest Algorithm 5 (Information-Summary Algorithm), which is developed by Mit Laboratory for Computer Science and RSA Data Security Inc, which is developed by MD2, MD3, and MD4 in the 1990s. Its role is to make large capacity information to be "compressed" into a confidential format before signing private privacy with digital signature software (that is, transforms an arbitrary length of byte string into a certain length). Whether it is MD2, MD4 or MD5, they all need to obtain a random length of information and generate a 128-bit information summary. Although the structure of these algorithms is more similar, the design of MD2 is completely different from MD4 and MD5, because MD2 is designed for 8-bit machines, while MD4 and MD5 are 32-bit computers. The descriptions of these three algorithms and C language source code have detailed descriptions in Internet RFCS 1321, http://www.ietf.org/rfc/rfc1321.txt), this is the most authoritative document, by Ronald L. Rivest submitted to IEFT in August 1992. Rivest developed an MD2 algorithm in 1989. In this algorithm, the information is first supplied to the information, so that the byte length of the information is a multiple of 16. Then, at a 16-bit test and append to the end of the information. And calculate the hash value based on this new information. Later, Rogier and Chauvaud found that if the test and the MD2 conflict will be generated. The result of the encryption of the MD2 algorithm is unique - neither repeat. In order to enhance the safety of the algorithm, RiveSt has developed an MD4 algorithm in 1990. The MD4 algorithm also needs to fill the information to ensure that the byte length of the information plus 448 can be divided by 512 (information byte length MOD 512 = 448). Then, one of the initial lengths of information indicated by 64-bit binary is added. The information is processed into blocks of 512-bit DAMG? RD / Merkle iterative structure, and each block is to be processed by three different steps. Den Boer and Bosselaers and others quickly discovered the first step and third steps in the MD4 version. Dobbertin demonstrates how to use a regular personal computer to find a conflict in the full version of the MD4 in a few minutes (this conflict is actually a vulnerability, which will result in encryption of different content but may get the same encryption. result). There is no doubt that MD4 is eliminated. Although the MD4 algorithm has such a large vulnerability in the security, it has a guiding role in the presence of several information security encryption algorithms that have been developed thereon. In addition to MD5, there are also SHA-1, RIPE-MD, and HAVAL, etc. One year later, that is, in 1991, Rivest developed technology more approached MD5 algorithm. It adds the concept of "safe-band" (safty-beelts) based on MD4. Although MD5 is slightly slower than MD4, it is safer. This algorithm is obvious by four and MD4 designs with a little different steps. In the MD5 algorithm, the size of the information - summary and the necessary conditions of the filler are identical to the MD4. Den Boer and Bosselaers have discovered pseudo-collisions in the MD5 algorithm, but there is no other resulting result. Van Orschot and Wiener have considered a function of violent search conflicts in hash, and they guess a machine that is designed to search for MD5 conflicts (this machine is approximately manufactured in 1994 It is a million US dollars) to find a conflict every 24 days.

However, from 1991 to 2001, there is no new algorithm for MD6 or other names that have alternative MD5 algorithms, and we can see that this flaw does not have much impact on MD5 security. All of these is not enough to become MD5 problems in practical applications. Also, since the use of the MD5 algorithm does not need to pay any copyright costs, in general (non-top secret applications. But even if the application is in the top secret field, MD5 is not a very good intermediate technology), MD5 It should be considered very safe. 8 code analysis Coding by sunrise_chen.

Source Code

<%

'--------------------------------------

'Cocoon Disk Manager V3 - MD5 Function

'

'Classic MD5 algorithm ASP implementation

'- taken from a foreign website

'--------------------------------------

PRIVATE CONST BITS_TO_A_BYTE = 8

Private const bytes_to_a_word = 4

PRIVATE const bits_to_a_word = 32

Private m_lonbits (30)

Private M_L2Power (30)

Private function lshift (Lvalue, iShiftBits)

IF iShiftBits = 0 THEN

Lshift = LVALUE

EXIT FUNCTION

Elseif ishiftBits = 31 THEN

IF Lvalue and 1 THEN

Lshift = & h80000000

Else

Lshift = 0

END IF

EXIT FUNCTION

Elseif IshiftBits <0 or iShiftBits> 31 THEN

Err.raise 6

END IF

IF (LValue and M_L2Power (31 - IshiftBits)) THEN

Lshift = ((Lvalue and M_lonbits (31 - (iShiftBits 1))) * m_l2power (iShiftBits)) or & h80000000

Else

Lshift = ((Lvalue and M_Lonbits) * m_l2power (iShiftBits))

END IF

END FUNCTION

Private function Rshift (Lvalue, IshiftBits)

IF iShiftBits = 0 THEN

Rshift = LVALUE

EXIT FUNCTION

Elseif ishiftBits = 31 THEN

If Lvalue and & H80000000 THEN

Rshift = 1

Else

Rshift = 0

END IF

EXIT FUNCTION

Elseif IshiftBits <0 or iShiftBits> 31 THEN

Err.raise 6

END IF

Rshift = (LValue and & H7ffffe) / M_L2Power (iShiftBits)

IF (Lvalue and & H80000000) THEN

Rshift = (RSHIFT OR (& H400000 / M_L2Power (IshiftBits - 1)))

END IF

End FunctionPrivate Function RotateLeft (Lvalue, iShiftBits)

Rotateleft = Lshift (Lvalue, IshiftBits) or Rshift (Lvalue, (32 - IshiftBits))

END FUNCTION

Private function addunsigned (lx, ly)

DIM LX4

DIM LY4

DIM LX8

DIM LY8

DIM LRESULT

Lx8 = lx and & h80000000

LY8 = ly and & h80000000

LX4 = lx and & h40000000

LY4 = Ly and & H40000000

LRESULT = (lx and & h3ffffff) (Ly and & H3FFFFFF)

IF lx4 and ly4 Then

Lresult = LRESULT XOR & H80000000 XOR LX8 XOR LY8

Elseif Lx4 or Ly4 Then

If LRESULT AND & H40000000 THEN

LResult = LRESULT XOR & HC0000000 XOR LX8 XOR LY8

Else

Lresult = LRESULT XOR & H40000000 xor LX8 XOR LY8

END IF

Else

LResult = LRESULT XOR LX8 XOR LY8

END IF

Addunsigned = LRESULT

END FUNCTION

Private function MD5_f (x, y, z)

MD5_F = (x and y) OR ((not x) and z)

END FUNCTION

Private function MD5_G (x, y, z)

MD5_G = (x and z) OR (Y AND (NOT Z))

END FUNCTION

Private function MD5_H (x, y, z)

MD5_H = (x xor y xor z)

END FUNCTION

Private function MD5_i (x, y, z)

MD5_i = (y xor (x or (not z))))

END FUNCTION

Private SUB MD5_FF (A, B, C, D, X, S, AC)

A = AddunSigned (A, AddunSigned (addunsigned (MD5_F (B, C, D), X), AC)

a = rotateleft (a, s)

A = addunsigned (a, b)

End Sub

Private SUB MD5_GG (A, B, C, D, X, S, AC)

A = addunsigned (A, AddunSigned (addunsigned (MD5_G (B, C, D), X), AC)

a = rotateleft (a, s)

A = addunsigned (a, b)

End Sub

Private SUB MD5_HH (A, B, C, D, X, S, AC)

A = addunsigned (A, AddunSigned (addunsigned (MD5_H (B, C, D), X), AC)

a = rotateleft (a, s)

A = addunsigned (a, b)

End Sub

Private SUB MD5_II (A, B, C, D, X, S, AC)

A = addunsigned (A, AddunSigned (AddunSigned (MD5_i (B, D), X), AC)) A = RotateLeft (A, S)

A = addunsigned (a, b)

End Sub

Private function convertToWordArray (SMESSAGE)

Dim LMessageLength

Dim lnumberofwords

Dim lwordArray ()

DIM LBYTEPSITION

DIM LBYTECOUNT

Dim lwordcount

Const modulus_bits = 512

Const congruent_bits = 448

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) OR LSHIFT (ASC (MID (SMESSAGE, LBYTECUNT 1, 1)), LBYTEPOSITION

lbytecount = lbytecount 1

Loop

LWORDCOUNT = lbytecount / bytes_to_a_word

LbytePosition = (lbytecount mod bytes_to_a_word) * bits_to_a_byte

LWordArray (lwordcount) = lwordArray (lwordcount) or lshift (& H80, lbyteposition)

LWordArray (lnumberofwords - 2) = lshift (LMessageLength, 3)

LWordArray (Lnumberofwords - 1) = RSHIFT (LMessageLength, 29)

ConvertToWordArray = LWORDARRAY

END FUNCTION

Private function Wordtohex (LValue)

DIM LBYTE

DIM LCOUNT

For LCOUNT = 0 TO 3

Lbyte = RSHIFT (LVALUE, LCOUNT * BITS_TO_A_BYTE) AND M_LONBITS (Bits_TO_A_BYTE - 1)

Wordtohex = WordToHex & Right ("0" & ​​HEX (LbyTe), 2)

NEXT

END FUNCTION

Public Function MD5 (SMESSAGE)

m_lonbits (0) = ClNG (1)

M_lonbits (1) = CLNG (3)

M_lonbits (2) = CLNG (7)

M_lonbits (3) = CLNG (15) M_LONBITS (4) = CLNG (31)

M_lonbits (5) = clng (63)

M_lonbit (6) = CLNG (127)

M_lonbits (7) = CLNG (255)

M_lonbits (8) = CLNG (511)

m_lonbits (9) = CLNG (1023)

M_lonbits (10) = CLNG (2047)

M_lonbits (11) = clng (4095)

M_lonbits (12) = CLNG (8191)

m_lonbits (13) = CLNG (16383)

M_lonbits (14) = CLNG (32767)

M_lonbits (15) = CLNG (65535)

M_lonbits (16) = CLNG (131071)

M_lonbits (17) = CLNG (262143)

M_lonbits (18) = CLNG (524287)

M_lonbits (19) = clng (1048575)

M_lonbits (20) = CLNG (2097151)

M_lonbits (21) = clng (4194303)

M_lonbits (22) = CLNG (8388607)

M_lonbits (23) = CLNG (16777215)

M_LONBITS (24) = CLNG (33554431)

M_LONBITS (25) = CLNG (67108863)

M_lonbits (26) = CLNG (134217727)

M_lonbit (27) = CLNG (268435455)

M_lonbit (28) = CLNG (536870911)

M_lonbits (29) = CLNG (1073741823)

M_lonbits (30) = clng (2147483647)

m_l2power (0) = CLNG (1)

M_L2Power (1) = CLNG (2)

M_L2Power (2) = CLNG (4)

M_L2Power (3) = CLNG (8)

M_L2Power (4) = CLNG (16)

M_L2Power (5) = CLNG (32)

M_L2Power (6) = CLNG (64)

M_L2Power (7) = clng (128)

M_L2Power (8) = CLNG (256)

M_L2Power (9) = CLNG (512)

M_L2Power (10) = CLNG (1024)

M_L2Power (11) = CLNG (2048)

M_L2Power (12) = CLNG (4096)

M_L2Power (13) = clng (8192)

M_L2Power (14) = CLNG (16384)

M_L2Power (15) = CLNG (32768)

M_L2Power (16) = CLNG (65536)

m_l2power (17) = CLNG (131072)

M_L2Power (18) = CLNG (262144)

M_L2Power (19) = CLNG (524288)

M_L2Power (20) = clng (1048576)

M_L2Power (21) = CLNG (2097152)

M_L2Power (22) = CLNG (4194304)

M_L2Power (23) = CLNG (8388608)

M_L2Power (24) = CLNG (16777216)

M_L2Power (25) = CLNG (33554432) M_L2Power (26) = CLNG (67108864)

M_L2Power (27) = CLNG (134217728)

M_L2Power (28) = CLNG (268435456)

M_L2Power (29) = CLNG (536870912)

M_L2Power (30) = CLNG (1073741824)

DIM X

DIM K

DIM AA

DIM BB

DIM CC

DIM DD

DIM A

DIM B

DIM C

DIM D

Const S11 = 7

Const S12 = 12

Const S13 = 17

Const S14 = 22

Const S21 = 5

Const S22 = 9

Const S23 = 14

Const S24 = 20

Const S31 = 4

Const S32 = 11

Const S33 = 16

Const S34 = 23

Const S41 = 6

Const S42 = 10

Const S43 = 15

Const S44 = 21

x = ConvertToWordArray (SMESSAGE)

A = & h67452301

B = & hefcdab89

C = & H98BADCFE

D = & h10325476

For K = 0 to Ubound (x) STEP 16

AA = a

BB = B

CC = C

DD = D

MD5_FF A, B, C, D, X (K 0), S11, & HD76AA478

MD5_FF D, A, B, C, X (K 1), S12, & HE8C7B756

MD5_FF C, D, A, B, X (K 2), S13, & H242070DB

MD5_FF B, C, D, A, X (k 3), S14, & HC1BDCEEE

MD5_FF A, B, C, D, X (K 4), S11, & HF57C0FAF

MD5_FF D, A, B, C, X (K 5), S12, & H4787C62A

MD5_FF C, D, A, B, X (K 6), S13, & Ha8304613

MD5_FF B, C, D, A, X (K 7), S14, & HFD469501

MD5_FF A, B, C, D, X (K 8), S11, & H698098D8

MD5_FF D, A, B, C, X (K 9), S12, & H8B44F7AF

MD5_FF C, D, A, B, X (K 10), S13, & HFFFF5BB1

MD5_FF B, C, D, A, X (K 11), S14, & H895CD7BE

MD5_FF A, B, C, D, X (k 12), S11, & H6B901122

MD5_FF D, A, B, C, X (K 13), S12, & HFD987193

MD5_FF C, D, A, B, X (K 14), S13, & Ha679438E

MD5_FF B, C, D, A, X (K 15), S14, & H49B40821

MD5_GG A, B, C, D, X (K 1), S21, & HF61E2562

MD5_GG D, A, B, C, X (K 6), S22, & HC040B340

MD5_GG C, D, A, B, X (K 11), S23, & H265E5A51

MD5_GG B, C, D, A, X (k 0), S24, & HE9B6C7AA

MD5_GG A, B, C, D, X (K 5), S21, & HD62F105D

MD5_GG D, A, B, C, X (K 10), S22, & H2441453

MD5_GG C, D, A, B, X (K 15), S23, & HD8A1E681

MD5_GG B, C, D, A, X (K 4), S24, & HE7D3FBC8

MD5_GG A, B, C, D, X (K 9), S21, & H21E1CDE6

MD5_GG D, A, B, C, X (k 14), S22, & HC33707D6

MD5_GG C, D, A, B, X (K 3), S23, & HF4D50D87

MD5_GG B, C, D, A, X (K 8), S24, & H455A14ED

MD5_GG A, B, C, D, X (K 13), S21, & HA9E3E905

MD5_GG D, A, B, C, X (K 2), S22, & HFCEFA3F8

MD5_GG C, D, A, B, X (K 7), S23, & H676F02D9

MD5_GG B, C, D, A, X (K 12), S24, & H8D2A4C8A

MD5_HH A, B, C, D, X (K 5), S31, & HFFFA3942

MD5_HH D, A, B, C, X (K 8), S32, & H8771F681

MD5_HH C, D, A, B, X (K 11), S33, & H6D9D6122

MD5_HHH B, C, D, A, X (k 14), S34, & HFDE5380C

MD5_HH A, B, C, D, X (k 1), S31, & Ha4bee44

MD5_HH D, A, B, C, X (K 4), S32, & H4BDECFA9

MD5_HH C, D, A, B, X (K 7), S33, & HF6BB4B60

MD5_HHH B, C, D, A, X (K 10), S34, & HBebfbc70

MD5_HH A, B, C, D, X (K 13), S31, & H289B7EC6

MD5_HH D, A, B, C, X (K 0), S32, & HeaA127FA

MD5_HH C, D, A, B, X (K 3), S33, & HD4EF3085

MD5_HHH B, C, D, A, X (K 6), S34, & H4881D05

MD5_HH A, B, C, D, X (K 9), S31, & HD9D4D039

MD5_HH D, A, B, C, X (K 12), S32, & HE6DB99E5

MD5_HH C, D, A, B, X (K 15), S33, & H1FA27CF8

MD5_HHH B, C, D, A, X (K 2), S34, & HC4AC5665

MD5_II A, B, C, D, X (K 0), S41, & HF4292244

MD5_II D, A, B, C, X (K 7), S42, & H432AFF97MD5_II C, D, A, B, X (K 14), S43, & HAB9423A7

MD5_II B, C, D, A, X (k 5), S44, & HFC93A039

MD5_II A, B, C, D, X (k 12), S41, & H655B59C3

MD5_II D, A, B, C, X (K 3), S42, & H8F0CCC92

MD5_II C, D, A, B, X (K 10), S43, & HFFEFF47D

MD5_II B, C, D, A, X (k 1), S44, & H85845DD1

MD5_II A, B, C, D, X (K 8), S41, & H6FA87E4F

MD5_II D, A, B, C, X (K 15), S42, & HFE2CE6E0

MD5_II C, D, A, B, X (K 6), S43, & HA3014314

MD5_II B, C, D, A, X (K 13), S44, & H4E0811A1

MD5_II A, B, C, D, X (K 4), S41, & HF7537E82

MD5_II D, A, B, C, X (K 11), S42, & HBD3AF235

MD5_II C, D, A, B, X (K 2), S43, & H2AD7D2BB

MD5_II B, C, D, A, X (K 9), S44, & HEB86D391

A = addunsigned (A, AA)

B = addunsigned (b, bb)

C = addunsigned (C, CC)

D = addunsigned (D, DD)

NEXT

MD5 = LCase (WordTohex (a) & WordTohex (B) & WordTohex (C) & WordToHex (D))

END FUNCTION

%>

转载请注明原文地址:https://www.9cbs.com/read-84350.html

New Post(0)