使用pynacl加密一个文件并解密另一个文件 - python

我有一些用Python 2.7在下面编写的代码,并使用pynacl在Mac OS X上运行。它目前的工作方式如下所述,它将加密密码,然后在以后解密。我想知道是否可以将解密的最后几行放在单独的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'))

python大神给出的解决方案

您可以使用泡菜:

加密脚本

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'))

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

Python:同时在for循环中添加到列表列表 - python

我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

查找字符串中的行数 - python

我正在创建一个python电影播放器​​/制作器,我想在多行字符串中找到行数。我想知道是否有任何内置函数或可以编写代码的函数来做到这一点:x = """ line1 line2 """ getLines(x) python大神给出的解决方案 如果换行符是'\n',则nlines …