AES加密算法的网站:http://www.ssleye.com/aes_cipher.html

"""
AES加密(加解密算法/工作模式/填充方式:AES/ECB/PKCS5Padding;测试环境key值:WuMlCJRN8zO886dw
"""

api_DZFPKJ 代码如下:

 #coding:utf-8
from get_urlencode import jm_urlencode
from get_Base64 import str_base64
from get_Sha1 import hash_hmac
from hashlib import sha1
import requests
import time
#定义参数
host = "http://test2.51fapiaonb.cn:4011/DZFP_Four"
data_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
nsrsbh = "" #这里填需要查询的纳税人识别号
DDLSH_str1 = ""
DDLSH_str2 = ""
DDLSH = DDLSH_str1 + DDLSH_str2
ddh = ""
date = "2018-07-20"
KPLX = "" #1 蓝票 2 红票
YFP_DM = ""
YFP_HM = ""
SGBZ = "" #传空
YHZCBS = "" #优惠标识
LSLBS = "" #0税率标志
ZZSTSGL = "" #内容
SPBM = "" #免税 4030000000000000000 不征收 6010000000000000000
#所有商品行数据
KPHJJE = "0.1"
HJBHSJE = "0.1"
HJSE = ""
#单个商品行数据
XMJE = "0.1"
SL = ""
SE = ""
#发票开具信息
mxs = "<FPKJXX_XMXX>" + \
"<XMMC><![CDATA[1]]></XMMC>" + \
"<XMDW></XMDW>" + \
"<GGXH></GGXH>" + \
"<XMSL>0</XMSL>" + \
"<HSBZ>0</HSBZ>" + \
"<XMDJ>0</XMDJ>" + \
"<FPHXZ>0</FPHXZ>" + \
"<SPBM>" + SPBM + "</SPBM>" + \
"<ZXBM></ZXBM>" + \
"<YHZCBS>" + YHZCBS + "</YHZCBS>" + \
"<LSLBS>" + LSLBS + "</LSLBS>" + \
"<ZZSTSGL>" + ZZSTSGL + "</ZZSTSGL>" + \
"<XMJE>" + XMJE + "</XMJE>" + \
"<SL>" + SL + "</SL>" + \
"<SE>" + SE + "</SE>" + \
"<KCE></KCE>" + \
"<KCQHSJE></KCQHSJE>" + \
"</FPKJXX_XMXX>" #电子发票外部API-DZFPKJ(电子发票开具)
original_content = "<REQUEST_FPKJXX>" + \
"<FPKJXX_FPTXX>" + \
"<NSRSBH>" + nsrsbh + "</NSRSBH>" + \
"<NSRMC>航信培训企业</NSRMC>" + \
"<DDLSH>" + DDLSH + "</DDLSH>" + \
"<DDH>" + ddh + "</DDH>" + \
"<DDDATE>" + date + "</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>" + KPLX + "</KPLX>" + \
"<YFP_DM>" + YFP_DM + "</YFP_DM>" + \
"<YFP_HM>" + YFP_HM + "</YFP_HM>" + \
"<KPHJJE>" + KPHJJE + "</KPHJJE>" + \
"<HJBHSJE>" + HJBHSJE + "</HJBHSJE>" + \
"<HJSE>" + HJSE + "</HJSE>" + \
"<BZ><![CDATA[公共能耗费,面积]]></BZ>" + \
"<BB></BB>" + \
"<WB></WB>" + \
"<FPZL></FPZL>" + \
"<SGBZ>" + SGBZ + "</SGBZ>" + \
"</FPKJXX_FPTXX>" + \
"<FPKJXX_XMXXS>" + \
mxs + \
"</FPKJXX_XMXXS>" + \
"</REQUEST_FPKJXX>"
#AES密钥
key = 'WuMlCJRN8zO886dw'
# print(original_content)
#初始数据original_content进行AES加密后,得到original_content_Aes
# original_content_Aes = jm_Aes(key, original_content)
# print("content进行AES加密后:\n", original_content_Ae
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=' #初始数据original_content_Aes进行Base64加密后,得到original_content_Aes_Base64
original_content_Aes_Base64 = str_base64(original_content_Aes)
# print("content进行AES和Base64加密后:\n", original_content_Aes_Base64) # #通用报文初始数据(DZFPCX发票查询) original_data = '<?xml version=\"1.0\" encoding=\"utf-8\"?>' + \
"<interface>" + \
"<globalInfo>" + \
"<version>4.0</version>" + \
"<interfaceCode>DZFPKJ</interfaceCode>" + \
"<token>{}</token>".format(nsrsbh) + \
"<requestTime>{}</requestTime>".format(data_time) + \
"</globalInfo>" + \
"<Data>" + \
"<content>{}</content>".format(original_content_Aes) + \
"</Data>" + \
"</interface>" #通用报文原始数据original_data进行Base64加密后,得到RequestParam,再进行urlencode解码
RequestParam_Base64 = str_base64(original_data)
# print("通用报文进行Base64加密后:\n", RequestParam_Base64)
RequestParam_Base64_Urlencode = jm_urlencode(RequestParam_Base64)
# print("通用报文进行Base64和Urlencode加密后:\n", RequestParam_Base64_Urlencode) #RequestParam通过HMAC-SHA1加密,得到Signature
Signature = hash_hmac('I7GBo4w9E6dmGlLVJMaVtXrhbNqp4KBh', RequestParam_Base64, sha1)
# print("通用报文进行Base64和Hash_hmac加密后:\n", Signature) #请求体data构建
RequestParam = RequestParam_Base64_Urlencode
# url = host + "?RequestParam=" + RequestParam + "&Signature=" + Signature
data = "RequestParam=" + RequestParam + "&Signature=" + Signature
result = requests.post(url=host, params=data)
print(result.url)
print('发票查询返回报文为:\n{}'.format(result.text))

