USEncrypt® + JSON#

USEncrypt® is packaged with native support for writing to and reading from JSON files through a new encoding/decoding scheme for encrypted objects: USEncoder and USDecoder. These can be easily accessed by importing them to a project as follows:

>>> from usencrypt.json import USEncoder, USDecoder

Saving an Encrypted Object to a JSON File#

The following is an example of saving a list of encrypted strings to a JSON file using USEncrypt®’s USEncoder:

>>> import json
>>> import usencrypt as ue
>>> from usencrypt.json import USEncoder
>>> ue_str_1 = ue.encrypt('abc')
>>> ue_str_2 = ue.encrypt('def')
>>> ue_str_3 = ue.encrypt('ghi')
>>> ue_str_list = [ue_str_1, ue_str_2, ue_str_3]
>>> ue_str_list
[(0x7f5457d9fb80) String: 0x2e26dc538cfe173637e13d4fe42215ff14fab9acc1ca042045f9b8f2a22fdde80xe3a8d83902c3cbc905cfb92e6adccee1f1d05f01d207d90ec0d282b7516a80970x1599463df3b4400ce911c2b4893c2db5e658d690e9b015ca81e19fd2ad0018c1, (0x7f5436e0b040) String: 0x36056445748619939e6f8b16c2c3595e58739a09c846d689b2000b422eaaf74b0x1c92a0929e82b102fb2ede7a9b0c505a024ee1f89b31f8976418cf112f899b3b0x092d0466395b9ab5a09fbf00beadf61b1f388c39458214e77b1ac749a9f4577c, (0x7f5436ee0c70) String: 0x9c96ecaa10dfbc808dc5a6b5c79ae9c434e608ca974123fd3c0098e1020976a40x0b195782b971dc572fc98e0b4dbcf1de87a9263ea653bd7cce8ecf7133c1547c0x5967e8489707742a8afa4a871d818d9239b035480ec9ae4a1a6109ea4ed40535]
>>> with open ('output.json', 'w') as outfile:
...     json.dump(ue_str_list, outfile, indent=4, cls=USEncoder)

This produces the following output.json file:

[
    {
        "_usencrypt_type": "usencrypt.cipher._string.String",
        "hex": "0x2e26dc538cfe173637e13d4fe42215ff14fab9acc1ca042045f9b8f2a22fdde80xe3a8d83902c3cbc905cfb92e6adccee1f1d05f01d207d90ec0d282b7516a80970x1599463df3b4400ce911c2b4893c2db5e658d690e9b015ca81e19fd2ad0018c1"
    },
    {
        "_usencrypt_type": "usencrypt.cipher._string.String",
        "hex": "0x36056445748619939e6f8b16c2c3595e58739a09c846d689b2000b422eaaf74b0x1c92a0929e82b102fb2ede7a9b0c505a024ee1f89b31f8976418cf112f899b3b0x092d0466395b9ab5a09fbf00beadf61b1f388c39458214e77b1ac749a9f4577c"
    },
    {
        "_usencrypt_type": "usencrypt.cipher._string.String",
        "hex": "0x9c96ecaa10dfbc808dc5a6b5c79ae9c434e608ca974123fd3c0098e1020976a40x0b195782b971dc572fc98e0b4dbcf1de87a9263ea653bd7cce8ecf7133c1547c0x5967e8489707742a8afa4a871d818d9239b035480ec9ae4a1a6109ea4ed40535"
    }
]

Here, each entry contains a _usencrypt_type value, which specifies the type of encrypted object, and a hex value, which is later used to load the encrypted object back into a project using USDecoder.

Loading an Encrypted Object from a JSON File#

Once an encrypted object has been successfully saved in a JSON file, it can be loaded back into a project using USDecoder as follows:

>>> import json
>>> import usencrypt as ue
>>> from usencrypt.json import USDecoder
>>> g = open('output.json', 'r')
>>> loaded = json.loads(g.read(), cls=USDecoder)
>>> g.close()
>>> loaded
[(0x7f5457d9fb80) String: 0x2e26dc538cfe173637e13d4fe42215ff14fab9acc1ca042045f9b8f2a22fdde80xe3a8d83902c3cbc905cfb92e6adccee1f1d05f01d207d90ec0d282b7516a80970x1599463df3b4400ce911c2b4893c2db5e658d690e9b015ca81e19fd2ad0018c1, (0x7f5436e0b040) String: 0x36056445748619939e6f8b16c2c3595e58739a09c846d689b2000b422eaaf74b0x1c92a0929e82b102fb2ede7a9b0c505a024ee1f89b31f8976418cf112f899b3b0x092d0466395b9ab5a09fbf00beadf61b1f388c39458214e77b1ac749a9f4577c, (0x7f5436ee0c70) String: 0x9c96ecaa10dfbc808dc5a6b5c79ae9c434e608ca974123fd3c0098e1020976a40x0b195782b971dc572fc98e0b4dbcf1de87a9263ea653bd7cce8ecf7133c1547c0x5967e8489707742a8afa4a871d818d9239b035480ec9ae4a1a6109ea4ed40535]

Here, we load the output.json file and successfully save its contents in a new loaded object, which can later be used throughout the project like any other object.

For more information about working with JSON, please refer to Python’s JSON API.