当前位置:   article > 正文

MD5加密--Python实现_python md5

python md5

1、简介

MD5消息摘要算法(MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。

2、特点

MD5是最常见的摘要算法,速度很快,生成结果是固定的128 bit 字节,通常用一个32位的16进制字符串表示。

MD5加密算法属于单向加密算法。MD5算法对比普通的加密算法缺少了解密的过程,它无法从密文(散列值)反过来得到原文。

单向加密算法是中在加密过程中不适应秘钥,将数据加密处理成加密数据,加密数据无法被解密。因为无法通过加密数据反向得到原来的内容,单向加密算法又被称为不可逆加密算法。单向加密算法一般使用哈希算法来生成密文,又称为哈希加密算法。

3、Python实现

  1. # -*- codeding = uft-8 -*-
  2. def int2bin(n, count=24):
  3. return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])
  4. class MD5(object):
  5. # 初始化密文
  6. def __init__(self, message):
  7. self.message = message
  8. self.ciphertext = ""
  9. self.A = 0x67452301
  10. self.B = 0xEFCDAB89
  11. self.C = 0x98BADCFE
  12. self.D = 0x10325476
  13. self.init_A = 0x67452301
  14. self.init_B = 0xEFCDAB89
  15. self.init_C = 0x98BADCFE
  16. self.init_D = 0x10325476
  17. '''
  18. self.A = 0x01234567
  19. self.B = 0x89ABCDEF
  20. self.C = 0xFEDCBA98
  21. self.D = 0x76543210
  22. '''
  23. #设置常数表T
  24. self.T = [0xD76AA478,0xE8C7B756,0x242070DB,0xC1BDCEEE,0xF57C0FAF,0x4787C62A,0xA8304613,0xFD469501,
  25. 0x698098D8,0x8B44F7AF,0xFFFF5BB1,0x895CD7BE,0x6B901122,0xFD987193,0xA679438E,0x49B40821,
  26. 0xF61E2562,0xC040B340,0x265E5A51,0xE9B6C7AA,0xD62F105D,0x02441453,0xD8A1E681,0xE7D3FBC8,
  27. 0x21E1CDE6,0xC33707D6,0xF4D50D87,0x455A14ED,0xA9E3E905,0xFCEFA3F8,0x676F02D9,0x8D2A4C8A,
  28. 0xFFFA3942,0x8771F681,0x6D9D6122,0xFDE5380C,0xA4BEEA44,0x4BDECFA9,0xF6BB4B60,0xBEBFBC70,
  29. 0x289B7EC6,0xEAA127FA,0xD4EF3085,0x04881D05,0xD9D4D039,0xE6DB99E5,0x1FA27CF8,0xC4AC5665,
  30. 0xF4292244,0x432AFF97,0xAB9423A7,0xFC93A039,0x655B59C3,0x8F0CCC92,0xFFEFF47D,0x85845DD1,
  31. 0x6FA87E4F,0xFE2CE6E0,0xA3014314,0x4E0811A1,0xF7537E82,0xBD3AF235,0x2AD7D2BB,0xEB86D391]
  32. #循环左移位数
  33. self.s = [7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
  34. 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
  35. 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
  36. 6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21]
  37. self.m = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  38. 1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12,
  39. 5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2,
  40. 0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9]
  41. # 附加填充位
  42. def fill_text(self):
  43. for i in range(len(self.message)):
  44. c = int2bin(ord(self.message[i]), 8)
  45. self.ciphertext += c
  46. if (len(self.ciphertext)%512 != 448):
  47. if ((len(self.ciphertext)+1)%512 != 448):
  48. self.ciphertext += '1'
  49. while (len(self.ciphertext)%512 != 448):
  50. self.ciphertext += '0'
  51. length = len(self.message)*8
  52. if (length <= 255):
  53. length = int2bin(length, 8)
  54. else:
  55. length = int2bin(length, 16)
  56. temp = length[8:12]+length[12:16]+length[0:4]+length[4:8]
  57. length = temp
  58. self.ciphertext += length
  59. while (len(self.ciphertext)%512 != 0):
  60. self.ciphertext += '0'
  61. # 分组处理(迭代压缩)
  62. def circuit_shift(self, x, amount):
  63. x &= 0xFFFFFFFF
  64. return ((x << amount) | (x >> (32 - amount))) & 0xFFFFFFFF
  65. def change_pos(self):
  66. a = self.A
  67. b = self.B
  68. c = self.C
  69. d = self.D
  70. self.A = d
  71. self.B = a
  72. self.C = b
  73. self.D = c
  74. def FF(self, mj, s, ti):
  75. mj = int(mj, 2)
  76. temp = self.F(self.B, self.C, self.D) + self.A + mj + ti
  77. temp = self.circuit_shift(temp, s)
  78. self.A = (self.B + temp)%pow(2, 32)
  79. self.change_pos()
  80. def GG(self, mj, s, ti):
  81. mj = int(mj, 2)
  82. temp = self.G(self.B, self.C, self.D) + self.A + mj + ti
  83. temp = self.circuit_shift(temp, s)
  84. self.A = (self.B + temp)%pow(2, 32)
  85. self.change_pos()
  86. def HH(self, mj, s, ti):
  87. mj = int(mj, 2)
  88. temp = self.H(self.B, self.C, self.D) + self.A + mj + ti
  89. temp = self.circuit_shift(temp, s)
  90. self.A = (self.B + temp)%pow(2, 32)
  91. self.change_pos()
  92. def II(self, mj, s, ti):
  93. mj = int(mj, 2)
  94. temp = self.I(self.B, self.C, self.D) + self.A + mj + ti
  95. temp = self.circuit_shift(temp, s)
  96. self.A = (self.B + temp)%pow(2, 32)
  97. self.change_pos()
  98. def F(self, X, Y, Z):
  99. return (X & Y) | ((~X) & Z)
  100. def G(self, X, Y, Z):
  101. return (X & Z) | (Y & (~Z))
  102. def H(self, X, Y, Z):
  103. return X ^ Y ^ Z
  104. def I(self, X, Y, Z):
  105. return Y ^ (X | (~Z))
  106. def group_processing(self):
  107. M = []
  108. for i in range(0, 512, 32):
  109. num = ""
  110. # 获取每一段的标准十六进制形式
  111. for j in range(0, len(self.ciphertext[i:i+32]), 4):
  112. temp = self.ciphertext[i:i+32][j:j + 4]
  113. temp = hex(int(temp, 2))
  114. num += temp[2]
  115. # 对十六进制进行小端排序
  116. num_tmp = ""
  117. for j in range(8, 0, -2):
  118. temp = num[j-2:j]
  119. num_tmp += temp
  120. num = ""
  121. for i in range(len(num_tmp)):
  122. num += int2bin(int(num_tmp[i], 16), 4)
  123. M.append(num)
  124. #print(M)
  125. for j in range(0, 16, 4):
  126. self.FF(M[self.m[j]], self.s[j], self.T[j])
  127. self.FF(M[self.m[j+1]], self.s[j+1], self.T[j+1])
  128. self.FF(M[self.m[j+2]], self.s[j+2], self.T[j+2])
  129. self.FF(M[self.m[j+3]], self.s[j+3], self.T[j+3])
  130. for j in range(0, 16, 4):
  131. self.GG(M[self.m[16+j]], self.s[16+j], self.T[16+j])
  132. self.GG(M[self.m[16+j+1]], self.s[16+j+1], self.T[16+j+1])
  133. self.GG(M[self.m[16+j+2]], self.s[16+j+2], self.T[16+j+2])
  134. self.GG(M[self.m[16+j+3]], self.s[16+j+3], self.T[16+j+3])
  135. for j in range(0, 16, 4):
  136. self.HH(M[self.m[32+j]], self.s[32+j], self.T[32+j])
  137. self.HH(M[self.m[32+j+1]], self.s[32+j+1], self.T[32+j+1])
  138. self.HH(M[self.m[32+j+2]], self.s[32+j+2], self.T[32+j+2])
  139. self.HH(M[self.m[32+j+3]], self.s[32+j+3], self.T[32+j+3])
  140. for j in range(0, 16, 4):
  141. self.II(M[self.m[48+j]], self.s[48+j], self.T[48+j])
  142. self.II(M[self.m[48+j+1]], self.s[48+j+1], self.T[48+j+1])
  143. self.II(M[self.m[48+j+2]], self.s[48+j+2], self.T[48+j+2])
  144. self.II(M[self.m[48+j+3]], self.s[48+j+3], self.T[48+j+3])
  145. self.A = (self.A+self.init_A)%pow(2, 32)
  146. self.B = (self.B+self.init_B)%pow(2, 32)
  147. self.C = (self.C+self.init_C)%pow(2, 32)
  148. self.D = (self.D+self.init_D)%pow(2, 32)
  149. '''
  150. print("A:{}".format(hex(self.A)))
  151. print("B:{}".format(hex(self.B)))
  152. print("C:{}".format(hex(self.C)))
  153. print("D:{}".format(hex(self.D)))
  154. '''
  155. answer = ""
  156. for register in [self.A, self.B, self.C, self.D]:
  157. register = hex(register)[2:]
  158. for i in range(8, 0, -2):
  159. answer += str(register[i-2:i])
  160. return answer
  161. # 测试
  162. # message = input("输入要加密的字符串:")
  163. # MD5 = MD5(message)
  164. # MD5.fill_text()
  165. # result = MD5.group_processing()
  166. # print("32位小写MD5加密:{}".format(result))
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/470078
推荐阅读
相关标签
  

闽ICP备14008679号