yawPyCrypto : encrypt file in blocks

J

John Hunter

I am trying to use to encrypt some potentially large files, so I want
to do it in blocks rather than as a single string. Below is the code
I am using.

When I run dec.finish(), I get the traceback

Traceback (most recent call last):
File "crblocks.py", line 37, in ?
dec.finish()
File "/usr/local/lib/python2.3/site-packages/yawPyCrypto/_CipherBase.py", line 239, in finish
raise ValueError("Decryption stream ended too early.")
ValueError: Decryption stream ended too early.

and the source and decrypted differ (the decrypted one is a little
smaller), though the apparently differ only at the end, because I can
play the decrypted mp3 that I am using for the test file.

Apparently, I am not handling the final bytes correctly. The file
size are listed below the code.

Any advice? Also, is this the best way to encrypt large files? Is
there a preferred block size?

Thanks,
John Hunter

from yawPyCrypto.Cipher import DecryptCipher, EncryptCipher
from yawPyCrypto.Constants import CIPHER_BLOWFISH, MODE_CBC

passwd = 'this is not a great passwd'
blocksize = 1024
infile = file('source.mp3')
enc = EncryptCipher(passwd, CIPHER_BLOWFISH, MODE_CBC)
outfile = file('crypted.mp3.cr', 'w')
while 1:
data = infile.read(blocksize)
if len(data)==0: break
enc.feed(data)
outfile.write(enc.data)
enc.finish()
infile.close()
outfile.close()


# Now try and decrypt
dec = DecryptCipher(passwd)
infile = file('crypted.mp3.cr')
outfile = file('decrypted.mp3', 'w')

while 1:
data = infile.read(blocksize)
if len(data)==0: break
dec.feed(data)
outfile.write(dec.data)
infile.close()
outfile.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top