yawPyCrypto : encrypt file in blocks

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Aug 28 13:38:19 EDT 2003


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()

------------------------------------------------------------------
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





More information about the Python-list mailing list