Шифрование md5: как считать файл, зашифровать и записать - VB

Узнай цену своей работы

Формулировка задачи:

есть модуль шифрования md5
Листинг программы
  1. Attribute VB_Name = "basMD5"
  2. Option Explicit
  3. Option Base 0
  4. Private Const MD5_BLK_LEN As Long = 64
  5. ' Constants for MD5Transform routine
  6. Private Const S11 As Long = 7
  7. Private Const S12 As Long = 12
  8. Private Const S13 As Long = 17
  9. Private Const S14 As Long = 22
  10. Private Const S21 As Long = 5
  11. Private Const S22 As Long = 9
  12. Private Const S23 As Long = 14
  13. Private Const S24 As Long = 20
  14. Private Const S31 As Long = 4
  15. Private Const S32 As Long = 11
  16. Private Const S33 As Long = 16
  17. Private Const S34 As Long = 23
  18. Private Const S41 As Long = 6
  19. Private Const S42 As Long = 10
  20. Private Const S43 As Long = 15
  21. Private Const S44 As Long = 21
  22. ' Constants for unsigned word addition
  23. Private Const OFFSET_4 = 4294967296#
  24. Private Const MAXINT_4 = 2147483647
  25. ' TEST FUNCTIONS...
  26. ' MD5 test suite:
  27. ' MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
  28. ' MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
  29. ' MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
  30. ' MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
  31. ' MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
  32. ' MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
  33. ' d174ab98d277d9f5a5611c2c9f419d9f
  34. ' MD5 ("123456789012345678901234567890123456789012345678901234567890123456
  35. ' 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a
  36. ' MD5 (1 million x 'a') = 7707d6ae4e027c70eea2a935c2296f21
  37. Public Function Test_md5_abc()
  38. Debug.Print MD5_string("abc")
  39. End Function
  40. Public Function md5_test_suite()
  41. Debug.Print MD5_string("")
  42. Debug.Print MD5_string("a")
  43. Debug.Print MD5_string("abc")
  44. Debug.Print MD5_string("message digest")
  45. Debug.Print MD5_string("abcdefghijklmnopqrstuvwxyz")
  46. Debug.Print MD5_string("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
  47. Debug.Print MD5_string("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
  48. End Function
  49. Public Function test_md5_empty()
  50. Debug.Print MD5_string("")
  51. End Function
  52. Public Function test_md5_around64()
  53. Dim strMessage As String
  54. strMessage = "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
  55. Debug.Print MD5_string(strMessage)
  56. Debug.Print MD5_string(Left(strMessage, 65))
  57. Debug.Print MD5_string(Left(strMessage, 64))
  58. Debug.Print MD5_string(Left(strMessage, 63))
  59. Debug.Print MD5_string(Left(strMessage, 62))
  60. Debug.Print MD5_string(Left(strMessage, 57))
  61. Debug.Print MD5_string(Left(strMessage, 56))
  62. Debug.Print MD5_string(Left(strMessage, 55))
  63. End Function
  64. Public Function test_md5_million_a()
  65. ' This may take some time...
  66. Dim abMessage() As Byte
  67. Dim mLen As Long
  68. Dim i As Long
  69. mLen = 1000000
  70. ReDim abMessage(mLen - 1)
  71. For i = 0 To mLen - 1
  72. abMessage(i) = &H61 ' 0x61 = 'a'
  73. Next
  74. Debug.Print MD5_bytes(abMessage, mLen)
  75. End Function
  76. ' MAIN EXPORTED MD5 FUNCTIONS...
  77. Public Function MD5_string(strMessage As String) As String
  78. ' Returns 32-char hex string representation of message digest
  79. ' Input as a string (max length 2^29-1 bytes)
  80. Dim abMessage() As Byte
  81. Dim mLen As Long
  82. ' Cope with the empty string
  83. If Len(strMessage) > 0 Then
  84. abMessage = StrConv(strMessage, vbFromUnicode)
  85. ' Compute length of message in bytes
  86. mLen = UBound(abMessage) - LBound(abMessage) + 1
  87. End If
  88. MD5_string = MD5_bytes(abMessage, mLen)
  89. End Function
  90. Public Function MD5_bytes(abMessage() As Byte, mLen As Long) As String
  91. ' Returns 32-char hex string representation of message digest
  92. ' Input as an array of bytes of length mLen bytes
  93. Dim nBlks As Long
  94. Dim nBits As Long
  95. Dim block(MD5_BLK_LEN - 1) As Byte
  96. Dim state(3) As Long
  97. Dim wb(3) As Byte
  98. Dim sHex As String
  99. Dim index As Long
  100. Dim partLen As Long
  101. Dim i As Long
  102. Dim j As Long
  103. ' Catch length too big for VB arithmetic (268 million!)
  104. If mLen >= &HFFFFFFF Then Error 6 ' overflow
  105. ' Initialise
  106. ' Number of complete 512-bit/64-byte blocks to process
  107. nBlks = mLen \ MD5_BLK_LEN
  108. ' Load magic initialization constants
  109. state(0) = &H67452301
  110. state(1) = &HEFCDAB89
  111. state(2) = &H98BADCFE
  112. state(3) = &H10325476
  113. ' Main loop for each complete input block of 64 bytes
  114. index = 0
  115. For i = 0 To nBlks - 1
  116. Call md5_transform(state, abMessage, index)
  117. index = index + MD5_BLK_LEN
  118. Next
  119. ' Construct final block(s) with padding
  120. partLen = mLen Mod MD5_BLK_LEN
  121. index = nBlks * MD5_BLK_LEN
  122. For i = 0 To partLen - 1
  123. block(i) = abMessage(index + i)
  124. Next
  125. block(partLen) = &H80
  126. ' Make sure padding (and bit-length) set to zero
  127. For i = partLen + 1 To MD5_BLK_LEN - 1
  128. block(i) = 0
  129. Next
  130. ' Two cases: partLen is < or >= 56
  131. If partLen >= MD5_BLK_LEN - 8 Then
  132. ' Need two blocks
  133. Call md5_transform(state, block, 0)
  134. For i = 0 To MD5_BLK_LEN - 1
  135. block(i) = 0
  136. Next
  137. End If
  138. ' Append number of bits in little-endian order
  139. nBits = mLen * 8
  140. block(MD5_BLK_LEN - 8) = nBits And &HFF
  141. block(MD5_BLK_LEN - 7) = nBits \ &H100 And &HFF
  142. block(MD5_BLK_LEN - 6) = nBits \ &H10000 And &HFF
  143. block(MD5_BLK_LEN - 5) = nBits \ &H1000000 And &HFF
  144. ' (NB we don't try to cope with number greater than 2^31)
  145. ' Final padded block with bit length
  146. Call md5_transform(state, block, 0)
  147. ' Decode 4 x 32-bit words into 16 bytes with LSB first each time
  148. ' and return result as a hex string
  149. MD5_bytes = ""
  150. For i = 0 To 3
  151. Call uwSplit(state(i), wb(3), wb(2), wb(1), wb(0))
  152. For j = 0 To 3
  153. If wb(j) < 16 Then
  154. sHex = "0" & Hex(wb(j))
  155. Else
  156. sHex = Hex(wb(j))
  157. End If
  158. MD5_bytes = MD5_bytes & sHex
  159. Next
  160. Next
  161. End Function
  162. ' INTERNAL FUNCTIONS...
  163. Private Sub md5_transform(state() As Long, buf() As Byte, ByVal index As Long)
  164. ' Updates 4 x 32-bit values in state
  165. ' Input: the next 64 bytes in buf starting at offset index
  166. ' Assumes at least 64 bytes are present after offset index
  167. Dim a As Long
  168. Dim b As Long
  169. Dim c As Long
  170. Dim d As Long
  171. Dim j As Integer
  172. Dim x(15) As Long
  173. a = state(0)
  174. b = state(1)
  175. c = state(2)
  176. d = state(3)
  177. ' Decode the next 64 bytes into 16 words with LSB first
  178. For j = 0 To 15
  179. x(j) = uwJoin(buf(index + 3), buf(index + 2), buf(index + 1), buf(index))
  180. index = index + 4
  181. Next
  182. ' Round 1
  183. a = FF(a, b, c, d, x(0), S11, &HD76AA478) ' 1
  184. d = FF(d, a, b, c, x(1), S12, &HE8C7B756) ' 2
  185. c = FF(c, d, a, b, x(2), S13, &H242070DB) ' 3
  186. b = FF(b, c, d, a, x(3), S14, &HC1BDCEEE) ' 4
  187. a = FF(a, b, c, d, x(4), S11, &HF57C0FAF) ' 5
  188. d = FF(d, a, b, c, x(5), S12, &H4787C62A) ' 6
  189. c = FF(c, d, a, b, x(6), S13, &HA8304613) ' 7
  190. b = FF(b, c, d, a, x(7), S14, &HFD469501) ' 8
  191. a = FF(a, b, c, d, x(8), S11, &H698098D8) ' 9
  192. d = FF(d, a, b, c, x(9), S12, &H8B44F7AF) ' 10
  193. c = FF(c, d, a, b, x(10), S13, &HFFFF5BB1) ' 11
  194. b = FF(b, c, d, a, x(11), S14, &H895CD7BE) ' 12
  195. a = FF(a, b, c, d, x(12), S11, &H6B901122) ' 13
  196. d = FF(d, a, b, c, x(13), S12, &HFD987193) ' 14
  197. c = FF(c, d, a, b, x(14), S13, &HA679438E) ' 15
  198. b = FF(b, c, d, a, x(15), S14, &H49B40821) ' 16
  199. ' Round 2
  200. a = GG(a, b, c, d, x(1), S21, &HF61E2562) ' 17
  201. d = GG(d, a, b, c, x(6), S22, &HC040B340) ' 18
  202. c = GG(c, d, a, b, x(11), S23, &H265E5A51) ' 19
  203. b = GG(b, c, d, a, x(0), S24, &HE9B6C7AA) ' 20
  204. a = GG(a, b, c, d, x(5), S21, &HD62F105D) ' 21
  205. d = GG(d, a, b, c, x(10), S22, &H2441453) ' 22
  206. c = GG(c, d, a, b, x(15), S23, &HD8A1E681) ' 23
  207. b = GG(b, c, d, a, x(4), S24, &HE7D3FBC8) ' 24
  208. a = GG(a, b, c, d, x(9), S21, &H21E1CDE6) ' 25
  209. d = GG(d, a, b, c, x(14), S22, &HC33707D6) ' 26
  210. c = GG(c, d, a, b, x(3), S23, &HF4D50D87) ' 27
  211. b = GG(b, c, d, a, x(8), S24, &H455A14ED) ' 28
  212. a = GG(a, b, c, d, x(13), S21, &HA9E3E905) ' 29
  213. d = GG(d, a, b, c, x(2), S22, &HFCEFA3F8) ' 30
  214. c = GG(c, d, a, b, x(7), S23, &H676F02D9) ' 31
  215. b = GG(b, c, d, a, x(12), S24, &H8D2A4C8A) ' 32
  216. ' Round 3
  217. a = HH(a, b, c, d, x(5), S31, &HFFFA3942) ' 33
  218. d = HH(d, a, b, c, x(8), S32, &H8771F681) ' 34
  219. c = HH(c, d, a, b, x(11), S33, &H6D9D6122) ' 35
  220. b = HH(b, c, d, a, x(14), S34, &HFDE5380C) ' 36
  221. a = HH(a, b, c, d, x(1), S31, &HA4BEEA44) ' 37
  222. d = HH(d, a, b, c, x(4), S32, &H4BDECFA9) ' 38
  223. c = HH(c, d, a, b, x(7), S33, &HF6BB4B60) ' 39
  224. b = HH(b, c, d, a, x(10), S34, &HBEBFBC70) ' 40
  225. a = HH(a, b, c, d, x(13), S31, &H289B7EC6) ' 41
  226. d = HH(d, a, b, c, x(0), S32, &HEAA127FA) ' 42
  227. c = HH(c, d, a, b, x(3), S33, &HD4EF3085) ' 43
  228. b = HH(b, c, d, a, x(6), S34, &H4881D05) ' 44
  229. a = HH(a, b, c, d, x(9), S31, &HD9D4D039) ' 45
  230. d = HH(d, a, b, c, x(12), S32, &HE6DB99E5) ' 46
  231. c = HH(c, d, a, b, x(15), S33, &H1FA27CF8) ' 47
  232. b = HH(b, c, d, a, x(2), S34, &HC4AC5665) ' 48
  233. ' Round 4
  234. a = II(a, b, c, d, x(0), S41, &HF4292244) ' 49
  235. d = II(d, a, b, c, x(7), S42, &H432AFF97) ' 50
  236. c = II(c, d, a, b, x(14), S43, &HAB9423A7) ' 51
  237. b = II(b, c, d, a, x(5), S44, &HFC93A039) ' 52
  238. a = II(a, b, c, d, x(12), S41, &H655B59C3) ' 53
  239. d = II(d, a, b, c, x(3), S42, &H8F0CCC92) ' 54
  240. c = II(c, d, a, b, x(10), S43, &HFFEFF47D) ' 55
  241. b = II(b, c, d, a, x(1), S44, &H85845DD1) ' 56
  242. a = II(a, b, c, d, x(8), S41, &H6FA87E4F) ' 57
  243. d = II(d, a, b, c, x(15), S42, &HFE2CE6E0) ' 58
  244. c = II(c, d, a, b, x(6), S43, &HA3014314) ' 59
  245. b = II(b, c, d, a, x(13), S44, &H4E0811A1) ' 60
  246. a = II(a, b, c, d, x(4), S41, &HF7537E82) ' 61
  247. d = II(d, a, b, c, x(11), S42, &HBD3AF235) ' 62
  248. c = II(c, d, a, b, x(2), S43, &H2AD7D2BB) ' 63
  249. b = II(b, c, d, a, x(9), S44, &HEB86D391) ' 64
  250. state(0) = uwAdd(state(0), a)
  251. state(1) = uwAdd(state(1), b)
  252. state(2) = uwAdd(state(2), c)
  253. state(3) = uwAdd(state(3), d)
  254. End Sub
  255. ' FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4
  256. Private Function AddRotAdd(f As Long, a As Long, b As Long, x As Long, s As Integer, ac As Long) As Long
  257. ' Common routine for FF, GG, HH and II
  258. ' #define AddRotAdd(f, a, b, c, d, x, s, ac) { \
  259. ' (a) += f + (x) + (UINT4)(ac); \
  260. ' (a) = ROTATE_LEFT ((a), (s)); \
  261. ' (a) += (b); \
  262. ' }
  263. Dim temp As Long
  264. temp = uwAdd(a, f)
  265. temp = uwAdd(temp, x)
  266. temp = uwAdd(temp, ac)
  267. temp = uwRol(temp, s)
  268. AddRotAdd = uwAdd(temp, b)
  269. End Function
  270. Private Function FF(a As Long, b As Long, c As Long, d As Long, x As Long, s As Integer, ac As Long) As Long
  271. ' Returns new value of a
  272. ' #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  273. ' #define FF(a, b, c, d, x, s, ac) { \
  274. ' (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  275. ' (a) = ROTATE_LEFT ((a), (s)); \
  276. ' (a) += (b); \
  277. ' }
  278. Dim t As Long
  279. Dim t2 As Long
  280. ' F ((b), (c), (d)) = (((b) & (c)) | ((~b) & (d)))
  281. t = b And c
  282. t2 = (Not b) And d
  283. t = t Or t2
  284. FF = AddRotAdd(t, a, b, x, s, ac)
  285. End Function
  286. Private Function GG(a As Long, b As Long, c As Long, d As Long, x As Long, s As Integer, ac As Long) As Long
  287. ' #define G(b, c, d) (((b) & (d)) | ((c) & (~d)))
  288. Dim t As Long
  289. Dim t2 As Long
  290. t = b And d
  291. t2 = c And (Not d)
  292. t = t Or t2
  293. GG = AddRotAdd(t, a, b, x, s, ac)
  294. End Function
  295. Private Function HH(a As Long, b As Long, c As Long, d As Long, x As Long, s As Integer, ac As Long) As Long
  296. ' #define H(b, c, d) ((b) ^ (c) ^ (d))
  297. Dim t As Long
  298. t = b Xor c Xor d
  299. HH = AddRotAdd(t, a, b, x, s, ac)
  300. End Function
  301. Private Function II(a As Long, b As Long, c As Long, d As Long, x As Long, s As Integer, ac As Long) As Long
  302. ' #define I(b, c, d) ((c) ^ ((b) | (~d)))
  303. Dim t As Long
  304. t = b Or (Not d)
  305. t = c Xor t
  306. II = AddRotAdd(t, a, b, x, s, ac)
  307. End Function
  308. ' Unsigned 32-bit word functions suitable for VB/VBA
  309. Private Function uwRol(w As Long, s As Integer) As Long
  310. ' Return 32-bit word w rotated left by s bits
  311. ' avoiding problem with VB sign bit
  312. Dim i As Integer
  313. Dim t As Long
  314. uwRol = w
  315. For i = 1 To s
  316. t = uwRol And &H3FFFFFFF
  317. t = t * 2
  318. If (uwRol And &H40000000) <> 0 Then
  319. t = t Or &H80000000
  320. End If
  321. If (uwRol And &H80000000) <> 0 Then
  322. t = t Or &H1
  323. End If
  324. uwRol = t
  325. Next
  326. End Function
  327. Private Function uwJoin(a As Byte, b As Byte, c As Byte, d As Byte) As Long
  328. ' Join 4 x 8-bit bytes into one 32-bit word a.b.c.d
  329. uwJoin = ((a And &H7F) * &H1000000) Or (b * &H10000) Or (CLng(c) * &H100) Or d
  330. If a And &H80 Then
  331. uwJoin = uwJoin Or &H80000000
  332. End If
  333. End Function
  334. Private Sub uwSplit(ByVal w As Long, a As Byte, b As Byte, c As Byte, d As Byte)
  335. ' Split 32-bit word w into 4 x 8-bit bytes
  336. a = CByte(((w And &HFF000000) \ &H1000000) And &HFF)
  337. b = CByte(((w And &HFF0000) \ &H10000) And &HFF)
  338. c = CByte(((w And &HFF00) \ &H100) And &HFF)
  339. d = CByte((w And &HFF) And &HFF)
  340. End Sub
  341. Public Function uwAdd(wordA As Long, wordB As Long) As Long
  342. ' Adds words A and B avoiding overflow
  343. Dim myUnsigned As Double
  344. myUnsigned = LongToUnsigned(wordA) + LongToUnsigned(wordB)
  345. ' Cope with overflow
  346. '[2010-10-20] Changed from ">" to ">=". Thanks Loek.
  347. If myUnsigned >= OFFSET_4 Then
  348. myUnsigned = myUnsigned - OFFSET_4
  349. End If
  350. uwAdd = UnsignedToLong(myUnsigned)
  351. End Function
  352. '****************************************************
  353. ' These two functions from Microsoft Article Q189323
  354. ' "HOWTO: convert between Signed and Unsigned Numbers"
  355. Private Function UnsignedToLong(value As Double) As Long
  356. If value < 0 Or value >= OFFSET_4 Then Error 6 ' Overflow
  357. If value <= MAXINT_4 Then
  358. UnsignedToLong = value
  359. Else
  360. UnsignedToLong = value - OFFSET_4
  361. End If
  362. End Function
  363. Private Function LongToUnsigned(value As Long) As Double
  364. If value < 0 Then
  365. LongToUnsigned = value + OFFSET_4
  366. Else
  367. LongToUnsigned = value
  368. End If
  369. End Function
проблема такова не могу 1) считать строку из фала 2) зашифровать 3)записать в новый файл подкинте каких нибудь примеров или книжку или похожих кодов Попытался сделать выходит чушь какето
Листинг программы
  1. Private Sub Command3_Click()
  2. Dim strMessage As String
  3. strMessage = "строка шифрования"
  4. Debug.Print MD5_string(strMessage)
  5. SaveFile strMessage, TruePath(Text1)
  6. Me.Hide
  7. MsgBox "файл зашифрован"
  8. If Check1.value = 1 Then MsgBox "работает "
  9. End Sub

Решение задачи: «Шифрование md5: как считать файл, зашифровать и записать»

textual
Листинг программы
  1. Open TextBox1.Text For Input As #intFH

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы