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.p y", line 37, in ?
dec.finish()
File "/usr/local/lib/python2.3/site-packages/yawPyCrypto/_CipherBase.py" , line 239, in finish
raise ValueError("Dec ryption 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.Cip her import DecryptCipher, EncryptCipher
from yawPyCrypto.Con stants import CIPHER_BLOWFISH , MODE_CBC
passwd = 'this is not a great passwd'
blocksize = 1024
infile = file('source.mp 3')
enc = EncryptCipher(p asswd, CIPHER_BLOWFISH , MODE_CBC)
outfile = file('crypted.m p3.cr', 'w')
while 1:
data = infile.read(blo cksize)
if len(data)==0: break
enc.feed(data)
outfile.write(e nc.data)
enc.finish()
infile.close()
outfile.close()
# Now try and decrypt
dec = DecryptCipher(p asswd)
infile = file('crypted.m p3.cr')
outfile = file('decrypted .mp3', 'w')
while 1:
data = infile.read(blo cksize)
if len(data)==0: break
dec.feed(data)
outfile.write(d ec.data)
infile.close()
outfile.close()
------------------------------------------------------------------
video:~/python/examples/yawpycrypto> ls -l *mp3*
-rw-r--r-- 1 jdhunter members 3620957 Aug 28 12:36 crypted.mp3.cr
-rw-r--r-- 1 jdhunter members 3609600 Aug 28 12:36 decrypted.mp3
-rw-r--r-- 1 jdhunter members 3610317 Aug 28 12:32 source.mp3