LoginSignup
3
5

More than 5 years have passed since last update.

jupyter 上で、データを暗号化して保存し、必要に応じて復号する

Posted at

サーバ上にカジュアルに、パスワードを暗号化しておいておいて、復号化して使いたいというようなことを思ってつくったスクリプトです。

# サーバ上にパスワードなどを暗号化した状態でおく方法
# 共通鍵での保存しておいて、jupyter で呼び出します。
# カジュアルな利用ではまあいいかなと

import gnupg
gpg = gnupg.GPG()

# 保存するテキストを作成
conf = """
{'id':'foo',
'passwor':'bar'
}
"""
# これで共通鍵 bar でテキストで暗号化
x = gpg.encrypt(conf, symmetric=True, passphrase='bar', encrypt=False)

# ファイル test.txt に保存 バイナリーなんで、utf-8 のテキスト化行います。
f = open('test.txt','w')
f.write(x.data.decode('utf-8'))
f.close()

# 保存したファイルを呼び出して復号します。
f = open('test.txt','r')
y = gpg.decrypt((f.read()), passphrase='bar')
conf = eval(y.data.decode('utf-8'))
print(conf)

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5