前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python base64 crypto

python base64 crypto

作者头像
py3study
发布2020-01-10 17:19:52
6970
发布2020-01-10 17:19:52
举报
文章被收录于专栏:python3python3

-- coding: utf-8 --

from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex import json import urllib2 import time import random, string import base64 import argparse import sys import os import datetime class prpcrypt():

代码语言:javascript
复制
def __init__(self, key, iv):
    self.key = key
    self.iv = iv
    self.mode = AES.MODE_CBC
    self.BS = AES.block_size
    # 补位
    self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)
    self.unpad = lambda s: s[0:-ord(s[-1])]

def encrypt(self, text):
    a = (self.BS - len(text) % self.BS)
    b = chr(self.BS - len(text) % self.BS)
    text = self.pad(text)
    cryptor = AES.new(self.key, self.mode, self.iv)
    # 目前AES-128 足够目前使用
    ciphertext = cryptor.encrypt(text)
    # 把加密后的字符串使用base64编码
    return base64.b64encode(ciphertext)

# 解密后,去掉补足的空格用strip() 去掉
def decrypt(self, text):
    text = base64.b64decode(text)
    cryptor = AES.new(self.key, self.mode, self.iv)
    plain_text = cryptor.decrypt(text)
    return self.unpad(plain_text.rstrip('\0'))

class mock(): def init(self, acct_id): key_len = 30 listStr = ['http://','cdit.waa.cn','/crit-w/serce'] self.url = ''.join(listStr) self.headers = {'Content-type': 'application/json', 'X_WANDA_ACCT_ID': acct_id, 'Cache-Control': 'no-cache'} self.keylist = [random.choice(string.letters + string.digits) for i in range(key_len)] self.reqsn = "".join(self.keylist) self.reqtime = str(int(time.time())) + '000'

代码语言:javascript
复制
def format(self, acct_id, acct_data):
    data = {"req_time": self.reqtime, "acct_id": acct_id, "inf_id": "P_C_B016", "prod_id": "P_C_B016",
            "request_sn": self.reqsn,  "req_data": acct_data}
    return data

def send(self, data):
    req = urllib2.Request(self.url, data, self.headers)
    try:
        f = urllib2.urlopen(req)
    except urllib2.HTTPError, e:
        #print e.code
        #print e.read()
        return None
    else:
        return f.read()

start_time = datetime.datetime.now() iv = "0000000000000000" key = '5d8f090cb6196248145266334a' acct_id = 'CRT_TET_ACCTID' acct_data = key = a2b_hex(key) req_data = mock(acct_id).format(acct_id, acct_data) req_data = json.dumps(req_data) #print "请求原文:", req_data req_data = prpcrypt(key, iv).encrypt(req_data) #print "请求密文:", req_data respon_data = mock(acct_id).send(req_data) #print "响应密文:", respon_data #if respon_data != None: respon_data = prpcrypt(key, iv).decrypt(respon_data)

retcode = respon_data["retcode"]

print retcode

respon_data = json.loads(respon_data) retcode = respon_data["retcode"] end_time = datetime.datetime.now() #print end_time result_end = str(end_time - start_time) #print result_end response_sn = respon_data['response_sn'] if name == 'main': iv = "0000000000000000" key = '5d8f090cb6196245266334a' acct_id = 'CRIT_TET_ACCTID' acct_data = {"name": "周是", "cardNo": "51222219120017", "idType": "101", "reasonCode": "01","mobile": "13924623448", "qq": "544564567@qq.com", "wechat": "dgfh0890","weibo": "dgkhs0120@sina.com"} key = a2b_hex(key) req_data = mock(acct_id).format(acct_id, acct_data) req_sn = req_data['request_sn'] acct_id = req_data['acct_id'] req_data = json.dumps(req_data) #print "请求原文:", req_data req_data = prpcrypt(key, iv).encrypt(req_data) #print "请求密文:", req_data respon_data = mock(acct_id).send(req_data) #print "响应密文:", respon_data if respon_data != None: respon_data = prpcrypt(key, iv).decrypt(respon_data) respon_data = json.loads(respon_data) #print "响应原文:", respon_data if respon_data['retcode'] == '000000': print "credit service running" else: print "fail" else: print "fail"

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • -- coding: utf-8 --
  • retcode = respon_data["retcode"]
  • print retcode
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档