asp_md5.asp
上传用户:btntkt
上传日期:2021-04-16
资源大小:5296k
文件大小:11k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

DOS

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