关于Python实现Interface base64加解密方法
Posted Admin_sys
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于Python实现Interface base64加解密方法相关的知识,希望对你有一定的参考价值。
‘‘‘
以下Python Code运行环境为windows10,
Python版本为3.5.3
涉及的库:base64,json,unittest
‘‘‘
# coding=utf-8
# import requests
# import hashlib
import base64, json
import unittest
class DemoRequests(unittest.TestCase):
def setUp(self):
#self._url = ‘http://www.baidu.com‘
self.json_ = {
"username": "001",
"password": "001",
"uuid": "F9E73F13915A6283F4D9916C36E6D867"
}
def tearDown(self):
print("Request_test_End...")
# 加密json_字符串
def test_request_json_encryption(self):
# 把dict类型的json_转换成bytes字节流的str类型的json_
json_encryption = base64.b64encode(json.dumps(self.json_).encode(‘utf-8‘))
print(json_encryption)
return json_encryption
# 解密json_字符串
def test_request_json_Decrypt(self):
# --------------------------------------------------------------------------------------------------------
json_encryption = self.test_request_json_encryption()
# 把bytes字节流的str类型的json进行解密 decode(默认为utf-8)
# b‘{"password": "001", "username": "001", "uuid": "F9E73F13915A6283F4D9916C36E6D867"}‘
json_decrypt = base64.b64decode(json_encryption.decode(‘utf-8‘))
print(json_decrypt)
# --------------------------------------------------------------------------------------------------------
# 把解密后的bytes类型数据转换成str类型数据
# {"uuid": "F9E73F13915A6283F4D9916C36E6D867", "password": "001", "username": "001"}
json_encryption_str = json_decrypt.decode()
print(json_encryption_str)
# -------------------------------------------------------------------------------------------------------
# 得到str类型数据后,要转换成dict类型,才能取出某个key的values
json_encryption_dict = json.loads(json_encryption_str)
print(type(json_encryption_dict))
# -------------------------------------------------------------------------------------------------------
# 比如要取出"uuid"的value
json_encryption_dict_uuid = json_encryption_dict["uuid"]
print(json_encryption_dict_uuid)
if __name__ == ‘__main__‘:
unittest.main()
以上是关于关于Python实现Interface base64加解密方法的主要内容,如果未能解决你的问题,请参考以下文章
python:面向对象编程之Zope.interface安装使用