0

我正在尝试解密从c_text.

from cryptography.fernet import Fernet

def cipher_decrypt(c_text):
    key = open(key_loc,'rb')
    key.seek(0)
    key_b = key.read()
    print ('Key: ' + str(key_b))
    print ('\nCipher text: ' + str(c_text))
    cipher_suite = Fernet(key_b)
    key.close()
    plain_text = cipher_suite.decrypt(c_text).decode()
    print ('\nPlain text: ' + plain_text)
    return plain_text

调用时,应打印以下内容:

密钥:b'z7oCVMrxjjgx3n1HFI9oCkyxMnOrXekYKNMEBDKF704='

Cipher text: b'gAAAAABbQxMuhTmZGb0fgR6eRwQO9_qPv0tMI9GVVtyNZbHmDb6YY0veCrvG8uat5m_huC6ZHjI17V-HhLTrUGgdQGlwowY1t24cAq9ybJgfGeQVwWLsR_0=gAAAAABbQxMvgyBwq3hhMbLLP1VMbbboix4qw_TD0nF164TN2QqLGA5iHtX-dpEkj4ALMUY_dhYMqOfXY0ZUqIiX4Z_7Ud-EB8FHN0RsSiaiTBXHOS6_55A=gAAAAABbQxMwm8dek1OLeJp-sE6qmrXQSgbVqi3Sx2JwafW4YpTWuRJosBWJJpBFQ8zp8_rQ5rsLhhs7mQ4XwhGxND1GXmg8RZSrQ9-eclg6L5qyHH5Rch4='

纯文本:服务名称:gmail\n 用户名:gmail\n 密码:gmail\n\n

但是,它只打印纯文本的第一行。怎么了?

编辑:这是我写入文件的代码片段,以及加密功能。

            ser_name = pymsgbox.prompt("Enter service name")
            ser_name_s = 'Servicename: ' + str(ser_name) + '\n'
            manager = open(mng_name,'ab')
            manager.write(cipher_encrypt(ser_name_s))
            manager.close()
            acc_name = pymsgbox.prompt("Enter user name or email associated with " + str(ser_name))
            acc_name_s = 'Username/Email: ' + str(acc_name) + '\n'
            manager = open(mng_name,'ab')
            manager.write(cipher_encrypt(acc_name_s))
            manager.close()
            pw_name = pymsgbox.password("Enter password associated with " + str(acc_name))
            pw_name_s = 'Password: ' + str(pw_name) + '\n'
            manager = open(mng_name,'ab')
            manager.write(cipher_encrypt(pw_name_s))
            manager.close()

加密功能:

def cipher_encrypt(p_text):
    key = open(key_loc,'rb')
    key.seek(0)
    key_b = key.read()
    cipher_suite = Fernet(key_b)
    key.close()
    cipher_text = cipher_suite.encrypt(p_text.encode())
    return cipher_text

编辑2:经过一些测试,我注意到解密只发生在上面代码中的第一个写入命令(即使加密正常进行)。所以这肯定有问题,尽管我一辈子都想不通......

4

2 回答 2

0

.decode()通过从我认为的纯文本对象中删除操作,你会得到你想要的结果。或者,将解码后的版本从其最终字节序列转换为字符串。

这是一个演示版本:

>> from cryptography.fernet import Fernet
>> key = Fernet.generate_key()
>> f = Fernet(key)
>> token = f.encrypt(b"Servicename: gmail\n Username: gmail\n Password: gmail\n\n")
>> decrypted = f.decrypt(token)
>> decoded = decrypted.decode()
>> print(decrypted)
b'Servicename: gmail\n Username: gmail\n Password: gmail\n\n'
>> print(decoded)
Servicename: gmail
 Username: gmail
 Password: gmail

您只看到版本中的第一行的事实是终端正在解释换行符。我使用 iPython (Jupyter) 和两个版本的完整输出进行了测试。

于 2018-07-09T08:57:12.090 回答
0

在您的示例中,我实际上得到的文字更少。重现步骤:

>>> from cryptography.fernet import Fernet
>>> key = b'z7oCVMrxjjgx3n1HFI9oCkyxMnOrXekYKNMEBDKF704='
>>> cipher_text = b'gAAAAABbQxMuhTmZGb0fgR6eRwQO9_qPv0tMI9GVVtyNZbHmDb6YY0veCrvG8uat5m_huC6ZHjI17V-HhLTrUGgdQGlwowY1t24cAq9ybJgfGeQVwWLsR_0=gAAAAABbQxMvgyBwq3hhMbLLP1VMbbboix4qw_TD0nF164TN2QqLGA5iHtX-dpEkj4ALMUY_dhYMqOfXY0ZUqIiX4Z_7Ud-EB8FHN0RsSiaiTBXHOS6_55A=gAAAAABbQxMwm8dek1OLeJp-sE6qmrXQSgbVqi3Sx2JwafW4YpTWuRJosBWJJpBFQ8zp8_rQ5rsLhhs7mQ4XwhGxND1GXmg8RZSrQ9-eclg6L5qyHH5Rch4='
>>> cipher_suite = Fernet(key)
>>> decrypt_result = cipher_suite.decrypt(cipher_text)
>>> decrypt_result
b'Servicename: gmail\n'

据我所知,这个密文中似乎没有存储任何其他信息?

于 2018-07-09T08:59:04.730 回答