Python base64 Crypto product encryption case

Source: Internet
Author: User
Tags base64 decrypt

- -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 ():

  def __init__ (self, Key, iv): Self.key = key Self.iv = IV Self.mode = AES. MODE_CBC self.bs = aes.block_size # complement 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) # Mesh The former AES-128 is sufficiently current to use ciphertext = Cryptor.encrypt (text) # to encrypt the string using base64 code return Base64.b64encode (ciphertext) # after decryption, Remove the top-up space with strip () to remove 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 (') ')  

Class mock ():
def Init(self, acct_id):
Key_len = 30
LISTSTR = [' http//', ' credit.wanda.cn ', '/credit-gw/service ']
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 '

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 datadef 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 = ' 5d8f090cb619624819a78e145266334a '
acct_id = ' Credit_test_acctid '
Acct_data =
Key = A2b_hex (key)
Req_data = Mock (acct_id). Format (acct_id, Acct_data)
Req_data = Json.dumps (req_data)
#print "Request Original:", Req_data
Req_data = Prpcrypt (key, IV). Encrypt (Req_data)
#print "Request ciphertext:", Req_data
Respon_data = Mock (acct_id). Send (Req_data)
#print "Response ciphertext:", 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 ']
Ifname= = 'Main‘:
IV = "0000000000000000"
Key = ' 5d8f090cb619624819a78e145266334a '
acct_id = ' Credit_test_acctid '
Acct_data = {"Name": "Chow", "Cardno": "512222197808120017", "Idtype": "101", "Reasoncode": "", "mobile": "13924652748", " QQ ":" [email protected] "," WeChat ":" dgfh0890 "," Weibo ":" [email protected] "}
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 "Request Original:", Req_data
Req_data = Prpcrypt (key, IV). Encrypt (Req_data)
#print "Request ciphertext:", Req_data
Respon_data = Mock (acct_id). Send (Req_data)
#print "Response ciphertext:", Respon_data
If respon_data! = None:
Respon_data = Prpcrypt (key, IV). Decrypt (Respon_data)
Respon_data = Json.loads (respon_data)
#print "Response Original:", Respon_data
If respon_data[' retcode '] = = ' 000000 ':
Print "Credit service running"
Else
Print "Fail"
Else
Print "Fail"

Python base64 Crypto product encryption case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.