[PYTHON] Encrypt and save data on jupyter and decrypt if necessary

This script was created with the idea that you would like to casually encrypt the password on the server, decrypt it, and use it.

#How to keep the password etc. encrypted on the server
#Save it with a common key and call it with jupyter.
#I wonder if it's okay for casual use

import gnupg
gpg = gnupg.GPG()

#Create text to save
conf = """
{'id':'foo',
'passwor':'bar'
}
"""
#Now text-encrypt with symmetric key bar
x = gpg.encrypt(conf, symmetric=True, passphrase='bar', encrypt=False)

#File test.Saved in txt Binary, utf-Make 8 texts.
f = open('test.txt','w')
f.write(x.data.decode('utf-8'))
f.close()

#Recall the saved file and decrypt it.
f = open('test.txt','r')
y = gpg.decrypt((f.read()), passphrase='bar')
conf = eval(y.data.decode('utf-8'))
print(conf)

Recommended Posts

Encrypt and save data on jupyter and decrypt if necessary
Save json data received by mosquitto on docker to db and Elasticsearch
Encrypt with Ruby (Rails) and decrypt with Python
Interactively visualize data with TreasureData, Pandas and Jupyter.
Find and delete memory-hungry variables on Jupyter (IPython)
Encrypt / decrypt data from golang using Azure KeyVault
Sort Fashion-MNIST data and save as PNG file
Try importing MLB data on Mac and Python