---------------------------------封装关键字-------------------------------

说明:

某项目系统接口请求返回是AES加密的,先通过接口返回,再进行解密操作如下:

#获取url接口返回结果(加密的)

get_linfo.py
import  requests
def get_list_info(url):
headers = {
'content-type': 'application/json;charset=UTF-8',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36'
}
res=requests.post(url=url,headers=headers)
return res.text
 
 # 解密文件
 AES_response_jiemi.py

from Crypto.Cipher import AES
import base64,json

def aes_ECB_Decrypt(data, key): # ECB模式的解密函数,data为要传入密文,key为16字节密钥
  key = key.encode('utf-8')
  aes = AES.new(key=key, mode=AES.MODE_ECB) # 创建解密对象
  # decrypt AES解密 B64decode为base64 转码
  result = aes.decrypt(base64.b64decode(data))
  response_info=str(result,'utf-8') # 以字符串的形式返回
  #rstrip()方法用于删除字符串末尾的指定字符(默认为空格),通过返回数据来看有
  new_string = response_info.rstrip(response_info[-1])
  #输出json格式内容, 将 JSON 字符串解析为 Python 字典
  return(json.loads(new_string))



 

 

--------robotframework 编码-------------

1、先导入封装的关键字;

 2、进行编码,如下图:

image

运行结果(run)如下:

image

 

 

 -----扩展:

 通过aes_ECB_Decrypt 函数转码时,利用json.loads()输出内容报错,解析器读取了预期之外的额外数据,可能导致的原因如下:

  1、文件格式不正确:JSON文件可能不是有效的JSON格式,或者文件中包含了无法解析的字符或结构。
  2、文件包含非JSON内容:如果JSON文件中除了有效的JSON数据之外,还包含了其他文本(例如注释或额外的字符),这可能导致解码错误。
  3、错误的读取方式:如果JSON文件是一个数组,但被当作对象来解析,或者反之,这可能导致解码错误。

image

解决方法如下:

查资料后需用到rstrip进行末尾删除,然后再输出新内容(如有更好的方法,可以告诉我);

   response_info=str(result,'utf-8') # 以字符串的形式返回
  #rstrip()方法用于删除字符串末尾的指定字符(默认为空格),通过返回数据来看有
  new_string = response_info.rstrip(response_info[-1])
  #输出jsong格式内容
  return(json.loads(new_string))

image

 

 

--end--

 

posted on 2025-07-18 09:49  给天使看的戲  阅读(59)  评论(0)    收藏  举报