zoukankan      html  css  js  c++  java
  • 关于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()
     
  • 相关阅读:
    Microsoft Internet Explorer 多个不明细节远程代码执行漏洞
    给妈妈写程序
    平安夜,100篇
    我的DV,Sony HC90E
    听相声
    嘉陵江的石头,也许吧
    一步一个脚印-产品升级随笔(3)-学会拍板
    一步一个脚印-产品升级随笔(1)-vision
    《网站重构》网络资源总结
    一步一个脚印-产品升级随笔(2)-为什么需要BS架构的产品
  • 原文地址:https://www.cnblogs.com/mrchenyushen/p/8975441.html
Copyright © 2011-2022 走看看