[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 谁能把VBS这个md5字符串函数,改成一个函数?这个函数太多过程,不好管理。

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

返回列表