0

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!')
SDurrani
  • 33
  • 5
  • I would recommend adding some print statements in the encryption function to see where exactly it "gets stuck" – The4thIceman Mar 14 '17 at 01:01
  • It’s hard to tell with the indentation in `encrypt`. Please run your script with `-tt` (`python -tt script.py`) and make sure what appears in your post matches the contents of the file. – Ry- Mar 14 '17 at 01:02
  • (If it does match, it’s because your `while not finished` only has one statement in it that doesn’t change `finished`.) – Ry- Mar 14 '17 at 01:02
  • Hello Ryan. How exactly would I fix that? And to answer The4thIceman, I did. print('Encrypted!') Doesn't go through. – SDurrani Mar 14 '17 at 01:04
  • @SDurrani I know, but maybe put a couple in the actual encrypt function, to see how far it gets in the function, because that seems to be where the issue is – The4thIceman Mar 14 '17 at 01:06
  • @The4thIceman I added many print checks like you said, and discovered a problem. check002 repeats infinitely! How will I stop this? – SDurrani Mar 14 '17 at 01:15
  • Note for the future. Not all SO users are familiar with pycrypto, so include `from Crypto.Cipher import AES` (and `import os`) in your code to make it actually run without figuring out where `AES` comes from is good. – Mark Tolonen Mar 14 '17 at 01:31

1 Answers1

0

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

The4thIceman
  • 3,709
  • 2
  • 29
  • 36
  • Thanks! This resolved the initial problem, but now the code is giving me new errors!: (Traceback (most recent call last): File "C:/Users/saeed/IdeaProjects/encryptor/encryption.py", line 46, in encrypt(in_file, out_file, key, iv) File "C:/Users/saeed/IdeaProjects/encryptor/encryption.py", line 16, in encrypt chunk += padding_length * chr(padding_length) TypeError: can't concat bytes to str) – SDurrani Mar 14 '17 at 01:21
  • @SDurrani Then I would post a new question regarding the new errors. I am not that knowledgeable regarding encryption and decryption – The4thIceman Mar 14 '17 at 01:23
  • OK! Thank you for your help. – SDurrani Mar 14 '17 at 01:23