2017-03-14 2 views
0

Ich habe ein kleines Stück Code geschrieben, um eine Datei zu verschlüsseln und zu entschlüsseln. Allerdings bleibt es irgendwo hängen, ich denke es ist, nachdem es es zuerst entschlüsselt hat, da es die verschlüsselte Datei erstellt, aber "verschlüsselt" nicht druckt. Was mache ich falsch? Ist es eine der Schleifen?Python-Code nicht zu stoppen, an einem bestimmten Punkt stecken bleiben

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

Ich würde empfehlen, einige print-Anweisungen in der Verschlüsselungsfunktion hinzuzufügen, um zu sehen, wo genau es "stecken bleibt" – The4thIceman

+0

Es ist schwer zu sagen mit der Einrückung in "verschlüsseln". Bitte führen Sie Ihr Skript mit '-tt' (' python -tt script.py') aus und stellen Sie sicher, dass das, was in Ihrem Post erscheint, mit dem Inhalt der Datei übereinstimmt. – Ryan

+0

(Wenn es passt, dann ist es, weil Ihre 'noch nicht fertig 'nur eine Anweisung enthält, die nicht' fertig 'ändert.) – Ryan

Antwort

0

Dies nur einen Fehler in der Veröffentlichung des Codes auf die Frage sein könnte, aber die Vertiefung sieht falsch:

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

sollte nicht die if-Anweisung eingerückt zu sein :

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

sonst liest du für immer in

+0

Danke! Dies löste das anfängliche Problem, aber jetzt gibt der Code mir neue Fehler !: (Traceback (letzter Anruf zuletzt): Datei "C: /Users/saeed/IdeaProjects/encryptor/encryption.py", Zeile 46, in encrypt (in_file, out_file, key, iv) Datei "C: /Users/saeed/IdeaProjects/encryptor/encryption.py", Zeile 16, in encrypt chunk + = padding_length * chr (padding_length) Typeerror: can‘ t concat Bytes zu str) – SDurrani

+0

@SDurrani Dann würde ich eine neue Frage zu den neuen Fehlern posten. Ich bin nicht so gut informiert in Bezug auf Verschlüsselung und Entschlüsselung – The4thIceman

+0

OK! Danke für Ihre Hilfe. – SDurrani

Verwandte Themen