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()
     
  • 相关阅读:
    Python数据结构与算法—栈
    var_export 和 var_dump
    PHp 下标是 区分大小写的
    和眼睛相处
    css 3 animation
    background-attachment: fixed | attachment 区别
    js 函数表达和函数声明
    function 和 new Function
    lastIndexOf js
    substring substr slice js比较
  • 原文地址:https://www.cnblogs.com/mrchenyushen/p/8975441.html
Copyright © 2011-2022 走看看