encryption.py 1.5 KB
Newer Older
fanxun committed
1
import hashlib
taoke committed
2 3
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
taoke committed
4
from base64 import encodebytes
dengmaosheng committed
5 6 7 8 9
import time
import json
import requests


fanxun committed
10

taoke committed
11 12 13

# 加密类
class Encryption:
fanxun committed
14
    def get_md5(self, pwd, salt=None):
taoke committed
15 16
        """md5加密成32位小写"""
        md5 = hashlib.md5()
fanxun committed
17 18 19 20
        if salt:
            pwd = pwd + salt
        else:
            pwd = pwd
taoke committed
21 22
        if pwd:
            md5.update(pwd.encode('utf-8'))
taoke committed
23
            # print(md5.hexdigest().lower())
taoke committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
            return md5.hexdigest().lower()
        else:
            return ''

    def aes_cipher(self,key, aes_str):
        """AES采用ECB模式,使用PKCS7补偿"""
        aes = AES.new(key.encode('utf-8'), AES.MODE_ECB)
        pad_pkcs7 = pad(aes_str.encode('utf-8'), AES.block_size, style='pkcs7')  # 选择pkcs7补全
        encrypt_aes = aes.encrypt(pad_pkcs7)
        # 加密结果
        encrypted_text = str(encodebytes(encrypt_aes), encoding='utf-8')  # 解码
        encrypted_text_str = encrypted_text.replace("\n", "")
        # 此处我的输出结果老有换行符,所以用了临时方法将它剔除
        return encrypted_text_str
fanxun committed
38

dengmaosheng committed
39
    def aes_token(self,token):
dengmaosheng committed
40
        """斑马信用token加密"""
dengmaosheng committed
41
        key = "HIKEDL@#"
jiaqiying committed
42
        m5dkey = self.get_md5(key)
dengmaosheng committed
43
        token_plus_timestamp = token + str(int(round(time.time() * 1000)))
jiaqiying committed
44
        encrypted_token = self.aes_cipher(m5dkey, token_plus_timestamp)
dengmaosheng committed
45 46 47
        return encrypted_token


fanxun committed
48 49

if __name__ == '__main__':
dengmaosheng committed
50
    Encryption().get_md5('fx123456', 'hikcreate_xj')