2017-03-14 51 views
0

我寫了一小段代碼來加密文件並對其進行解密。然而,它在某處被卡住,我認爲它是在它最初解密它之後,因爲它創建了加密文件,但不打印「加密!」。我究竟做錯了什麼?它是一個循環嗎?Python代碼不停止,在特定點處卡住

代碼:

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!') 
+0

我建議在加密函數中添加一些打印語句,看看它「卡住」的位置 – The4thIceman

+0

很難用'encrypt'中的縮進來判斷。請用'-tt'('python -tt script.py')運行腳本,並確保文章中出現的內容與文件內容匹配。 – Ryan

+0

(如果它匹配,這是因爲你的'未完成'只有一個聲明不會改變'已完成'。) – Ryan

回答

0

這可能只是一個錯誤在發佈代碼的問題,但看起來壓痕不正確:

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)) 

否則你永遠在

閱讀
+0

謝謝!這解決了最初的問題,但是現在代碼給了我新的錯誤:(Traceback(最近調用最後一次): 文件「C:/Users/saeed/IdeaProjects/encryptor/encryption.py」,第46行,在 加密(in_file,out_file,key,iv) 文件「C:/Users/saeed/IdeaProjects/encryptor/encryption.py」,第16行,加密 chunk + = padding_length * chr(padding_length) TypeError:can' t concat bytes to str) – SDurrani

+0

@SDurrani然後我會發佈一個關於新錯誤的新問題。我對加密和解密沒有那麼瞭解 – The4thIceman

+0

行!感謝您的幫助。 – SDurrani