Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57422

Re: pycrypto: what am I doing wrong?

References <97dbe3cc-b739-4876-9221-1143dc1b8c73@googlegroups.com>
Date 2013-10-24 18:07 +1100
Subject Re: pycrypto: what am I doing wrong?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1447.1382598445.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Oct 24, 2013 at 4:22 PM, Paul Pittlerson <menkomigen6@gmail.com> wrote:
> msg = cipher.encrypt(txt)
>
>>>> '|s\x08\xf2\x12\xde\x8cD\xe7u*'
>
> msg = cipher.encrypt(txt)
>
>>>> '\xa1\xed7\xb8h<l\x7f\xd7\xba\xed'
>
> # etc

Is this strictly the code you're using? AES is a stream cipher; what
you've effectively done is encrypt the text twice, once as a follow-on
message from the other. To decrypt the second, you'll need to include
the first - or treat it as a stream, and decrypt piece by piece.
Untested code:

import hashlib
from Crypto.Cipher import AES
from Crypto import Random

# Shorter version of your key hashing:
key = hashlib.sha256("my key").digest()

iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
txt = 'hello world'

msg1 = cipher.encrypt(txt)
msg2 = cipher.encrypt(txt)
# You may need to reset cipher here, I'm not sure.
# cipher = AES.new(key, AES.MODE_CFB, iv)
cipher.decrypt(iv) # Initialize the decrypter with the init vector
print(cipher.decrypt(msg1))
print(cipher.decrypt(msg2))



I don't have pycrypto to test with, but running the same code with
Pike's Crypto module does what I expect here.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

pycrypto: what am I doing wrong? Paul Pittlerson <menkomigen6@gmail.com> - 2013-10-23 22:22 -0700
  Re: pycrypto: what am I doing wrong? Chris Angelico <rosuav@gmail.com> - 2013-10-24 18:07 +1100
    Re: pycrypto: what am I doing wrong? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-10-24 09:30 +0200
      Re: pycrypto: what am I doing wrong? Chris Angelico <rosuav@gmail.com> - 2013-10-24 18:43 +1100
  Re: pycrypto: what am I doing wrong? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-10-24 09:33 +0200
    Re: pycrypto: what am I doing wrong? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-10-24 09:35 +0200

csiph-web