Hello, this is my first post on DaniWeb so I hope you'll go easy on me ,and my english.I'm working on a implementation of the AES algorithm in python;my app is supposed to encrypt the contents of files, and it does, yet it is kind of slow.I've optimized the code as good as I could because I am a beginner , but I would like to know if there is a way to reduce the amount of text , before I go through the encryption phase with it.I cannot use PyCrypto, nor OOP ( well I can only use OOP for the interface, silly requirements..), I used Galois multiplication tables for the MixColumns operation to further reduce the execution time, so if you have some ideea of how I could reduce the amount of text , please do tell.
Thank you for your help.

Edited 5 Years Ago by drak0: n/a

Perhaps you could compress the text first to reduce its size

import zlib
text = open("file_to_be_encrypted.txt").read()
data = zlib.compress(text) # normaly much shorter than text

# ... encrypt data with your AES code
# text can be retrieved with zlib.decompress(data)

This technique won't help if your data is already in compressed format (.zip, .jpg, ...)

Edited 5 Years Ago by Gribouillis: n/a

Thank you, did that, I'm sorry for wasting your time, I should have said that is used zlib.One question about this , is it possible that pickling the text read from the file might increase it's size?

Edited 5 Years Ago by drak0: n/a

Thank you, did that, I'm sorry for wasting your time, I should have said that is used zlib.One question about this , is it possible that pickling the text read from the file might increase it's size?

Yes I think it is possible, but not significantly. Pickle is not a compression algorithm, it's a persistence algorithm adapted to python data structures. It has nothing to do with zlib or gzip.

Yes I think it is possible, but not significantly. Pickle is not a compression algorithm, it's a persistence algorithm adapted to python data structures. It has nothing to do with zlib or gzip.

I know but I am doing this :

text=file.read()                      
pickled_text=pickle.dumps(text, -1)                               
compressed_text=zlib.compress(pickled_text)

That is why I asked.And please, could you tell me how to edit the code so that is shows the number of the line?Like you did.
LE: found how

Edited 5 Years Ago by drak0: n/a

I know but I am doing this :

text=file.read()                      
pickled_text=pickle.dumps(text, -1)                               
compressed_text=zlib.compress(pickled_text)

That is why I asked.And please, could you tell me how to edit the code so that is shows the number of the line?Like you did.
LE: found how

Pickling a string does not seem to be a good idea. Your combination of pickle and compress may be less efficient than a single compression.

>>> from pickle import dumps
>>> s = "hello world"
>>> p = dumps(s, -1)
>>> len(p)
18 # > len(s) !
>>> len(s)
11
>>> repr(p)
"'\\x80\\x02U\\x0bhello worldq\\x00.'"

pickling the string only added some noise (the info needed to tell python that the pickled object is a string).

Edited 5 Years Ago by Gribouillis: n/a

pickling the string only added some noise (the info needed to tell python that the pickled object is a string).

Thanks for that! I really missed that one, I modified the code, I gained 1 sec while running the app on a 2 page txt file, so it's a good gain.

This question has already been answered. Start a new discussion instead.