SimpleCrypt Python错误 [英] SimpleCrypt Python Error
问题描述
我正在使用 simplecrypt
library 加密文件,但是我似乎无法以 simplecrypt
可以解码文件的方式读取该文件。
$ p $加密码:
从simplecrypt导入加密,解密
/ pre>
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:要解密的数据必须是字节;您不能使用字符串,因为没有字符串编码将接受所有可能的字符。
from simplecrypt import encrypt解密
def decrypt_file(file_name,key):
with open(file_name,'rb')as fo:
ciphertext = fo.read()
dec = decrypt ,key)
with open(file_name [: - 4],'wb')as fo:
fo.write(dec)
decrypt_file(test.txt.enc,securepass )
解决方案Aha ...次要错误 : - )
根据链接,
symplecrypt.encrypt
和simplecrypt.decrypt
是('密码',文字)
。在你的代码中,你有一个倒数((文本,键)
)。你正在传递文本加密/解密在第一个参数和第二个键。
工作示例:
从$ c
def encryption_file(file_name,key):
with open(file_name,'rb')as fo:
plaintext = fo.read()
print 文本加密:%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)
打印解密文本:%s%dec
with open(file_name [: - 4],'wb')as fo:
fo.write (dec)
如果__name__ ==__main__:
encrypt_file(test.txt,securepass)
decrypt_file(test.txt.enc, securepass)
I am using the
simplecrypt
library to encrypt a file, however I cannot seem to read the file in a way thatsimplecrypt
can decode it.Encryption code:
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")
This works fine and runs without any errors, however as soon as i try to decode it i get this error (using the below code)
simplecrypt.DecryptionException: Data to decrypt must be bytes; you cannot use a string because no string encoding will accept all possible characters.
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")
解决方案Aha... Minor mistake :-)
According to the docs in the link you provided in your question, the arguments to
symplecrypt.encrypt
andsimplecrypt.decrypt
are('password', text)
. In your code you've got that inverted ((text, key)
). You're passing the text to encrypt/decrypt in the first argument and the key in the second. Just reverse that order and will work.Working example:
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")
这篇关于SimpleCrypt Python错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!