-->

python实现密码与时间戳的加密

1. 概述:

由于工作需要,要对用户的密码进行加密,由于仅是用一种加密方式(例如md5)比较容易被破解,故,我们进行了二次加密,代码如下

2.代码实现

import datetime
import hashlib  # 待加密信息

#加密方法
def encryption(pwd):
    """
    加密
    时间戳(16位)每个数字加6后转为16进制,共16位
    pwd加密为16为
    pwd16位在奇数位置,时间戳加密在偶数位
    :param pwd:
    :return:
    """
    time_stamp_int = int(datetime.datetime.now().timestamp() * 10 ** 6)
    time_stamp_str_lst = list(str(time_stamp_int))
    time_stamp_str_lst = list(map(lambda x: hex(int(x) + 6).__str__().replace('0x', ''), time_stamp_str_lst))
    hl = hashlib.md5()
    hl.update(pwd.encode("utf-8"))
    pwd_hl_lst = list(hl.hexdigest()[8:-8])
    result = ['0'] * 32
    result[0::2] = pwd_hl_lst
    result[1::2] = time_stamp_str_lst
    return ''.join(result)

#解密方法
def decrypt(pwd):
    assert len(pwd) == 32
    pwd_lst = list(pwd)
    time_stamp_str_lst = pwd_lst[1::2]
    time_stamp = int(int(''.join(list(map(lambda x: str(int(x, 16) - 6), time_stamp_str_lst)))) / 10 ** 6)
    pwd_de = ''.join(pwd_lst[0::2])
    return pwd_de, time_stamp

#测试加密与解密
print(datetime.datetime.now().timestamp().__str__())
pwd = encryption("aaa")
pwd_de, time_stamp = decrypt(pwd)

print(pwd_de, time_stamp)
posted @ 2023-05-30 09:31  ꧁ʚ星月天空ɞ꧂  阅读(487)  评论(0)    收藏  举报