本次涉及三个网站:分别以编号 1 2 3 表示,文中所有 1 2 3 分别代表三个网站
1:https://www.jidaihome.com
2. http://www.qhce.gov.cn/login.jsp
3:https://web.ewt360.com/register/#/login?_k=zb7fzw
通过分析网站得知:
1:填充方式为 Pkcs7; 加密模式为 CBC, 返回结果需 base64编码
2:填充方式为 ZeroPadding; 加密模式为 CBC, 返回结果需 base64编码
3:填充方式为 Pkcs7; 加密模式为 CBC, 返回结果需 先转为16进制,在解码,然后全部转为大写
pad = lambda s: s + (16 - len(s)) * chr(16 - len(s)) # aes补全类型为 Pkcs7 时 需要 先进行补全
# aes补全类型为 ZeroPadding 时 需要 先进行补全 def add_to_16(s): while len(s) % 16 != 0: s += '\0' return s # 返回bytes
3: t.Encrypt = function(e) { var t = n["default"].enc.Utf8.parse(e); return n["default"].AES.encrypt(t, i, { iv: o, mode: n["default"].mode.CBC, padding: n["default"].pad.Pkcs7 }).ciphertext.toString().toUpperCase() } },
1: t.aesEncrypt = function(t, e) { var r = i.enc.Utf8.parse(e) , n = i.enc.Utf8.parse(t); return i.AES.encrypt(n, r, { iv: i.enc.Utf8.parse("0102030405060708"), mode: i.mode.CBC, padding: i.pad.Pkcs7 }).toString() }
不知道加密对不对可以在 http://tool.chacuo.net/cryptaes (一个在线加密解密网站)里带入key,vi, 加密模式, 补全模式,需加密数据,选择加密后数据返回格式看看和网页上所显示是否一样
代码如下:注释中 1 2 3 分别代表三个网站
# coding=utf-8 import binascii import hashlib, base64, math import random from binascii import b2a_hex, a2b_hex from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex import warnings warnings.filterwarnings("ignore") def hash_jia_mi(one_record): ''' md5 加密后 可以 降低内存 ''' hs = hashlib.md5() hs.update(one_record.encode("utf8")) result = hs.hexdigest() # print(result) return result def base64_jia_mi(a): result = base64.b64encode(a.encode('utf-8')) # print(str(result, 'utf-8')) return str(result, 'utf-8') def base64_jie_mi(a): result = base64.b64decode(a.encode('utf-8')) # print(str(result, 'utf-8')) return str(result, 'utf-8') def get_r(t): e = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" i = "" r = 0 while r < t: n = math.floor(random.random() * len(e)) i += e[n] r += 1 # print('i = ', i) return i # aes补全类型为 ZeroPadding 时 需要 先进行补全 def add_to_16(s): while len(s) % 16 != 0: s += '\0' return s # 返回bytes def AES_Encrypt(key, data): # 返回 最终 request payload 数据 # vi = '0102030405060708' # # 偏移量 --> 1 网站编号,下同 # vi = '2017110912453698' # 偏移量 --> 3 vi = '1234567812345678' # 偏移量 --> 2 pad = lambda s: s + (16 - len(s)) * chr(16 - len(s)) # aes补全类型为 Pkcs7 时 需要 先进行补全 print('pad = ', pad) # data = pad(data) data = add_to_16(data) print('data = ', data) # 字符串补位 cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8')) # print(cipher) # 加密结果 encryptedbytes = cipher.encrypt(data.encode('utf-8')) # # 加密后得到的是bytes类型的数据 # 最终返回数据 # --> 3 先转为16进制,在解码,然后全部转为大写 enctext = b2a_hex(encryptedbytes).decode().upper() # --> 1 先转为16进制,在解码,然后全部转为大写 encodestrs = base64.b64encode(encryptedbytes) # str yong base64加密后显示 enctext = encodestrs.decode('utf8') # 使用Base64进行编码,返回byte字符串 # --> 2 先转为16进制,在解码,然后全部转为大写 encodestrs = base64.b64encode(encryptedbytes) # str yong base64加密后显示 enctext = encodestrs.decode('utf8') # 使用Base64进行编码,返回byte字符串 print('enctext = ', enctext) # 对byte字符串按utf-8进行解码 return enctext # k = get_r(16) # 密钥 --> 1 # k = '20171109124536982017110912453698' # 密钥 --> 3 k = '1234567812345678' # 密钥 --> 2 # data = {"mobile": "%s" % phone, "password": "%s" % hash_jia_mi(pwd)} # 需要加密的数据 --> 1 # data = '666666' # 需要加密的数据 --> 3 data = 'yin1112324' # 需要加密的数据 --> 2 request_payload = AES_Encrypt(k, data) # 调用方法,传入 key(密钥) 和 需要加密的数据需注意对应模式及补全方法及加密后返回格式,否则结果不正确