1
2Private Const BITS_TO_A_BYTE = 8
3Private Const BYTES_TO_A_WORD = 4
4Private Const BITS_TO_A_WORD = 32
5
6Private m_lOnBits(30)
7Private m_l2Power(30)
8
9Private Function LShift(lValue, iShiftBits)
10If iShiftBits = 0 Then
11LShift = lValue
12Exit Function
13ElseIf iShiftBits = 31 Then
14If lValue And 1 Then
15LShift = &H80000000
16Else
17LShift = 0
18End If
19Exit Function
20ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
21Err.Raise 6
22End If
23
24If (lValue And m_l2Power(31 - iShiftBits)) Then
25LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
26Else
27LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
28End If
29End Function
30
31Private Function RShift(lValue, iShiftBits)
32If iShiftBits = 0 Then
33RShift = lValue
34Exit Function
35ElseIf iShiftBits = 31 Then
36If lValue And &H80000000 Then
37RShift = 1
38Else
39RShift = 0
40End If
41Exit Function
42ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
43Err.Raise 6
44End If
45
46RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
47
48If (lValue And &H80000000) Then
49RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
50End If
51End Function
52
53Private Function RotateLeft(lValue, iShiftBits)
54RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
55End Function
56
57Private Function AddUnsigned(lX, lY)
58Dim lX4
59Dim lY4
60Dim lX8
61Dim lY8
62Dim lResult
63
64lX8 = lX And &H80000000
65lY8 = lY And &H80000000
66lX4 = lX And &H40000000
67lY4 = lY And &H40000000
68
69lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
70
71If lX4 And lY4 Then
72lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
73ElseIf lX4 Or lY4 Then
74If lResult And &H40000000 Then
75lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
76Else
77lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
78End If
79Else
80lResult = lResult Xor lX8 Xor lY8
81End If
82
83AddUnsigned = lResult
84End Function
85
86Private Function md5_F(x, y, z)
87md5_F = (x And y) Or ((Not x) And z)
88End Function
89
90Private Function md5_G(x, y, z)
91md5_G = (x And z) Or (y And (Not z))
92End Function
93
94Private Function md5_H(x, y, z)
95md5_H = (x Xor y Xor z)
96End Function
97
98Private Function md5_I(x, y, z)
99md5_I = (y Xor (x Or (Not z)))
100End Function
101
102Private Sub md5_FF(a, b, c, d, x, s, ac)
103a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))
104a = RotateLeft(a, s)
105a = AddUnsigned(a, b)
106End Sub
107
108Private Sub md5_GG(a, b, c, d, x, s, ac)
109a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))
110a = RotateLeft(a, s)
111a = AddUnsigned(a, b)
112End Sub
113
114Private Sub md5_HH(a, b, c, d, x, s, ac)
115a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))
116a = RotateLeft(a, s)
117a = AddUnsigned(a, b)
118End Sub
119
120Private Sub md5_II(a, b, c, d, x, s, ac)
121a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))
122a = RotateLeft(a, s)
123a = AddUnsigned(a, b)
124End Sub
125
126Private Function ConvertToWordArray(sMessage)
127Dim lMessageLength
128Dim lNumberOfWords
129Dim lWordArray()
130Dim lBytePosition
131Dim lByteCount
132Dim lWordCount
133
134Const MODULUS_BITS = 512
135Const CONGRUENT_BITS = 448
136
137lMessageLength = Len(sMessage)
138
139lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
140ReDim lWordArray(lNumberOfWords - 1)
141
142lBytePosition = 0
143lByteCount = 0
144Do Until lByteCount >= lMessageLength
145lWordCount = lByteCount \ BYTES_TO_A_WORD
146lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
147lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
148lByteCount = lByteCount + 1
149Loop
150
151lWordCount = lByteCount \ BYTES_TO_A_WORD
152lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
153
154lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
155
156lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
157lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
158
159ConvertToWordArray = lWordArray
160End Function
161
162Private Function WordToHex(lValue)
163Dim lByte
164Dim lCount
165
166For lCount = 0 To 3
167lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
168WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
169Next
170End Function
171
172Public Function MD5(sMessage)
173m_lOnBits(0) = CLng(1)
174m_lOnBits(1) = CLng(3)
175m_lOnBits(2) = CLng(7)
176m_lOnBits(3) = CLng(15)
177m_lOnBits(4) = CLng(31)
178m_lOnBits(5) = CLng(63)
179m_lOnBits(6) = CLng(127)
180m_lOnBits(7) = CLng(255)
181m_lOnBits(8) = CLng(511)
182m_lOnBits(9) = CLng(1023)
183m_lOnBits(10) = CLng(2047)
184m_lOnBits(11) = CLng(4095)
185m_lOnBits(12) = CLng(8191)
186m_lOnBits(13) = CLng(16383)
187m_lOnBits(14) = CLng(32767)
188m_lOnBits(15) = CLng(65535)
189m_lOnBits(16) = CLng(131071)
190m_lOnBits(17) = CLng(262143)
191m_lOnBits(18) = CLng(524287)
192m_lOnBits(19) = CLng(1048575)
193m_lOnBits(20) = CLng(2097151)
194m_lOnBits(21) = CLng(4194303)
195m_lOnBits(22) = CLng(8388607)
196m_lOnBits(23) = CLng(16777215)
197m_lOnBits(24) = CLng(33554431)
198m_lOnBits(25) = CLng(67108863)
199m_lOnBits(26) = CLng(134217727)
200m_lOnBits(27) = CLng(268435455)
201m_lOnBits(28) = CLng(536870911)
202m_lOnBits(29) = CLng(1073741823)
203m_lOnBits(30) = CLng(2147483647)
204
205m_l2Power(0) = CLng(1)
206m_l2Power(1) = CLng(2)
207m_l2Power(2) = CLng(4)
208m_l2Power(3) = CLng(8)
209m_l2Power(4) = CLng(16)
210m_l2Power(5) = CLng(32)
211m_l2Power(6) = CLng(64)
212m_l2Power(7) = CLng(128)
213m_l2Power(8) = CLng(256)
214m_l2Power(9) = CLng(512)
215m_l2Power(10) = CLng(1024)
216m_l2Power(11) = CLng(2048)
217m_l2Power(12) = CLng(4096)
218m_l2Power(13) = CLng(8192)
219m_l2Power(14) = CLng(16384)
220m_l2Power(15) = CLng(32768)
221m_l2Power(16) = CLng(65536)
222m_l2Power(17) = CLng(131072)
223m_l2Power(18) = CLng(262144)
224m_l2Power(19) = CLng(524288)
225m_l2Power(20) = CLng(1048576)
226m_l2Power(21) = CLng(2097152)
227m_l2Power(22) = CLng(4194304)
228m_l2Power(23) = CLng(8388608)
229m_l2Power(24) = CLng(16777216)
230m_l2Power(25) = CLng(33554432)
231m_l2Power(26) = CLng(67108864)
232m_l2Power(27) = CLng(134217728)
233m_l2Power(28) = CLng(268435456)
234m_l2Power(29) = CLng(536870912)
235m_l2Power(30) = CLng(1073741824)
236
237
238Dim x
239Dim k
240Dim AA
241Dim BB
242Dim CC
243Dim DD
244Dim a
245Dim b
246Dim c
247Dim d
248
249Const S11 = 7
250Const S12 = 12
251Const S13 = 17
252Const S14 = 22
253Const S21 = 5
254Const S22 = 9
255Const S23 = 14
256Const S24 = 20
257Const S31 = 4
258Const S32 = 11
259Const S33 = 16
260Const S34 = 23
261Const S41 = 6
262Const S42 = 10
263Const S43 = 15
264Const S44 = 21
265
266x = ConvertToWordArray(sMessage)
267
268a = &H67452301
269b = &HEFCDAB89
270c = &H98BADCFE
271d = &H10325476
272
273For k = 0 To UBound(x) Step 16
274AA = a
275BB = b
276CC = c
277DD = d
278
279md5_FF a, b, c, d, x(k + 0), S11, &HD76AA478
280md5_FF d, a, b, c, x(k + 1), S12, &HE8C7B756
281md5_FF c, d, a, b, x(k + 2), S13, &H242070DB
282md5_FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
283md5_FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
284md5_FF d, a, b, c, x(k + 5), S12, &H4787C62A
285md5_FF c, d, a, b, x(k + 6), S13, &HA8304613
286md5_FF b, c, d, a, x(k + 7), S14, &HFD469501
287md5_FF a, b, c, d, x(k + 8), S11, &H698098D8
288md5_FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
289md5_FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
290md5_FF b, c, d, a, x(k + 11), S14, &H895CD7BE
291md5_FF a, b, c, d, x(k + 12), S11, &H6B901122
292md5_FF d, a, b, c, x(k + 13), S12, &HFD987193
293md5_FF c, d, a, b, x(k + 14), S13, &HA679438E
294md5_FF b, c, d, a, x(k + 15), S14, &H49B40821
295
296md5_GG a, b, c, d, x(k + 1), S21, &HF61E2562
297md5_GG d, a, b, c, x(k + 6), S22, &HC040B340
298md5_GG c, d, a, b, x(k + 11), S23, &H265E5A51
299md5_GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
300md5_GG a, b, c, d, x(k + 5), S21, &HD62F105D
301md5_GG d, a, b, c, x(k + 10), S22, &H2441453
302md5_GG c, d, a, b, x(k + 15), S23, &HD8A1E681
303md5_GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
304md5_GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
305md5_GG d, a, b, c, x(k + 14), S22, &HC33707D6
306md5_GG c, d, a, b, x(k + 3), S23, &HF4D50D87
307md5_GG b, c, d, a, x(k + 8), S24, &H455A14ED
308md5_GG a, b, c, d, x(k + 13), S21, &HA9E3E905
309md5_GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
310md5_GG c, d, a, b, x(k + 7), S23, &H676F02D9
311md5_GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A
312
313md5_HH a, b, c, d, x(k + 5), S31, &HFFFA3942
314md5_HH d, a, b, c, x(k + 8), S32, &H8771F681
315md5_HH c, d, a, b, x(k + 11), S33, &H6D9D6122
316md5_HH b, c, d, a, x(k + 14), S34, &HFDE5380C
317md5_HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
318md5_HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
319md5_HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
320md5_HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
321md5_HH a, b, c, d, x(k + 13), S31, &H289B7EC6
322md5_HH d, a, b, c, x(k + 0), S32, &HEAA127FA
323md5_HH c, d, a, b, x(k + 3), S33, &HD4EF3085
324md5_HH b, c, d, a, x(k + 6), S34, &H4881D05
325md5_HH a, b, c, d, x(k + 9), S31, &HD9D4D039
326md5_HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
327md5_HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
328md5_HH b, c, d, a, x(k + 2), S34, &HC4AC5665
329
330md5_II a, b, c, d, x(k + 0), S41, &HF4292244
331md5_II d, a, b, c, x(k + 7), S42, &H432AFF97
332md5_II c, d, a, b, x(k + 14), S43, &HAB9423A7
333md5_II b, c, d, a, x(k + 5), S44, &HFC93A039
334md5_II a, b, c, d, x(k + 12), S41, &H655B59C3
335md5_II d, a, b, c, x(k + 3), S42, &H8F0CCC92
336md5_II c, d, a, b, x(k + 10), S43, &HFFEFF47D
337md5_II b, c, d, a, x(k + 1), S44, &H85845DD1
338md5_II a, b, c, d, x(k + 8), S41, &H6FA87E4F
339md5_II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
340md5_II c, d, a, b, x(k + 6), S43, &HA3014314
341md5_II b, c, d, a, x(k + 13), S44, &H4E0811A1
342md5_II a, b, c, d, x(k + 4), S41, &HF7537E82
343md5_II d, a, b, c, x(k + 11), S42, &HBD3AF235
344md5_II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
345md5_II b, c, d, a, x(k + 9), S44, &HEB86D391
346
347a = AddUnsigned(a, AA)
348b = AddUnsigned(b, BB)
349c = AddUnsigned(c, CC)
350d = AddUnsigned(d, DD)
351Next
352
353MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
354' MD5=LCase(WordToHex(b) & WordToHex(c)) 'I crop this to fit 16byte database password :D
355End Function
356
357Response.Write "123456的加密结果为[" & md5 ("123456") & "]"
MD5不可逆加密算法的ASP实现实例
comments powered by Disqus