api_DZFPCX 代码如下:

 #coding:utf-8
from get_Aes import jm_Aes
from get_urlencode import jm_urlencode
from get_Base64 import str_base64
from get_Sha1 import hash_hmac
from hashlib import sha1
import requests
import time
#定义参数
host = "http://test2.51fapiaonb.cn:4011/DZFP_Four"
data_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
nsrsbh = "" #这里填需要查询的纳税人识别号
# #电子发票外部API-DZFPCX(电子发票查询)
ddlsh = "" #这里填写发票对应的订单流水号
original_content = "<FPXX>" + \
"<NSRSBH>{}</NSRSBH>".format(nsrsbh) + \
"<DDLSH>{}</DDLSH>".format(ddlsh) + "</FPXX>" #AES密钥
key = 'WuMlCJRN8zO886dw' #初始数据original_content进行AES加密后,得到original_content_Aes
original_content_Aes = jm_Aes(key, original_content)
# print("content进行AES加密后:\n", original_content_Aes) #初始数据original_content_Aes进行Base64加密后,得到original_content_Aes_Base64
original_content_Aes_Base64 = str_base64(original_content_Aes)
# print("content进行AES和Base64加密后:\n", original_content_Aes_Base64) # #通用报文初始数据(DZFPCX发票查询)
original_data = '<?xml version=\"1.0\" encoding=\"utf-8\"?>' + \
"<interface>" + \
"<globalInfo>" + \
"<version>4.1</version>" + \
"<interfaceCode>DZFPCX</interfaceCode>" + \
"<token>" + nsrsbh + "</token>" + \
"<requestTime>" + data_time + "</requestTime>" + \
"</globalInfo>" + \
"<Data>" + \
"<content>" + original_content_Aes + "</content>" + \
"</Data>" + \
"</interface>" #通用报文原始数据original_data进行Base64加密后,得到RequestParam,再进行urlencode解码
RequestParam_Base64 = str_base64(original_data)
# print("通用报文进行Base64加密后:\n", RequestParam_Base64)
RequestParam_Base64_Urlencode = jm_urlencode(RequestParam_Base64)
# print("通用报文进行Base64和Urlencode加密后:\n", RequestParam_Base64_Urlencode) #RequestParam通过HMAC-SHA1加密,得到Signature
Signature = hash_hmac('I7GBo4w9E6dmGlLVJMaVtXrhbNqp4KBh', RequestParam_Base64, sha1)
# print("通用报文进行Base64和Hash_hmac加密后:\n", Signature) #请求体data构建
RequestParam = RequestParam_Base64_Urlencode
# url = host + "?RequestParam=" + RequestParam + "&Signature=" + Signature
data = "RequestParam=" + RequestParam + "&Signature=" + Signature
result = requests.post(url=host, params=data)
# print(result.url) #打印请求url
print('发票查询返回报文为:\n{}'.format(result.text))

str_AES_ECB加密算法:

 #coding:utf-8
