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

[已解决]VBS怎样计算文件MD5值?

如下是从网络上搜到的函数,但我在使用过程中出现ActiveX控件无法创建对象CAPICOM.HashedData,弄了许久,无果,在此求解。
  1. '获得文件的MD5值
  2. Function MD5Sum(filename)
  3.     Set MyStream = CreateObject("ADODB.Stream")
  4.     MyStream.Type = 1
  5.     MyStream.Open()
  6.     MyStream.LoadFromFile(filename)
  7.     Set MyHashed = CreateObject("CAPICOM.HashedData")
  8.     MyHashed.Algorithm = 3
  9.     MyHashed.Hash(MyStream.Read())
  10.     MyStream.Close
  11.     MD5Sum = MyHashed.Value
  12. End Function
  13. '要获取字符串的MD5值的话,需要先把字符转成二进制格式:
  14. Function str2strb(str)
  15.     For i = 1 to Len(str)
  16.         tmp = Mid(str, i, 1)
  17.         restr = restr & chrB(ascB(tmp))
  18.     Next
  19.     str2strb = restr
  20. End Function
复制代码

[ 本帖最后由 s700800900 于 2010-8-2 17:29 编辑 ]

VBS算法我倒是有,不会,主要是调用别的程序出个黑框框,消黑框框的代码还没整明白。,算了,结贴吧。下面是VBS 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. a=inputbox("请输入密码:")
  69. wscript.echo md5(a)
  70. Private Function LShift(lValue, iShiftBits)
  71. If iShiftBits = 0 Then
  72. LShift = lValue
  73. Exit Function
  74. ElseIf iShiftBits = 31 Then
  75. If lValue And 1 Then
  76. LShift = &H80000000
  77. Else
  78. LShift = 0
  79. End If
  80. Exit Function
  81. ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  82. Err.Raise 6
  83. End If
  84. If (lValue And m_l2Power(31 - iShiftBits)) Then
  85. LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
  86. Else
  87. LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
  88. End If
  89. End Function
  90. Private Function RShift(lValue, iShiftBits)
  91. If iShiftBits = 0 Then
  92. RShift = lValue
  93. Exit Function
  94. ElseIf iShiftBits = 31 Then
  95. If lValue And &H80000000 Then
  96. RShift = 1
  97. Else
  98. RShift = 0
  99. End If
  100. Exit Function
  101. ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  102. Err.Raise 6
  103. End If
  104. RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
  105. If (lValue And &H80000000) Then
  106. RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
  107. End If
  108. End Function
  109. Private Function RotateLeft(lValue, iShiftBits)
  110. RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
  111. End Function
  112. Private Function AddUnsigned(lX, lY)
  113. Dim lX4
  114. Dim lY4
  115. Dim lX8
  116. Dim lY8
  117. Dim lResult
  118. lX8 = lX And &H80000000
  119. lY8 = lY And &H80000000
  120. lX4 = lX And &H40000000
  121. lY4 = lY And &H40000000
  122. lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
  123. If lX4 And lY4 Then
  124. lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
  125. ElseIf lX4 Or lY4 Then
  126. If lResult And &H40000000 Then
  127. lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
  128. Else
  129. lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
  130. End If
  131. Else
  132. lResult = lResult Xor lX8 Xor lY8
  133. End If
  134. AddUnsigned = lResult
  135. End Function
  136. Private Function F(x, y, z)
  137. F = (x And y) Or ((Not x) And z)
  138. End Function
  139. Private Function G(x, y, z)
  140. G = (x And z) Or (y And (Not z))
  141. End Function
  142. Private Function H(x, y, z)
  143. H = (x Xor y Xor z)
  144. End Function
  145. Private Function I(x, y, z)
  146. I = (y Xor (x Or (Not z)))
  147. End Function
  148. Private Sub FF(a, b, c, d, x, s, ac)
  149. a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac))
  150. a = RotateLeft(a, s)
  151. a = AddUnsigned(a, b)
  152. End Sub
  153. Private Sub GG(a, b, c, d, x, s, ac)
  154. a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac))
  155. a = RotateLeft(a, s)
  156. a = AddUnsigned(a, b)
  157. End Sub
  158. Private Sub HH(a, b, c, d, x, s, ac)
  159. a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac))
  160. a = RotateLeft(a, s)
  161. a = AddUnsigned(a, b)
  162. End Sub
  163. Private Sub II(a, b, c, d, x, s, ac)
  164. a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac))
  165. a = RotateLeft(a, s)
  166. a = AddUnsigned(a, b)
  167. End Sub
  168. Private Function ConvertToWordArray(sMessage)
  169. Dim lMessageLength
  170. Dim lNumberOfWords
  171. Dim lWordArray()
  172. Dim lBytePosition
  173. Dim lByteCount
  174. Dim lWordCount
  175. Const MODULUS_BITS = 512
  176. Const CONGRUENT_BITS = 448
  177. lMessageLength = Len(sMessage)
  178. lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
  179. ReDim lWordArray(lNumberOfWords - 1)
  180. lBytePosition = 0
  181. lByteCount = 0
  182. Do Until lByteCount >= lMessageLength
  183. lWordCount = lByteCount \ BYTES_TO_A_WORD
  184. lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  185. lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
  186. lByteCount = lByteCount + 1
  187. Loop
  188. lWordCount = lByteCount \ BYTES_TO_A_WORD
  189. lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  190. lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
  191. lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
  192. lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
  193. ConvertToWordArray = lWordArray
  194. End Function
  195. Private Function WordToHex(lValue)
  196. Dim lByte
  197. Dim lCount
  198. For lCount = 0 To 3
  199. lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
  200. WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
  201. Next
  202. End Function
  203. Public Function MD5(sMessage)
  204. Dim x
  205. Dim k
  206. Dim AA
  207. Dim BB
  208. Dim CC
  209. Dim DD
  210. Dim a
  211. Dim b
  212. Dim c
  213. Dim d
  214. Const S11 = 7
  215. Const S12 = 12
  216. Const S13 = 17
  217. Const S14 = 22
  218. Const S21 = 5
  219. Const S22 = 9
  220. Const S23 = 14
  221. Const S24 = 20
  222. Const S31 = 4
  223. Const S32 = 11
  224. Const S33 = 16
  225. Const S34 = 23
  226. Const S41 = 6
  227. Const S42 = 10
  228. Const S43 = 15
  229. Const S44 = 21
  230. x = ConvertToWordArray(sMessage)
  231. a = &H67452301
  232. b = &HEFCDAB89
  233. c = &H98BADCFE
  234. d = &H10325476
  235. For k = 0 To UBound(x) Step 16
  236. AA = a
  237. BB = b
  238. CC = c
  239. DD = d
  240. FF a, b, c, d, x(k + 0), S11, &HD76AA478
  241. FF d, a, b, c, x(k + 1), S12, &HE8C7B756
  242. FF c, d, a, b, x(k + 2), S13, &H242070DB
  243. FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
  244. FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
  245. FF d, a, b, c, x(k + 5), S12, &H4787C62A
  246. FF c, d, a, b, x(k + 6), S13, &HA8304613
  247. FF b, c, d, a, x(k + 7), S14, &HFD469501
  248. FF a, b, c, d, x(k + 8), S11, &H698098D8
  249. FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
  250. FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
  251. FF b, c, d, a, x(k + 11), S14, &H895CD7BE
  252. FF a, b, c, d, x(k + 12), S11, &H6B901122
  253. FF d, a, b, c, x(k + 13), S12, &HFD987193
  254. FF c, d, a, b, x(k + 14), S13, &HA679438E
  255. FF b, c, d, a, x(k + 15), S14, &H49B40821
  256. GG a, b, c, d, x(k + 1), S21, &HF61E2562
  257. GG d, a, b, c, x(k + 6), S22, &HC040B340
  258. GG c, d, a, b, x(k + 11), S23, &H265E5A51
  259. GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
  260. GG a, b, c, d, x(k + 5), S21, &HD62F105D
  261. GG d, a, b, c, x(k + 10), S22, &H2441453
  262. GG c, d, a, b, x(k + 15), S23, &HD8A1E681
  263. GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
  264. GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
  265. GG d, a, b, c, x(k + 14), S22, &HC33707D6
  266. GG c, d, a, b, x(k + 3), S23, &HF4D50D87
  267. GG b, c, d, a, x(k + 8), S24, &H455A14ED
  268. GG a, b, c, d, x(k + 13), S21, &HA9E3E905
  269. GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
  270. GG c, d, a, b, x(k + 7), S23, &H676F02D9
  271. GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A
  272. HH a, b, c, d, x(k + 5), S31, &HFFFA3942
  273. HH d, a, b, c, x(k + 8), S32, &H8771F681
  274. HH c, d, a, b, x(k + 11), S33, &H6D9D6122
  275. HH b, c, d, a, x(k + 14), S34, &HFDE5380C
  276. HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
  277. HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
  278. HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
  279. HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
  280. HH a, b, c, d, x(k + 13), S31, &H289B7EC6
  281. HH d, a, b, c, x(k + 0), S32, &HEAA127FA
  282. HH c, d, a, b, x(k + 3), S33, &HD4EF3085
  283. HH b, c, d, a, x(k + 6), S34, &H4881D05
  284. HH a, b, c, d, x(k + 9), S31, &HD9D4D039
  285. HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
  286. HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
  287. HH b, c, d, a, x(k + 2), S34, &HC4AC5665
  288. II a, b, c, d, x(k + 0), S41, &HF4292244
  289. II d, a, b, c, x(k + 7), S42, &H432AFF97
  290. II c, d, a, b, x(k + 14), S43, &HAB9423A7
  291. II b, c, d, a, x(k + 5), S44, &HFC93A039
  292. II a, b, c, d, x(k + 12), S41, &H655B59C3
  293. II d, a, b, c, x(k + 3), S42, &H8F0CCC92
  294. II c, d, a, b, x(k + 10), S43, &HFFEFF47D
  295. II b, c, d, a, x(k + 1), S44, &H85845DD1
  296. II a, b, c, d, x(k + 8), S41, &H6FA87E4F
  297. II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
  298. II c, d, a, b, x(k + 6), S43, &HA3014314
  299. II b, c, d, a, x(k + 13), S44, &H4E0811A1
  300. II a, b, c, d, x(k + 4), S41, &HF7537E82
  301. II d, a, b, c, x(k + 11), S42, &HBD3AF235
  302. II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
  303. II b, c, d, a, x(k + 9), S44, &HEB86D391
  304. a = AddUnsigned(a, AA)
  305. b = AddUnsigned(b, BB)
  306. c = AddUnsigned(c, CC)
  307. d = AddUnsigned(d, DD)
  308. Next
  309. MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
  310. End Function
复制代码

[ 本帖最后由 s700800900 于 2010-8-2 17:32 编辑 ]

TOP

那时人家封装好的三方组件……

你要用需要dll或者ocx。

MD5的算法好像有VBS版的,但是是字符串的,文件的MD5的,建议还是找个3方调用下算了。

TOP

返回列表