yawPyCrypto : encrypt file in blocks

Discussion in 'Python' started by John Hunter, Aug 28, 2003.

  1. John Hunter

    John Hunter Guest

    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
     
    John Hunter, Aug 28, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arjen
    Replies:
    3
    Views:
    454
    Scott Allen
    Feb 27, 2005
  2. Heiko Wundram

    ANN: yawPyCrypto 0.1 and Flatten 0.2

    Heiko Wundram, Aug 11, 2003, in forum: Python
    Replies:
    0
    Views:
    292
    Heiko Wundram
    Aug 11, 2003
  3. matt
    Replies:
    1
    Views:
    285
    George Ogata
    Aug 6, 2004
  4. Steven Taylor
    Replies:
    9
    Views:
    271
    Brian Candler
    Apr 27, 2009
  5. http://ejobseek.com

    Encrypt in Perl, De-encrypt in Javascript

    http://ejobseek.com, Sep 1, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    295
    James Willmore
    Sep 1, 2003
Loading...

Share This Page