python - PyCrypto AES suddenly stops decrypting after several lines of processing input -
i attempting encrypt , decrypt 5-14 mb text files 2 python files, 1 containing encrypt/decrypt methods , being runner methods.
here's class file:
from crypto.cipher import aes crypto import random import sys import codecs reload(sys) sys.setdefaultencoding('utf8') sys.stdout = codecs.getwriter('utf8')(sys.stdout) class crypt: def __init__(self, encrypted, key): self.message = encrypted self.key = key def encrypt(self): iv = random.new().read(aes.block_size) if len(self.message) % aes.block_size != 0: self.message += " " * (aes.block_size - (len(self.message) % aes.block_size)) lenkey = len(self.key) if lenkey > 32: self.key = self.key[:32] cipher = aes.new(self.key, aes.mode_cfb, iv) return iv + cipher.encrypt(self.message) def decrypt(self): iv, message = (self.message[:aes.block_size], self.message[aes.block_size:]) if len(message) % aes.block_size != 0: message += " " * (aes.block_size - (len(message) % aes.block_size)) lenkey = len(self.key) if lenkey > 32: self.key = self.key[:32] cipher = aes.new(self.key, aes.mode_cfb, iv) return cipher.decrypt(message).strip()
here's runner:
import crypt_classes crypto.hash import sha256 import sys reload(sys) sys.setdefaultencoding('utf8') h = sha256.new() while true: choice = raw_input("encrypt [1] or decrypt [2]? ").strip() if choice != "1" , choice != "2": print "invalid choice, please try again." continue key = "" first = "" confirm = "" while true: first = raw_input("enter passphrase (>= 32 chars): ").strip() # confirm = raw_input("confirm passphrase: ").strip() confirm = first if first == confirm , len(confirm) >= 32: h.update(first) key = h.hexdigest() break else: print "passphrases did not match or shorter 32 characters. try again." continue if choice == "1": f = open("encrypted.txt", "w") h = open("source.txt", "r") message = h.read() h.close() encrypter = crypt_classes.crypt(message, key) e = encrypter.encrypt() f.write(e) f.close() break elif choice == "2": f = open("encrypted.txt", "r") encrypted = f.read() f.close() decrypter = crypt_classes.crypt(encrypted, key) w = open("decrypted.txt", "w") o = decrypter.decrypt() w.write(o) w.close() break
encryption works fine, file decrypted , program ostensibly finishes, few lines shown in file along few miscellaneous characters, so:
# data file generated 23andme at: thu may 22 15:47:06 2008 # # below text version of data. fields tab-separated # each line corresponds single snp. each snp, provide identifier # (an rsid or internal id), lu�
no errors thrown, , have no idea why whole file (several mb) isn't decrypting. appreciate help, , please forgive sloppy code or lack of explanation; i'm new here. please comment if further elaboration required.
thank you!
just use simple functions. see no need class here.
taking hex digest , truncating makes effective keylength 128 while seem want 256.
don't write encrypted data utf8. don't use padding cfb mode.
my try (which works on system); can put encrypt , decrypt functions imports in separate file (module), if like.
from crypto.hash import sha256 crypto.cipher import aes crypto import random import sys def encrypt_data(data, key): iv=random.new().read(aes.block_size) aes=aes.new(key, aes.mode_cfb, iv) return iv+aes.encrypt(data) def decrypt_data(data, key): iv = data[:16] data = data[16:] aes=aes.new(key, aes.mode_cfb, iv) return aes.decrypt(data) h = sha256.new() while true: choice = raw_input("encrypt [1] or decrypt [2]? ").strip() if choice != "1" , choice != "2": print "invalid choice, please try again." continue key = "" first = "" confirm = "" while true: first = raw_input("enter passphrase (>= 32 chars): ").strip() # confirm = raw_input("confirm passphrase: ").strip() confirm = first if first == confirm , len(confirm) >= 32: h.update(first) key = h.digest() break else: print "passphrases did not match or shorter 32 characters. try again." continue if choice == "1": f = open("encrypteddna.txt", "w") h = open("genome_mikolaj_habryn_20080522154706.txt", "r") message = h.read() h.close() encmessage = encrypt_data(message, key) f.write(encmessage) f.close() break elif choice == "2": f = open("encrypteddna.txt", "r") encrypted = f.read() f.close() decrypted = decrypt_data(encrypted,key) w = open("decrypteddna.txt", "w") w.write(decrypted) w.close() break
~
Comments
Post a Comment