设为首页收藏本站

SKY外语、计算机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3111|回复: 1
打印 上一主题 下一主题

VB6_MD5不可逆算法

[复制链接]

16

主题

0

好友

216

积分

中级会员

Rank: 3Rank: 3

生肖
性别

最佳新人 论坛元老

跳转到指定楼层
楼主
发表于 2012-4-28 21:24:41 |只看该作者 |倒序浏览
本帖最后由 fieldmax 于 2012-4-28 22:14 编辑

  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. Private Function LShift(lValue, iShiftBits)
  7. If iShiftBits = 0 Then
  8. LShift = lValue
  9. Exit Function
  10. ElseIf iShiftBits = 31 Then
  11. If lValue And 1 Then
  12. LShift = &H80000000
  13. Else
  14. LShift = 0
  15. End If
  16. Exit Function
  17. ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  18. Err.Raise 6
  19. End If

  20. If (lValue And m_l2Power(31 - iShiftBits)) Then
  21. LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
  22. Else
  23. LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
  24. End If
  25. End Function

  26. Private Function RShift(lValue, iShiftBits)
  27. If iShiftBits = 0 Then
  28. RShift = lValue
  29. Exit Function
  30. ElseIf iShiftBits = 31 Then
  31. If lValue And &H80000000 Then
  32. RShift = 1
  33. Else
  34. RShift = 0
  35. End If
  36. Exit Function
  37. ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  38. Err.Raise 6
  39. End If

  40. RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)

  41. If (lValue And &H80000000) Then
  42. RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
  43. End If
  44. End Function

  45. Private Function RotateLeft(lValue, iShiftBits)
  46. RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
  47. End Function

  48. Private Function AddUnsigned(lX, lY)
  49. Dim lX4
  50. Dim lY4
  51. Dim lX8
  52. Dim lY8
  53. Dim lResult

  54. lX8 = lX And &H80000000
  55. lY8 = lY And &H80000000
  56. lX4 = lX And &H40000000
  57. lY4 = lY And &H40000000

  58. lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)

  59. If lX4 And lY4 Then
  60. lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
  61. ElseIf lX4 Or lY4 Then
  62. If lResult And &H40000000 Then
  63. lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
  64. Else
  65. lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
  66. End If
  67. Else
  68. lResult = lResult Xor lX8 Xor lY8
  69. End If

  70. AddUnsigned = lResult
  71. End Function

  72. Private Function md5_F(x, y, z)
  73. md5_F = (x And y) Or ((Not x) And z)
  74. End Function

  75. Private Function md5_G(x, y, z)
  76. md5_G = (x And z) Or (y And (Not z))
  77. End Function

  78. Private Function md5_H(x, y, z)
  79. md5_H = (x Xor y Xor z)
  80. End Function

  81. Private Function md5_I(x, y, z)
  82. md5_I = (y Xor (x Or (Not z)))
  83. End Function

  84. Private Sub md5_FF(a, b, c, d, x, s, ac)
  85. a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))
  86. a = RotateLeft(a, s)
  87. a = AddUnsigned(a, b)
  88. End Sub

  89. Private Sub md5_GG(a, b, c, d, x, s, ac)
  90. a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))
  91. a = RotateLeft(a, s)
  92. a = AddUnsigned(a, b)
  93. End Sub

  94. Private Sub md5_HH(a, b, c, d, x, s, ac)
  95. a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))
  96. a = RotateLeft(a, s)
  97. a = AddUnsigned(a, b)
  98. End Sub

  99. Private Sub md5_II(a, b, c, d, x, s, ac)
  100. a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))
  101. a = RotateLeft(a, s)
  102. a = AddUnsigned(a, b)
  103. End Sub

  104. Private Function ConvertToWordArray(sMessage)
  105. Dim lMessageLength
  106. Dim lNumberOfWords
  107. Dim lWordArray()
  108. Dim lBytePosition
  109. Dim lByteCount
  110. Dim lWordCount

  111. Const MODULUS_BITS = 512
  112. Const CONGRUENT_BITS = 448

  113. lMessageLength = Len(sMessage)

  114. lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
  115. ReDim lWordArray(lNumberOfWords - 1)

  116. lBytePosition = 0
  117. lByteCount = 0
  118. Do Until lByteCount >= lMessageLength
  119. lWordCount = lByteCount \ BYTES_TO_A_WORD
  120. lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
  121. lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
  122. lByteCount = lByteCount + 1
  123. Loop

  124. lWordCount = lByteCount \ BYTES_TO_A_WORD
  125. lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE

  126. lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)

  127. lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
  128. lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)

  129. ConvertToWordArray = lWordArray
  130. End Function

  131. Private Function WordToHex(lValue)
  132. Dim lByte
  133. Dim lCount

  134. For lCount = 0 To 3
  135. lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
  136. WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
  137. Next
  138. End Function

  139. Public Function MD5(sMessage)
  140. m_lOnBits(0) = CLng(1)
  141. m_lOnBits(1) = CLng(3)
  142. m_lOnBits(2) = CLng(7)
  143. m_lOnBits(3) = CLng(15)
  144. m_lOnBits(4) = CLng(31)
  145. m_lOnBits(5) = CLng(63)
  146. m_lOnBits(6) = CLng(127)
  147. m_lOnBits(7) = CLng(255)
  148. m_lOnBits(8) = CLng(511)
  149. m_lOnBits(9) = CLng(1023)
  150. m_lOnBits(10) = CLng(2047)
  151. m_lOnBits(11) = CLng(4095)
  152. m_lOnBits(12) = CLng(8191)
  153. m_lOnBits(13) = CLng(16383)
  154. m_lOnBits(14) = CLng(32767)
  155. m_lOnBits(15) = CLng(65535)
  156. m_lOnBits(16) = CLng(131071)
  157. m_lOnBits(17) = CLng(262143)
  158. m_lOnBits(18) = CLng(524287)
  159. m_lOnBits(19) = CLng(1048575)
  160. m_lOnBits(20) = CLng(2097151)
  161. m_lOnBits(21) = CLng(4194303)
  162. m_lOnBits(22) = CLng(8388607)
  163. m_lOnBits(23) = CLng(16777215)
  164. m_lOnBits(24) = CLng(33554431)
  165. m_lOnBits(25) = CLng(67108863)
  166. m_lOnBits(26) = CLng(134217727)
  167. m_lOnBits(27) = CLng(268435455)
  168. m_lOnBits(28) = CLng(536870911)
  169. m_lOnBits(29) = CLng(1073741823)
  170. m_lOnBits(30) = CLng(2147483647)

  171. m_l2Power(0) = CLng(1)
  172. m_l2Power(1) = CLng(2)
  173. m_l2Power(2) = CLng(4)
  174. m_l2Power(3) = CLng(8)
  175. m_l2Power(4) = CLng(16)
  176. m_l2Power(5) = CLng(32)
  177. m_l2Power(6) = CLng(64)
  178. m_l2Power(7) = CLng(128)
  179. m_l2Power(8) = CLng(256)
  180. m_l2Power(9) = CLng(512)
  181. m_l2Power(10) = CLng(1024)
  182. m_l2Power(11) = CLng(2048)
  183. m_l2Power(12) = CLng(4096)
  184. m_l2Power(13) = CLng(8192)
  185. m_l2Power(14) = CLng(16384)
  186. m_l2Power(15) = CLng(32768)
  187. m_l2Power(16) = CLng(65536)
  188. m_l2Power(17) = CLng(131072)
  189. m_l2Power(18) = CLng(262144)
  190. m_l2Power(19) = CLng(524288)
  191. m_l2Power(20) = CLng(1048576)
  192. m_l2Power(21) = CLng(2097152)
  193. m_l2Power(22) = CLng(4194304)
  194. m_l2Power(23) = CLng(8388608)
  195. m_l2Power(24) = CLng(16777216)
  196. m_l2Power(25) = CLng(33554432)
  197. m_l2Power(26) = CLng(67108864)
  198. m_l2Power(27) = CLng(134217728)
  199. m_l2Power(28) = CLng(268435456)
  200. m_l2Power(29) = CLng(536870912)
  201. m_l2Power(30) = CLng(1073741824)



  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. md5_FF a, b, c, d, x(k + 0), S11, &HD76AA478
  239. md5_FF d, a, b, c, x(k + 1), S12, &HE8C7B756
  240. md5_FF c, d, a, b, x(k + 2), S13, &H242070DB
  241. md5_FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
  242. md5_FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
  243. md5_FF d, a, b, c, x(k + 5), S12, &H4787C62A
  244. md5_FF c, d, a, b, x(k + 6), S13, &HA8304613
  245. md5_FF b, c, d, a, x(k + 7), S14, &HFD469501
  246. md5_FF a, b, c, d, x(k + 8), S11, &H698098D8
  247. md5_FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
  248. md5_FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
  249. md5_FF b, c, d, a, x(k + 11), S14, &H895CD7BE
  250. md5_FF a, b, c, d, x(k + 12), S11, &H6B901122
  251. md5_FF d, a, b, c, x(k + 13), S12, &HFD987193
  252. md5_FF c, d, a, b, x(k + 14), S13, &HA679438E
  253. md5_FF b, c, d, a, x(k + 15), S14, &H49B40821

  254. md5_GG a, b, c, d, x(k + 1), S21, &HF61E2562
  255. md5_GG d, a, b, c, x(k + 6), S22, &HC040B340
  256. md5_GG c, d, a, b, x(k + 11), S23, &H265E5A51
  257. md5_GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
  258. md5_GG a, b, c, d, x(k + 5), S21, &HD62F105D
  259. md5_GG d, a, b, c, x(k + 10), S22, &H2441453
  260. md5_GG c, d, a, b, x(k + 15), S23, &HD8A1E681
  261. md5_GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
  262. md5_GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
  263. md5_GG d, a, b, c, x(k + 14), S22, &HC33707D6
  264. md5_GG c, d, a, b, x(k + 3), S23, &HF4D50D87
  265. md5_GG b, c, d, a, x(k + 8), S24, &H455A14ED
  266. md5_GG a, b, c, d, x(k + 13), S21, &HA9E3E905
  267. md5_GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
  268. md5_GG c, d, a, b, x(k + 7), S23, &H676F02D9
  269. md5_GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A

  270. md5_HH a, b, c, d, x(k + 5), S31, &HFFFA3942
  271. md5_HH d, a, b, c, x(k + 8), S32, &H8771F681
  272. md5_HH c, d, a, b, x(k + 11), S33, &H6D9D6122
  273. md5_HH b, c, d, a, x(k + 14), S34, &HFDE5380C
  274. md5_HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
  275. md5_HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
  276. md5_HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
  277. md5_HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
  278. md5_HH a, b, c, d, x(k + 13), S31, &H289B7EC6
  279. md5_HH d, a, b, c, x(k + 0), S32, &HEAA127FA
  280. md5_HH c, d, a, b, x(k + 3), S33, &HD4EF3085
  281. md5_HH b, c, d, a, x(k + 6), S34, &H4881D05
  282. md5_HH a, b, c, d, x(k + 9), S31, &HD9D4D039
  283. md5_HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
  284. md5_HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
  285. md5_HH b, c, d, a, x(k + 2), S34, &HC4AC5665

  286. md5_II a, b, c, d, x(k + 0), S41, &HF4292244
  287. md5_II d, a, b, c, x(k + 7), S42, &H432AFF97
  288. md5_II c, d, a, b, x(k + 14), S43, &HAB9423A7
  289. md5_II b, c, d, a, x(k + 5), S44, &HFC93A039
  290. md5_II a, b, c, d, x(k + 12), S41, &H655B59C3
  291. md5_II d, a, b, c, x(k + 3), S42, &H8F0CCC92
  292. md5_II c, d, a, b, x(k + 10), S43, &HFFEFF47D
  293. md5_II b, c, d, a, x(k + 1), S44, &H85845DD1
  294. md5_II a, b, c, d, x(k + 8), S41, &H6FA87E4F
  295. md5_II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
  296. md5_II c, d, a, b, x(k + 6), S43, &HA3014314
  297. md5_II b, c, d, a, x(k + 13), S44, &H4E0811A1
  298. md5_II a, b, c, d, x(k + 4), S41, &HF7537E82
  299. md5_II d, a, b, c, x(k + 11), S42, &HBD3AF235
  300. md5_II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
  301. md5_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. ' MD5=LCase(WordToHex(b) & WordToHex(c)) 'I crop this to fit 16byte database password :D
  309. End Function

  310. Private Sub Command1_Click()
  311. Text2.Text = MD5(Text1.Text)
  312. End Sub
复制代码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享淘帖0 收藏收藏0 评分评分

0

主题

1

好友

119

积分

注册会员

Rank: 2

性别
保密
沙发
发表于 2012-6-6 02:18:30 |只看该作者
本帖最后由 sky_yx 于 2015-12-30 14:21 编辑

看看。。。。。。。。。。。。学习

回复

使用道具 评分 举报

您需要登录后才可以回帖 登录 | 立即注册


手机版|SKY外语计算机学习 ( 粤ICP备12031577 )    

GMT+8, 2024-12-23 00:35 , Processed in 0.105383 second(s), 26 queries .

回顶部