今天有同学找到我说,能不能写一个简单的python md5加密模块,作为服务器端程序的响应函数。信息安全之类的问题了。md5采用单向加密机制。
直接上代码:
(k1, k2 是系统自动生成的随机整数,user_name是用户名)
def encrypt (a="user_name" , k1=1 , k2=1): #get system time b = time.strftime('%Y%m%d%H%M',time.localtime(time.time())) ka1 = k1; ka2 = k2 Str = list(a+b) Len = len(Str) array1 = [0]*Len; array2 = [0]*Len #re_str1, md5 digest of array1 array1 = [ chr( ord(Str[i]) ^ ka1) for i in range (0,Len)] tmp1 = ''.join(array1) re_str1 = hashlib.md5(tmp1.encode('utf-8')).hexdigest() re_str1 = list(re_str1) #re_str2, md5 digest of array2 array2 = [ chr( ord(Str[i]) ^ ka2) for i in range (0,Len)] tmp2 = ''.join(array2) re_str2 = hashlib.md5(tmp2.encode('utf-8')).hexdigest() re_str2 = list(re_str2) resultf = "" value = [ord(re_str1[i*4+3])^ord(re_str2[i*4+3]) for i in range(0,8)] for i in range (0,8): resultf += '%d'%(value[i]%10) return resultf if __name__ == "__main__": import string import hashlib import time print encrypt("te",13,23)
时间: 2024-11-08 20:10:34