"""
AES加密(加解密算法/工作模式/填充方式:AES/ECB/PKCS5Padding;测试环境key值:WuMlCJRN8zO886dw
"""
import base64
from Crypto.Cipher import AES
# 补足字符串长度为16的倍数
def add_to_16(s):
while len(s) % 16 != 0:
s += (16 - len(s) % 16) * chr(16 - len(s) % 16)
return str.encode(s) # 返回bytes def jm_Aes(key_input, text_input):
aes = AES.new(str.encode(key_input), AES.MODE_ECB) # 初始化加密器,本例采用ECB加密模式
encrypted_text = str(base64.encodebytes(aes.encrypt(add_to_16(text_input))), encoding='utf8').replace('\n', '') # 加密
# decrypted_text = aes.decrypt(base64.decodebytes(bytes(encrypted_text, encoding='utf8'))).decode("utf8") # 解密
# decrypted_text = decrypted_text[:-ord(decrypted_text[-1])] # 去除多余补位
# print('pkcs5加密值:', encrypted_text)
# print('pkcs5解密值:', decrypted_text)
return encrypted_text if __name__ == '__main__':
key = 'WuMlCJRN8zO886dw' # 密码
text1 = '<FPXX><NSRSBH>330201999999868</NSRSBH><DDLSH>3302019999998682019112609102712218</DDLSH></FPXX>' # 待加密文本
# 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>'
print(jm_Aes(key, text1))
pass

str_Base64加密算法:

 #coding:utf-8
import base64
#对输入的字符进行base64加密
def str_base64(code):
jm_one = base64.b64encode(code.encode('utf-8'))
jm_two = str(jm_one, 'utf-8')
return jm_two if __name__ == '__main__':
# reslut = str_base64('abcd1234')
# print(reslut)
pass

str_SHA1加密算法:

 #coding:utf-8
from hashlib import sha1
import hmac
#对输入的字符进行Base64加密后,再进行hmac-sha1加密
def hash_hmac(key, code, sha1):
hmac_code = hmac.new(key.encode(), code.encode(), sha1)
return hmac_code.hexdigest() if __name__ == '__main__':
# reslut = hash_hmac('I7GBo4w9E6dmGlLVJMaVtXrhbNqp4KBh', 'YWJjZDEyMzQ=', sha1)
# print(reslut)
pass

Python_Urlencode算法:

 #coding=utf-8
from urllib import parse def jm_urlencode(code):
s_encode = parse.quote(code, 'utf8')
return s_encode if __name__ == '__main__':
# url = 'hhCWnmDtU1NxQlXiE+LMiRozGLJpbu2P/2hraeHuWUQdSUtwMmwlRlPzIJqI+lbiXind8vWCsOlZenQgb7JNNIdjqpEZK3gGQguik3hddTDMYL2GZ6NlNH4SL+NWXTVb'
# print(jm_urlencode(url))
pass

api_DZFPKJ & api_DZFPCX的更多相关文章

  1. api_DZFPKJ & api_DZFPCX(get_AES_url代码优化)

    通过AES加密网站的接口来传值,不需要手动加密字符串后复制过来传值. #coding:utf-8 import requests import re def get_aes_url(key, text ...

随机推荐

  1. java-TreeMap

    2019-12-17 10:34:55 //返回小于key的第一个键: K lowerKey(K key); //返回大于key的第一个键: K higherKey(K key); //返回小于等于k ...

  2. PHP7内核(五):系统分析生命周期

    上篇文章讲述了模块初始化阶段之前的准备工作,本篇我来详细介绍PHP生命周期的五个阶段. 一.模块初始化阶段 我们先来看一下该阶段的每个函数的作用. 1.1.sapi_initialize_reques ...

  3. 【宇哥带你玩转MySQL】索引篇(一)索引揭秘,看他是如何让你的查询性能指数提升的

    场景复现,一个索引提高600倍查询速度? 首先准备一张books表 create table books( id int not null primary key auto_increment, na ...

  4. 封装一个通用的PopupWindow

    上篇文章是关于建造者设计模式的,今天顺便封装一个通用的 PopupWindow 来实践一下, 同时也方便以后使用 PopupWindow,本文将从下面几个方面来介绍 PopupWindow 及其封装, ...

  5. docker-compose搭建redis哨兵集群

    头脑风暴 出于学习目的,您可以很轻松地在docker环境下运行redis的单个实例,但是如果您需要在生产环境中运行它,那么必须将Redis部署为HA(High Avaliable)模式. Redis ...

  6. springboot集成通用mapper详细配置

    通常,我们利用mybatis写持久层方法.要么按照传统定义mapper方法,定义xml文件的方式,全部手写.要么需要通过mybatis-generator逆向工程插件生成大量的xxxExample文件 ...

  7. Sql练习201908200916

    表结构: 一条sql修改多条数据(Sql server),oracle,mysql请自行尝试: ; go 完成.

  8. python:列表生成式的学习

    看个例子: # 定义一个列表 l=[1,2,3,4,5] #()用于创建一个list,结果依次返回列表l的元素的平方,返回list s=[i*i for i in l] # 打印列表s print(s ...

  9. 基于Websocket开发的仿微信聊天室

    一.运行环境及涉及技术:----------------------------------* Visual Studio 2019* SQL SERVER 2008 R2* .Net FrameWo ...

  10. 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...