|
是这样的 我这个代码是测试[登录接口],需要把密码通过aes加密,然后进行签名.在最后调用登录接口,具体见以下截图.但是一直提示签名失败.麻烦大神帮我看看,万分感谢.
接口文档相关信息我截图如下:(我就是按照这个步骤来弄的)
代码如下:
import unittest
import hashlib
import requests
import os, sys
from Crypto.Cipher import AES
from Crypto import Random
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
# from db_fixture import test_data
app_master_secret="F0CED9E71C0D2798CFCEF0D3086F60";
#测试b2c登录接口
class b2cTest(unittest.TestCase):
def setUp(self):
self.base_url = "http://test.xxxxx.com/api/login/"
def tearDown(self):
print (self.result)
def test_example(self):
#加密
key = b"7BD87CCADE73F50B4918DCAD082938" # 16-bytes password
iv = Random.new().read(AES.block_size)
self.key = hashlib.sha256(key).digest()
obj = AES.new(self.key,AES.MODE_CBC,iv)
password_chushi = "a12345678"
password_chushi.encode("utf-8")
data = self.pkcs7padding(password_chushi)
password_value=obj.encrypt(data)
myParameters={'username': '15600002746', 'password': str(password_value)}
print(myParameters['username'])
print(myParameters['password'])
#签名
md5 = hashlib.md5()
sign_str = "post" + str(self.base_url) + str(myParameters['username']) + str(myParameters['password']) + app_master_secret # 拼接字符串
sign_str1 = sign_str.upper(); # 转换为大写
sign_bytes_utf8 = sign_str1.encode(encoding="utf-8")
md5.update(sign_bytes_utf8)
self.sign_md5 = md5.hexdigest()
#组装my_parameters
payload={"client-type": 2,
"device":"111",
"device-identifier":"222",
"app-version":"333",
"channel":"444",
"os-version": "555",
"timestamp":"666",
"my-parameters":str(myParameters),
"sign":self.sign_md5
}
#发送请求
r = requests.post(self.base_url,data=payload)
print (data)
self.result = r.json()
print(self.result['retCode'])
print (r.status_code)
print (self.result)
self.assertEqual(self.result['retCode'], 200)
def pkcs7padding(self, data):
bs = AES.block_size
padding = bs - len(data) % bs
padding_text = chr(padding) * padding
return data + padding_text
if __name__ == '__main__':
unittest.main()
服务器返回:{'retMsg': '签名验证失败', 'retCode': '40000'}
|
|