‘‘‘
The following Python code runtime environment is WINDOWS10,
Python version is 3.5.3
Libraries involved: Base64,json,unittest
‘‘‘
# Coding=utf-8# import Requests# import Hashlibimport base64, JSONImport UnitTestclass 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 ...")# Encrypt Json_ stringdef test_request_json_encryption (self):# Convert the dict type of Json_ to the bytes byte stream of the str type Json_json_encryption = Base64.b64encode (Json.dumps (Self.json_). Encode (' Utf-8 '))print (json_encryption)return json_encryption# Decrypt Json_ stringdef test_request_json_decrypt (self):# --------------------------------------------------------------------------------------------------------json_encryption = self.test_request_json_encryption ()# Decrypt the json of str type bytes byte stream decode (default = Utf-8)# b ' {"Password": "001", "username": "001", "UUID": "f9e73f13915a6283f4d9916c36e6d867"} 'Json_decrypt = Base64.b64decode (Json_encryption.decode (' Utf-8 '))print (Json_decrypt)# --------------------------------------------------------------------------------------------------------# Convert decrypted bytes type data to str type data# {"UUID": "f9e73f13915a6283f4d9916c36e6d867", "Password": "001", "username": "001"}json_encryption_str = Json_decrypt.decode ()print (JSON_ENCRYPTION_STR)# -------------------------------------------------------------------------------------------------------# After obtaining the STR type data, it is necessary to convert to the Dict type to remove the values of a key.json_encryption_dict = json.loads (json_encryption_str)print (Type (json_encryption_dict)) # -------------------------------------------------------------------------------------------------------# For example, to remove the "UUID" valuejson_encryption_dict_uuid = json_encryption_dict["uuid"]print (JSON_ENCRYPTION_DICT_UUID)if __name__ = = ' __main__ ':Unittest.main ()
On Python implementation of interface Base64 and decryption method