1

以下にPython 2.7で書いたコードがいくつかあり、mac os xで実行されているpynaclを使用しています。現在、以下のように動作し、パスワードを暗号化し、後で復号化します。復号化する最後の数行が別のpythonファイルにある可能性があるかどうか知りたいですか? 別の python ファイルは、毎日実行される cronjob であり、実行するにはパスワードが必要です。これが、復号化部分をファイル #2 に配置する必要がある理由です。提案があれば教えてください。

ファイル #1 をファイル #2 にインポートし、さらにファイル #1 の必要な変数をファイルに保存しようとしましたが、「SealedBox」はエラー「TypeError: 引数 1 はバッファに変換可能でなければなりません。 SealedBox ではありません」

#!/usr/bin/env python2


import nacl.utils
from nacl.public import PrivateKey, SealedBox
import getpass

# Generate Bob's private key, as we've done in the Box example
skbob = PrivateKey.generate()
pkbob = skbob.public_key

# Alice wishes to send a encrypted message to Bob,
# but prefers the message to be untraceable
sealed_box = SealedBox(pkbob)

# This is Alice's message
message = getpass.getpass("LDAP Password is:")

# Encrypt the message, it will carry the ephemeral key public part
# to let Bob decrypt it
encrypted = sealed_box.encrypt(message)

# Store the data with binary mode:
# with open('file.bin', 'wb') as f:
#   f.write(encrypted)

unseal_box = SealedBox(skbob)

# with open('file2.bin', 'wb') as f:
#   f.write(unseal_box)

# decrypt the received message, this is where File #2 would start
plaintext = unseal_box.decrypt(encrypted)
print(plaintext.decode('utf-8'))
4

1 に答える 1

0

ピクルスを使用できます:

暗号化スクリプト

from nacl.public import PrivateKey, SealedBox
import getpass
import pickle

# Generate Bob's private key, as we've done in the Box example
skbob = PrivateKey.generate()
pkbob = skbob.public_key

# Alice wishes to send a encrypted message to Bob,
# but prefers the message to be untraceable
sealed_box = SealedBox(pkbob)

# This is Alice's message
message = getpass.getpass("LDAP Password is:")

# Encrypt the message, it will carry the ephemeral key public part
# to let Bob decrypt it
encrypted = sealed_box.encrypt(message.encode())

# Store the data with binary mode:
with open('file.bin', 'wb') as f:
    pickle.dump(encrypted, f)
with open('file2.bin', 'wb') as f:
    pickle.dump(skbob, f)

復号化スクリプト

from nacl.public import SealedBox
import pickle

with open('file.bin', 'rb') as f:
    encrypted = pickle.load(f)
with open('file2.bin', 'rb') as f:
    skbob = pickle.load(f)

unseal_box = SealedBox(skbob)
# decrypt the received message, this is where File #2 would start
plaintext = unseal_box.decrypt(encrypted)
print(plaintext.decode('utf-8'))
于 2019-05-10T23:40:42.270 に答える