Skip to content

Commit

Permalink
Fixed stream bug (broke due to optional padding option). See issue #8.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jun 1, 2016
1 parent 5518ae8 commit dd56e07
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
8 changes: 4 additions & 4 deletions pyaes/blockfeeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ def _feed_stream(feeder, in_stream, out_stream, block_size = BLOCK_SIZE):
def encrypt_stream(mode, in_stream, out_stream, block_size = BLOCK_SIZE, padding = PADDING_DEFAULT):
'Encrypts a stream of bytes from in_stream to out_stream using mode.'

encrypter = Encrypter(mode)
_feed_stream(encrypter, in_stream, out_stream, block_size, padding)
encrypter = Encrypter(mode, padding = padding)
_feed_stream(encrypter, in_stream, out_stream, block_size)


def decrypt_stream(mode, in_stream, out_stream, block_size = BLOCK_SIZE, padding = PADDING_DEFAULT):
'Decrypts a stream of bytes from in_stream to out_stream using mode.'

decrypter = Decrypter(mode)
_feed_stream(decrypter, in_stream, out_stream, block_size, padding)
decrypter = Decrypter(mode, padding = padding)
_feed_stream(decrypter, in_stream, out_stream, block_size)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
for API reference and details.'''

setup(name = 'pyaes',
version = '1.5.0',
version = '1.6.0',
description = 'Pure-Python Implementation of the AES block-cipher and common modes of operation',
long_description = LONG_DESCRIPTION,
author = 'Richard Moore',
Expand Down
34 changes: 33 additions & 1 deletion tests/test-blockfeeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@
import os
import random

try:
from StringIO import StringIO
except:
import io
StringIO = io.BytesIO

import pyaes
from pyaes.blockfeeder import Decrypter, Encrypter

from pyaes import decrypt_stream, encrypt_stream
from pyaes.util import to_bufferable


Expand Down Expand Up @@ -114,3 +120,29 @@
passed = decrypted == plaintext
cipher_length = len(ciphertext)
print(" cipher-length=%(cipher_length)s passed=%(passed)s" % locals())

plaintext = os.urandom(1000)

for mode_name in pyaes.AESModesOfOperation:
mode = pyaes.AESModesOfOperation[mode_name]
print(mode.name + ' (stream operations)')

kw = dict(key = key)
if mode_name in ('cbc', 'cfb', 'ofb'):
kw['iv'] = os.urandom(16)

moo = mode(**kw)
output = StringIO()
pyaes.encrypt_stream(moo, StringIO(plaintext), output)
output.seek(0)
ciphertext = output.read()

moo = mode(**kw)
output = StringIO()
pyaes.decrypt_stream(moo, StringIO(ciphertext), output)
output.seek(0)
decrypted = output.read()

passed = decrypted == plaintext
cipher_length = len(ciphertext)
print(" cipher-length=%(cipher_length)s passed=%(passed)s" % locals())

0 comments on commit dd56e07

Please sign in to comment.