Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.034 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'init': 0.07; 'initialize': 0.07; 'msg': 0.09; 'random': 0.14; '24,': 0.16; 'cipher': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hashlib': 0.16; 'sure.': 0.16; 'wrote:': 0.18; 'module': 0.19; "skip:' 30": 0.19; 'thu,': 0.19; 'import': 0.22; 'reset': 0.22; 'paul': 0.24; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; '>>>>': 0.31; 'piece': 0.31; 'second,': 0.31; 'subject:what': 0.31; 'txt': 0.31; 'with,': 0.31; 'text': 0.33; 'running': 0.33; 'etc': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'shorter': 0.36; 'done': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'strictly': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; "you've": 0.63; 'effectively': 0.66; 'skip:r 30': 0.69; 'other.': 0.75; 'pycrypto': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Z1tl4zVtNCCkdF5fuy0cVRB5mJSOctNXGg7p+sSqHLs=; b=cRtTQU7s2Lfm9507DGFM3lBGNjPu9frr3NuAmdwNq+gBbngfBunHlLd5YNtk79oTWT 0BPgf7a6InNA0vMvmZkevcUShLcmuIQ+jd5Jpy1a75Kk1DWnZb+ZvdE+5ECS1DjyxGEc 0x1pSoheOFHazCWPgO54FrSZG1/uwEpKx0do9auLLPfPUr8OARfTFRJmTy1+wRjaEeTX VYeli1KSlPAuwBKqc0mb4VhRY9S0n4z1GWMSLk6uST70OCgVLhP9pNSRRkma+K0Ybbfa S0CWGunvN/X+Pjk6ISKhnp11veGjZnnFw1umoydtDZQ7iBAY+s11Th+9kR+JtwSsNIVB 6vhw== MIME-Version: 1.0 X-Received: by 10.66.188.203 with SMTP id gc11mr1691014pac.63.1382598436623; Thu, 24 Oct 2013 00:07:16 -0700 (PDT) In-Reply-To: <97dbe3cc-b739-4876-9221-1143dc1b8c73@googlegroups.com> References: <97dbe3cc-b739-4876-9221-1143dc1b8c73@googlegroups.com> Date: Thu, 24 Oct 2013 18:07:16 +1100 Subject: Re: pycrypto: what am I doing wrong? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1382598445 news.xs4all.nl 15898 [2001:888:2000:d::a6]:59147 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57422 On Thu, Oct 24, 2013 at 4:22 PM, Paul Pittlerson wrote: > msg = cipher.encrypt(txt) > >>>> '|s\x08\xf2\x12\xde\x8cD\xe7u*' > > msg = cipher.encrypt(txt) > >>>> '\xa1\xed7\xb8h > # 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