首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SimpleCrypt Python错误

SimpleCrypt Python错误
EN

Stack Overflow用户
提问于 2014-06-17 13:15:52
回答 1查看 4.7K关注 0票数 1

我正在使用图书馆加密文件,但是我似乎无法以simplecrypt可以解码的方式读取该文件。

加密码:

代码语言:javascript
复制
from simplecrypt import encrypt, decrypt
def encrypt_file(file_name, key):
    with open(file_name, 'rb') as fo:
        plaintext = fo.read()
    enc = encrypt(plaintext, key)
    with open(file_name + ".enc", 'wb') as fo:
        fo.write(enc)

encrypt_file("test.txt", "securepass")

这很好,运行时没有任何错误,但是一旦我试图解码它,我就会得到这个错误(使用下面的代码)。

simplecrypt.DecryptionException: Data to decrypt must be bytes; you cannot use a string because no string encoding will accept all possible characters.

代码语言:javascript
复制
from simplecrypt import encrypt, decrypt
def decrypt_file(file_name, key):
    with open(file_name, 'rb') as fo:
        ciphertext = fo.read()
    dec = decrypt(ciphertext, key)
    with open(file_name[:-4], 'wb') as fo:
        fo.write(dec)
decrypt_file("test.txt.enc", "securepass")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-17 13:28:59

啊哈。小错误:-)

根据您在问题中提供的链接文档,symplecrypt.encryptsimplecrypt.decrypt的参数是('password', text)。在您的代码中,您已经得到了倒置( (text, key) )。您将在第一个参数中传递用于加密/解密的文本,在第二个参数中传递密钥。只要倒序就行了。

工作实例:

代码语言:javascript
复制
from simplecrypt import encrypt, decrypt
def encrypt_file(file_name, key):
    with open(file_name, 'rb') as fo:
        plaintext = fo.read()
    print "Text to encrypt: %s" % plaintext
    enc = encrypt(key, plaintext)
    with open(file_name + ".enc", 'wb') as fo:
        fo.write(enc)

def decrypt_file(file_name, key):
    with open(file_name, 'rb') as fo:
        ciphertext = fo.read()
    dec = decrypt(key, ciphertext)
    print "decrypted text: %s" % dec
    with open(file_name[:-4], 'wb') as fo:
        fo.write(dec)

if __name__ == "__main__":
    encrypt_file("test.txt", "securepass")
    decrypt_file("test.txt.enc", "securepass")  
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24264874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档