AES加密算法的网站:http://www.ssleye.com/aes_cipher.html
"""
AES加密(加解密算法/工作模式/填充方式:AES/ECB/PKCS5Padding;测试环境key值:WuMlCJRN8zO886dw
"""
api_DZFPKJ 代码如下:
1 #coding:utf-8 2 from get_urlencode import jm_urlencode 3 from get_Base64 import str_base64 4 from get_Sha1 import hash_hmac 5 from hashlib import sha1 6 import requests 7 import time 8 #定义参数 9 host = "http://test2.51fapiaonb.cn:4011/DZFP_Four" 10 data_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) 11 nsrsbh = "330201999999868" #这里填需要查询的纳税人识别号 12 DDLSH_str1 = "3302019999998" 13 DDLSH_str2 = "684935" 14 DDLSH = DDLSH_str1 + DDLSH_str2 15 ddh = "" 16 date = "2018-07-20" 17 KPLX = "1" #1 蓝票 2 红票 18 YFP_DM = "" 19 YFP_HM = "" 20 SGBZ = "" #传空 21 YHZCBS = "0" #优惠标识 22 LSLBS = "3" #0税率标志 23 ZZSTSGL = "" #内容 24 SPBM = "20300000" #免税 4030000000000000000 不征收 6010000000000000000 25 #所有商品行数据 26 KPHJJE = "0.1" 27 HJBHSJE = "0.1" 28 HJSE = "0" 29 #单个商品行数据 30 XMJE = "0.1" 31 SL = "0" 32 SE = "0" 33 #发票开具信息 34 mxs = "<FPKJXX_XMXX>" + 35 "<XMMC><![CDATA[1]]></XMMC>" + 36 "<XMDW></XMDW>" + 37 "<GGXH></GGXH>" + 38 "<XMSL>0</XMSL>" + 39 "<HSBZ>0</HSBZ>" + 40 "<XMDJ>0</XMDJ>" + 41 "<FPHXZ>0</FPHXZ>" + 42 "<SPBM>" + SPBM + "</SPBM>" + 43 "<ZXBM></ZXBM>" + 44 "<YHZCBS>" + YHZCBS + "</YHZCBS>" + 45 "<LSLBS>" + LSLBS + "</LSLBS>" + 46 "<ZZSTSGL>" + ZZSTSGL + "</ZZSTSGL>" + 47 "<XMJE>" + XMJE + "</XMJE>" + 48 "<SL>" + SL + "</SL>" + 49 "<SE>" + SE + "</SE>" + 50 "<KCE></KCE>" + 51 "<KCQHSJE></KCQHSJE>" + 52 "</FPKJXX_XMXX>" 53 54 #电子发票外部API-DZFPKJ(电子发票开具) 55 original_content = "<REQUEST_FPKJXX>" + 56 "<FPKJXX_FPTXX>" + 57 "<NSRSBH>" + nsrsbh + "</NSRSBH>" + 58 "<NSRMC>航信培训企业</NSRMC>" + 59 "<DDLSH>" + DDLSH + "</DDLSH>" + 60 "<DDH>" + ddh + "</DDH>" + 61 "<DDDATE>" + date + "</DDDATE>" + 62 "<BMB_BBH>13.0</BMB_BBH>" + 63 "<XHF_DZ><![CDATA[222]]></XHF_DZ>" + 64 "<XHF_DH><![CDATA[222]]></XHF_DH>" + 65 "<XHF_YHZH><![CDATA[222]]></XHF_YHZH>" + 66 "<GHFMC><![CDATA[22 2]]></GHFMC>" + 67 "<GHF_NSRSBH><![CDATA[330203999999018]]></GHF_NSRSBH>" + 68 "<GHF_DZ><![CDATA[地址]]></GHF_DZ>" + 69 "<GHF_GDDH><![CDATA[电话]]></GHF_GDDH>" + 70 "<GHF_SJ>17871838087</GHF_SJ>" + 71 "<GHF_EMAIL><![CDATA[zxy@ms.whb.ac.cn]]></GHF_EMAIL>" + 72 "<GHF_YHZH><![CDATA[yhzh]]></GHF_YHZH>" + 73 "<KPLY>999</KPLY>" + 74 "<KPY>综合服务公共能耗</KPY>" + 75 "<SKY>日常维修综合服务</SKY>" + 76 "<FHR>车位管理综合面积</FHR>" + 77 "<KPLX>" + KPLX + "</KPLX>" + 78 "<YFP_DM>" + YFP_DM + "</YFP_DM>" + 79 "<YFP_HM>" + YFP_HM + "</YFP_HM>" + 80 "<KPHJJE>" + KPHJJE + "</KPHJJE>" + 81 "<HJBHSJE>" + HJBHSJE + "</HJBHSJE>" + 82 "<HJSE>" + HJSE + "</HJSE>" + 83 "<BZ><![CDATA[公共能耗费,面积]]></BZ>" + 84 "<BB></BB>" + 85 "<WB></WB>" + 86 "<FPZL></FPZL>" + 87 "<SGBZ>" + SGBZ + "</SGBZ>" + 88 "</FPKJXX_FPTXX>" + 89 "<FPKJXX_XMXXS>" + 90 mxs + 91 "</FPKJXX_XMXXS>" + 92 "</REQUEST_FPKJXX>" 93 #AES密钥 94 key = 'WuMlCJRN8zO886dw' 95 # print(original_content) 96 #初始数据original_content进行AES加密后,得到original_content_Aes 97 # original_content_Aes = jm_Aes(key, original_content) 98 # print("content进行AES加密后: ", original_content_Ae 99 original_content_Aes = '9qyQm/1khrFeC+rlbwMd5/RaEIb+zZOLlS0yk+6kTlc8ZQLWS1VzwJGmbB1Qg4jetlFZYOAyeDXzNW5+8QIRZGLuTbvLwW5w40V/QeSXn+CsT0P+IWSyXmB6dJ6PQUXHYRn3Jh2UV4SMD1yx+C5SdSSW1D7dGpyMSPEIzuDxYdLEpoT21XXCSK9j0VvswRKhdYzMAEmpeGWxuqnyA2ARAojEREo/RBKj4ZLpH9yMkpueQ0YcgOQoosb38M9mumcuLfyBgkIKdEPYkgXyxPwFpm4IraXamKV9z6sjpMozgkPf5kW8e6dpLctqaWszILuZzmqha/MU3+4pLObonPdK4EUZ9zjdHPG8qwqLbtOE1RrjansSkDFq7pYlSYc0bceMWcb/zywR2MnpaVBfG6IwBjKlSUmYzarVq+UNhNGiyJQS0o6I9W5DpeRJwE8reSWLiItdZuU/MwrkYcrmn75W3KPDbL+Cqs/Rdy/v4JEQI19Ln7EkV9pnOHghS6X4yd/kCPSCnw5FaQzJiChf8tZRC4bUrgjKOZBpZM13aRu8nyXnrjAv8WyEZoISlvWHat2kI1A2pUEBCQRCOMSD62VlW6rBQsi52at+tJbxF1hw8tnuY3EEAbfHiQ6zyx5zJzhm6QhjqhaKWyf8rSOJcHga+5U5oB7qtzaAouK2us5oz25lO/sxDmui1UXooTUsN+9iRRn3ON0c8byrCotu04TVGnHj1hgooHvGVrVMbQsTHdaCXDqA5mUt/36CWOK6Hu+/q8nbTD1un4xuxIxsgly1TgmQkd+v8cbvTm29UJYt3tW2YgUlbjWILNVvUqfncIGgoaSCX33ma8AsBUSBR5cnE0HBNJmJCMmVnvCVddNAMST+/Ti0iUwkW1hsM4cSDatWwr7+iheiFYPznKh6GYbHOFocJkKcGJf4p/9rErtBXxCRHi8/Ce3jvT7XxCajQj7VCUvdd2pfi844ILFuAwrCSNwnblwlKLy+6fizrMBs6R2kwifRQcdvF4oiKT0Ni2yK/TNzEZfL4kTT/bYpkw4DcxBLhPgWP3T5lx9PJ4XunVAp5xblupvOfoxskhdjonTrCZYodTvEm3wCbKUbOij0GWlaERgf8bjrzBeZBSMKUKnpGkK8S8j9a29LsscqvFfGrGeSEsdk3URvahnM9qFuc+vf+bkX4cDmq09y7Ot1RQW6xN1fA9SrJP/73dfKo588QjpgRA4cSb6tPTbNGAbluu3jMTof+q+b7pGH8XR9a3YYzBi3ehk7sTo7SaOa50ifpUAh+HQHO1E70d5MMBcsh2sVbfPeicZU3sYhE+cKGxaW2CNM7mgasSmzyky5IuWpKgUOyL57eZejUAce5y4MWYgu1hBN+Y28uBcUhstapNiOfLBh8OBGSZrQByVQa8dBU/TO/hWUDnrS0JMWHqgRIJVsMPcszBo0ab0P8N7U3omkbX4nJ2xZ/oNlFIt9ObxRy0T0S7fGnfth4GHNZ5pzmepCxYc1KFvVdw8a9afLk3v4mVwi6bOnm4XDpgD0OdW8Lkgb9WWGbawDE1WbchGbSOa9xSpUA9VxTZvo4ZsKPEXe49aFLZctkeiuQ3JGqHKmsmrZUbtuD4EnajwxYs24OSfV7xsLI7T0ZrW9w5e1P3M=' 100 101 #初始数据original_content_Aes进行Base64加密后,得到original_content_Aes_Base64 102 original_content_Aes_Base64 = str_base64(original_content_Aes) 103 # print("content进行AES和Base64加密后: ", original_content_Aes_Base64) 104 105 # #通用报文初始数据(DZFPCX发票查询) 106 107 original_data = '<?xml version="1.0" encoding="utf-8"?>' + 108 "<interface>" + 109 "<globalInfo>" + 110 "<version>4.0</version>" + 111 "<interfaceCode>DZFPKJ</interfaceCode>" + 112 "<token>{}</token>".format(nsrsbh) + 113 "<requestTime>{}</requestTime>".format(data_time) + 114 "</globalInfo>" + 115 "<Data>" + 116 "<content>{}</content>".format(original_content_Aes) + 117 "</Data>" + 118 "</interface>" 119 120 #通用报文原始数据original_data进行Base64加密后,得到RequestParam,再进行urlencode解码 121 RequestParam_Base64 = str_base64(original_data) 122 # print("通用报文进行Base64加密后: ", RequestParam_Base64) 123 RequestParam_Base64_Urlencode = jm_urlencode(RequestParam_Base64) 124 # print("通用报文进行Base64和Urlencode加密后: ", RequestParam_Base64_Urlencode) 125 126 #RequestParam通过HMAC-SHA1加密,得到Signature 127 Signature = hash_hmac('I7GBo4w9E6dmGlLVJMaVtXrhbNqp4KBh', RequestParam_Base64, sha1) 128 # print("通用报文进行Base64和Hash_hmac加密后: ", Signature) 129 130 #请求体data构建 131 RequestParam = RequestParam_Base64_Urlencode 132 # url = host + "?RequestParam=" + RequestParam + "&Signature=" + Signature 133 data = "RequestParam=" + RequestParam + "&Signature=" + Signature 134 result = requests.post(url=host, params=data) 135 print(result.url) 136 print('发票查询返回报文为: {}'.format(result.text))
api_DZFPCX 代码如下:
1 #coding:utf-8 2 from get_Aes import jm_Aes 3 from get_urlencode import jm_urlencode 4 from get_Base64 import str_base64 5 from get_Sha1 import hash_hmac 6 from hashlib import sha1 7 import requests 8 import time 9 #定义参数 10 host = "http://test2.51fapiaonb.cn:4011/DZFP_Four" 11 data_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) 12 nsrsbh = "330201999999868" #这里填需要查询的纳税人识别号 13 # #电子发票外部API-DZFPCX(电子发票查询) 14 ddlsh = "3302019999998684935" #这里填写发票对应的订单流水号 15 original_content = "<FPXX>" + 16 "<NSRSBH>{}</NSRSBH>".format(nsrsbh) + 17 "<DDLSH>{}</DDLSH>".format(ddlsh) + "</FPXX>" 18 19 #AES密钥 20 key = 'WuMlCJRN8zO886dw' 21 22 #初始数据original_content进行AES加密后,得到original_content_Aes 23 original_content_Aes = jm_Aes(key, original_content) 24 # print("content进行AES加密后: ", original_content_Aes) 25 26 #初始数据original_content_Aes进行Base64加密后,得到original_content_Aes_Base64 27 original_content_Aes_Base64 = str_base64(original_content_Aes) 28 # print("content进行AES和Base64加密后: ", original_content_Aes_Base64) 29 30 # #通用报文初始数据(DZFPCX发票查询) 31 original_data = '<?xml version="1.0" encoding="utf-8"?>' + 32 "<interface>" + 33 "<globalInfo>" + 34 "<version>4.1</version>" + 35 "<interfaceCode>DZFPCX</interfaceCode>" + 36 "<token>" + nsrsbh + "</token>" + 37 "<requestTime>" + data_time + "</requestTime>" + 38 "</globalInfo>" + 39 "<Data>" + 40 "<content>" + original_content_Aes + "</content>" + 41 "</Data>" + 42 "</interface>" 43 44 #通用报文原始数据original_data进行Base64加密后,得到RequestParam,再进行urlencode解码 45 RequestParam_Base64 = str_base64(original_data) 46 # print("通用报文进行Base64加密后: ", RequestParam_Base64) 47 RequestParam_Base64_Urlencode = jm_urlencode(RequestParam_Base64) 48 # print("通用报文进行Base64和Urlencode加密后: ", RequestParam_Base64_Urlencode) 49 50 #RequestParam通过HMAC-SHA1加密,得到Signature 51 Signature = hash_hmac('I7GBo4w9E6dmGlLVJMaVtXrhbNqp4KBh', RequestParam_Base64, sha1) 52 # print("通用报文进行Base64和Hash_hmac加密后: ", Signature) 53 54 #请求体data构建 55 RequestParam = RequestParam_Base64_Urlencode 56 # url = host + "?RequestParam=" + RequestParam + "&Signature=" + Signature 57 data = "RequestParam=" + RequestParam + "&Signature=" + Signature 58 result = requests.post(url=host, params=data) 59 # print(result.url) #打印请求url 60 print('发票查询返回报文为: {}'.format(result.text))
str_AES_ECB加密算法:
1 #coding:utf-8 2 """ 3 AES加密(加解密算法/工作模式/填充方式:AES/ECB/PKCS5Padding;测试环境key值:WuMlCJRN8zO886dw 4 """ 5 import base64 6 from Crypto.Cipher import AES 7 # 补足字符串长度为16的倍数 8 def add_to_16(s): 9 while len(s) % 16 != 0: 10 s += (16 - len(s) % 16) * chr(16 - len(s) % 16) 11 return str.encode(s) # 返回bytes 12 13 def jm_Aes(key_input, text_input): 14 aes = AES.new(str.encode(key_input), AES.MODE_ECB) # 初始化加密器,本例采用ECB加密模式 15 encrypted_text = str(base64.encodebytes(aes.encrypt(add_to_16(text_input))), encoding='utf8').replace(' ', '') # 加密 16 # decrypted_text = aes.decrypt(base64.decodebytes(bytes(encrypted_text, encoding='utf8'))).decode("utf8") # 解密 17 # decrypted_text = decrypted_text[:-ord(decrypted_text[-1])] # 去除多余补位 18 # print('pkcs5加密值:', encrypted_text) 19 # print('pkcs5解密值:', decrypted_text) 20 return encrypted_text 21 22 if __name__ == '__main__': 23 key = 'WuMlCJRN8zO886dw' # 密码 24 text1 = '<FPXX><NSRSBH>330201999999868</NSRSBH><DDLSH>3302019999998682019112609102712218</DDLSH></FPXX>' # 待加密文本 25 # text2 = '<REQUEST_FPKJXX><FPKJXX_FPTXX><NSRSBH>330201999999868</NSRSBH><NSRMC>测试1</NSRMC><DDLSH>3302019999998684935</DDLSH><DDH></DDH><DDDATE>2018-07-20</DDDATE><BMB_BBH>13.0</BMB_BBH><XHF_DZ><![CDATA[222]]></XHF_DZ><XHF_DH><![CDATA[222]]></XHF_DH><XHF_YHZH><![CDATA[222]]></XHF_YHZH><GHFMC><![CDATA[22 2]]></GHFMC><GHF_NSRSBH><![CDATA[330203999999018]]></GHF_NSRSBH><GHF_DZ><![CDATA[地址]]></GHF_DZ><GHF_GDDH><![CDATA[电话]]></GHF_GDDH><GHF_SJ>17871838087</GHF_SJ><GHF_EMAIL><![CDATA[zxy@ms.whb.ac.cn]]></GHF_EMAIL><GHF_YHZH><![CDATA[yhzh]]></GHF_YHZH><KPLY>999</KPLY><KPY>综合服务公共能耗</KPY><SKY>日常维修综合服务</SKY><FHR>车位管理综合面积</FHR><KPLX>1</KPLX><YFP_DM></YFP_DM><YFP_HM></YFP_HM><KPHJJE>0.1</KPHJJE><HJBHSJE>0.1</HJBHSJE><HJSE>0</HJSE><BZ><![CDATA[公共能耗费,面积]]></BZ><BB></BB><WB></WB><FPZL></FPZL><SGBZ></SGBZ></FPKJXX_FPTXX><FPKJXX_XMXXS><FPKJXX_XMXX><XMMC><![CDATA[1]]></XMMC><XMDW></XMDW><GGXH></GGXH><XMSL>0</XMSL><HSBZ>0</HSBZ><XMDJ>0</XMDJ><FPHXZ>0</FPHXZ><SPBM>20300000</SPBM><ZXBM></ZXBM><YHZCBS>0</YHZCBS><LSLBS>3</LSLBS><ZZSTSGL></ZZSTSGL><XMJE>0.1</XMJE><SL>0</SL><SE>0</SE><KCE></KCE><KCQHSJE></KCQHSJE></FPKJXX_XMXX></FPKJXX_XMXXS></REQUEST_FPKJXX>' 26 print(jm_Aes(key, text1)) 27 pass
str_Base64加密算法:
1 #coding:utf-8 2 import base64 3 #对输入的字符进行base64加密 4 def str_base64(code): 5 jm_one = base64.b64encode(code.encode('utf-8')) 6 jm_two = str(jm_one, 'utf-8') 7 return jm_two 8 9 if __name__ == '__main__': 10 # reslut = str_base64('abcd1234') 11 # print(reslut) 12 pass
str_SHA1加密算法:
1 #coding:utf-8 2 from hashlib import sha1 3 import hmac 4 #对输入的字符进行Base64加密后,再进行hmac-sha1加密 5 def hash_hmac(key, code, sha1): 6 hmac_code = hmac.new(key.encode(), code.encode(), sha1) 7 return hmac_code.hexdigest() 8 9 if __name__ == '__main__': 10 # reslut = hash_hmac('I7GBo4w9E6dmGlLVJMaVtXrhbNqp4KBh', 'YWJjZDEyMzQ=', sha1) 11 # print(reslut) 12 pass
Python_Urlencode算法:
1 #coding=utf-8 2 from urllib import parse 3 4 def jm_urlencode(code): 5 s_encode = parse.quote(code, 'utf8') 6 return s_encode 7 8 if __name__ == '__main__': 9 # url = 'hhCWnmDtU1NxQlXiE+LMiRozGLJpbu2P/2hraeHuWUQdSUtwMmwlRlPzIJqI+lbiXind8vWCsOlZenQgb7JNNIdjqpEZK3gGQguik3hddTDMYL2GZ6NlNH4SL+NWXTVb' 10 # print(jm_urlencode(url)) 11 pass