Python代码没有停下来,陷入某个特定的地步(Python code not stopping, getting stuck at a certain point)
我写了一小段代码来加密文件并对其进行解密。 然而,它在某处被卡住,我认为它是在它最初解密它之后,因为它创建了加密文件,但不打印“加密!”。 我究竟做错了什么? 它是一个循环吗?
码:
from hashlib import md5 from Crypto import Random from Crypto.Cipher import AES import os from Crypto import * def encrypt(in_file, out_file, key, iv): bs = AES.block_size cipher = AES.new(key, AES.MODE_CBC, iv) finished = False print('check001') while not finished: chunk = in_file.read(1024 * bs) print('check002') if len(chunk) == 0 or len(chunk) % bs != 0: padding_length = bs - (len(chunk) % bs) chunk += padding_length * chr(padding_length) finished = True out_file.write(cipher.encrypt(chunk)) print('check003') def decrypt(in_file, out_file, key, iv): bs = AES.block_size cipher = AES.new(key, AES.MODE_CBC, iv) next_chunk = '' finished = False while not finished: chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs)) if len(next_chunk) == 0: padding_length = ord(chunk[-1]) if padding_length < 1 or padding_length > bs: raise ValueError("bad decrypt pad (%d)" % padding_length) if chunk[-padding_length:] != (padding_length * chr(padding_length)): raise ValueError("bad decrypt") chunk = chunk[:-padding_length] finished = True out_file.write(chunk) encFile= input('Enter the path of the file you would like to encrypt: ') doneFile= encFile + '.enc' unFile = encFile + '.dec' in_file = open(encFile, 'rb') print('check004') out_file = open(doneFile, 'wb') print('check005') key = os.urandom(32) iv = os.urandom(16) print('check006') encrypt(in_file, out_file, key, iv) print('check007') in_file.close() out_file.close() print('Encrypted!') in_file = open(doneFile, 'rb') out_file = open(unFile, 'wb') decrypt(in_file, out_file, key, iv) in_file.close() out_file.close() print('Decrypted!')I have written a small piece of code to encrypt a file and decrypt it. However It is getting stuck somewhere, I think it is after it initially decrypts it as it creates the encrypted file but does not print 'Encrypted!'. What am I doing wrong? Is it one of the loops?
Code:
from hashlib import md5 from Crypto import Random from Crypto.Cipher import AES import os from Crypto import * def encrypt(in_file, out_file, key, iv): bs = AES.block_size cipher = AES.new(key, AES.MODE_CBC, iv) finished = False print('check001') while not finished: chunk = in_file.read(1024 * bs) print('check002') if len(chunk) == 0 or len(chunk) % bs != 0: padding_length = bs - (len(chunk) % bs) chunk += padding_length * chr(padding_length) finished = True out_file.write(cipher.encrypt(chunk)) print('check003') def decrypt(in_file, out_file, key, iv): bs = AES.block_size cipher = AES.new(key, AES.MODE_CBC, iv) next_chunk = '' finished = False while not finished: chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs)) if len(next_chunk) == 0: padding_length = ord(chunk[-1]) if padding_length < 1 or padding_length > bs: raise ValueError("bad decrypt pad (%d)" % padding_length) if chunk[-padding_length:] != (padding_length * chr(padding_length)): raise ValueError("bad decrypt") chunk = chunk[:-padding_length] finished = True out_file.write(chunk) encFile= input('Enter the path of the file you would like to encrypt: ') doneFile= encFile + '.enc' unFile = encFile + '.dec' in_file = open(encFile, 'rb') print('check004') out_file = open(doneFile, 'wb') print('check005') key = os.urandom(32) iv = os.urandom(16) print('check006') encrypt(in_file, out_file, key, iv) print('check007') in_file.close() out_file.close() print('Encrypted!') in_file = open(doneFile, 'rb') out_file = open(unFile, 'wb') decrypt(in_file, out_file, key, iv) in_file.close() out_file.close() print('Decrypted!')最满意答案
将代码发布到问题可能只是一个错误,但缩进看起来不正确:
while not finished: chunk = in_file.read(1024 * bs) if len(chunk) == 0 or len(chunk) % bs != 0: padding_length = bs - (len(chunk) % bs) chunk += padding_length * chr(padding_length) finished = True out_file.write(cipher.encrypt(chunk))不应该将if语句缩写为:
while not finished: chunk = in_file.read(1024 * bs) if len(chunk) == 0 or len(chunk) % bs != 0: padding_length = bs - (len(chunk) % bs) chunk += padding_length * chr(padding_length) finished = True out_file.write(cipher.encrypt(chunk))否则你永远在读书
This might just be a mistake in posting the code to the question, but the indentation looks incorrect:
while not finished: chunk = in_file.read(1024 * bs) if len(chunk) == 0 or len(chunk) % bs != 0: padding_length = bs - (len(chunk) % bs) chunk += padding_length * chr(padding_length) finished = True out_file.write(cipher.encrypt(chunk))shouldn't the if statement be indented to be:
while not finished: chunk = in_file.read(1024 * bs) if len(chunk) == 0 or len(chunk) % bs != 0: padding_length = bs - (len(chunk) % bs) chunk += padding_length * chr(padding_length) finished = True out_file.write(cipher.encrypt(chunk))otherwise you are forever reading in
更多推荐
发布评论