On 8/15/07, per9000 <[EMAIL PROTECTED]> wrote: > Hi python people, > > I am trying to figure out the best way to encrypt files in python. > > I've build a small script (see below) that encrypts the ubuntu 7.04 > iso file in 2 minutes (I like python :) ). > > But I have some thoughts about it. By pure luck (?) this file happened > to be N*512 bytes long so I do not have to add crap at the end - but > on files of the size N*512 + M (M != 521) I will add some crap to make > it fit in the algorithm. When I later decrypt I will have the stuff I > do not want. How do people solve this? (By writing the number of > relevant bytes in readable text in the beginning of the file?)
The term you're looking for is "padding". See http://en.wikipedia.org/wiki/Padding_%28cryptography%29 for a brief introduction, and especially the two RFCs mentioned about halfway down. > Also I wonder if this can be solved with filestreams (Are there > streams in python? The only python file streams I found in the evil > search engine was stuff in other forums.) I don't know how to answer this, because it's not clear what you mean by "file streams". Python's file objects act similarly to things called streams in other languages, such as Java's InputStreams and Readers, if that's what you're asking. > Other comments are of course also welcome, > Per > > > # crypto_hardcoded.py starts here [snip] I notice there's some repeated code in your main loop. This generally means there's room for improvement in your program flow. Here's one possible way you could structure it: separate out the file reading and padding logic into a generator function that takes a filename or file object, and yields blocks one at a time, padded to the correct block size. Then your main loop can be simplified to something like this: for plaintext_block in read_as_blocks(in_file, block_size): ciphertext_block = cryptor.encrypt(plaintext_block) out_file.write(ciphertext_block) Techniques like these can make it easier to follow what's going on, especially as your programs get more complicated. Don't be afraid to experiment! -- David -- http://mail.python.org/mailman/listinfo/python-list