0

I am using Pycrypto encrypter and decrypter with AES 256 Symmetric key algorithm. My encryption part is taking much more time in comparison to decryption time. Is there any good reason for it?

I tried on 16 MB file:

Encryption time = 3.31 secs
Decryption time = 0.18 secs

Can someone help me in telling what could be the reason for this?

Encryption Logic Looks like below:

def encrypt_file(key, infile, chunksize=64 * 1024):
    """ Encrypts a file using AES (CBC mode) with the
        given key.

        key:
            The encryption key - a string that must be
            either 16, 24 or 32 bytes long. Longer keys
            are more secure.

        infile:
            input file

        chunksize:
            Sets the size of the chunk which the function
            uses to read and encrypt the file. Larger chunk
            sizes can be faster for some files and machines.
            chunksize must be divisible by 16.
    """
    iv = os.urandom(16)
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    infile.seek(0, os.SEEK_END)
    filesize = infile.tell()
    infile.seek(0, os.SEEK_SET)

    encrypted = b'';
    encrypted += struct.pack('<Q', filesize)
    encrypted += iv

    while True:
        chunk = infile.read(chunksize)
        if len(chunk) == 0:
            break
        elif len(chunk) % 16 != 0:
            chunk += b' ' * (16 - len(chunk) % 16)
        encrypted += encryptor.encrypt(chunk)

    return encrypted

And Decryption logic looks like below:

def decrypt_file(key, infile, out_filename, chunksize=24*1024):
    """ Decrypts a file using AES (CBC mode) with the
        given key.
    """
    if key == '':
        with open(out_filename, 'wb') as outfile:
            for line in infile:
                outfile.write(line)
        return outfile.name

    origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
    iv = infile.read(16)
    decryptor = AES.new(key, AES.MODE_CBC, iv)

    # for line in infile:
    #     print(line)
    with open(out_filename, 'wb+') as outfile:
        while True:
            chunk = infile.read(chunksize)
            if len(chunk) == 0:
                break
            outfile.write(decryptor.decrypt(chunk))
        outfile.truncate(origsize)

        return outfile.name
Community
  • 1
  • 1
hatellla
  • 4,796
  • 8
  • 49
  • 101
  • Well? Did you use a profiler to indicate where the delay is? A usual culprit is the random number generator, which you don't need during decrypt. Note that your file handling is incorrect, the `read` method may return fewer bytes than the amount requested (which then may be padded in your code, which means that your plaintext file will be incorrect). – Maarten Bodewes Apr 25 '18 at 14:46
  • PS any reason for the different buffer sizes? How did you measure the time? Separate processes? – Maarten Bodewes Apr 25 '18 at 14:53
  • No reason as such for different buffer sizes. For measuring time, I used time.time() in python. – hatellla Apr 25 '18 at 15:01
  • Did you use the same process for encryption and decryption? Listen, if you don't match parameters, then how do you know you are testing right? What about I/O speed? What about interpreter startup times? Testing performance is an art as well. – Maarten Bodewes Apr 25 '18 at 15:07
  • Had to vote down as the testing strategy is missing, and what we know of it is flawed. You may also want to fix the file reading issue before doing any more tests - correctness is more important than performance. – Maarten Bodewes Apr 26 '18 at 12:32

0 Answers0