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


Groups > comp.lang.python > #7100 > unrolled thread

Re: python + php encrypt/decrypt

Started bygeremy condra <debatem1@gmail.com>
First post2011-06-06 10:41 -0700
Last post2011-06-06 17:45 -0600
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: python + php encrypt/decrypt geremy condra <debatem1@gmail.com> - 2011-06-06 10:41 -0700
    Re: python + php encrypt/decrypt miamia <peterirbizon@gmail.com> - 2011-06-06 15:19 -0700
      Re: python + php encrypt/decrypt Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 17:45 -0600

#7100 — Re: python + php encrypt/decrypt

Fromgeremy condra <debatem1@gmail.com>
Date2011-06-06 10:41 -0700
SubjectRe: python + php encrypt/decrypt
Message-ID<mailman.2497.1307382091.9059.python-list@python.org>
On Sun, Jun 5, 2011 at 3:34 AM, Peter Irbizon <peterirbizon@gmail.com> wrote:
> Hello, thanks, Unfortunatelly I don't understand how xml should resolve my
> issue. My problem is:
> I am trying to use aes256 cbc on python and php to decrypt "textstring". But
> results are not the same in php and python. Any idea why? password and iv is
> the same so I don't know where is the problem. I am trying do decrypt data
> in python then store it as base64 and read and decrypt it in php.
>
> 2011/6/4 <hidura@gmail.com>
>>
>> Use xml to pass the encrypt text.
>>
>> On , Peter Irbizon <peterirbizon@gmail.com> wrote:
>> >
>> > Hello,
>> >
>> > I would like to encrypt text in python and decrypt it in my PHP script.
>> > I tried to use pycrypto and some aes php scripts but the results are not the
>> > same. Please, is there any example (the best way source codes) how to do
>> > this in python and PHP?

Please provide links to the AES implementation you're trying to use
from PHP and the Python and PHP code you're using.

Geremy Condra

[toc] | [next] | [standalone]


#7113

Frommiamia <peterirbizon@gmail.com>
Date2011-06-06 15:19 -0700
Message-ID<0a8faeca-4c26-490b-9676-ebe8e3b1c930@l6g2000vbn.googlegroups.com>
In reply to#7100
On Jun 6, 7:41 pm, geremy condra <debat...@gmail.com> wrote:
> On Sun, Jun 5, 2011 at 3:34 AM, Peter Irbizon <peterirbi...@gmail.com> wrote:
> > Hello, thanks, Unfortunatelly I don't understand how xml should resolve my
> > issue. My problem is:
> > I am trying to use aes256 cbc on python and php to decrypt "textstring". But
> > results are not the same in php and python. Any idea why? password and iv is
> > the same so I don't know where is the problem. I am trying do decrypt data
> > in python then store it as base64 and read and decrypt it in php.
>
> > 2011/6/4 <hid...@gmail.com>
>
> >> Use xml to pass the encrypt text.
>
> >> On , Peter Irbizon <peterirbi...@gmail.com> wrote:
>
Hello,

php I am trying to use is here:
http://code.google.com/p/antares4pymes/source/browse/trunk/library/System/Crypt/AES.php?r=20

and python code:
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import base64
import os

BLOCK_SIZE = 32
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e:
c.decrypt(base64.b64decode(e)).rstrip(PADDING)
secret = "passkeypasskeyaa" #os.urandom(BLOCK_SIZE)
iv='1234567890123456'
cipher = AES.new(secret, AES.MODE_CBC, iv)
# encode a string
tajnytext ='Alice has a cat'
encoded = EncodeAES(cipher, tajnytext)
print encoded

# decode the encoded string
decoded = DecodeAES(cipher, encoded)
print 'Decrypted string:', decoded


Thank you for your help in advance

> >> > Hello,
>
> >> > I would like to encrypt text in python and decrypt it in my PHP script.
> >> > I tried to use pycrypto and some aes php scripts but the results are not the
> >> > same. Please, is there any example (the best way source codes) how to do
> >> > this in python and PHP?
>
> Please provide links to the AES implementation you're trying to use
> from PHP and the Python and PHP code you're using.
>
> Geremy Condra- Hide quoted text -
>
> - Show quoted text -

[toc] | [prev] | [next] | [standalone]


#7117

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-06-06 17:45 -0600
Message-ID<mailman.2510.1307403967.9059.python-list@python.org>
In reply to#7113
On Mon, Jun 6, 2011 at 4:19 PM, miamia <peterirbizon@gmail.com> wrote:
> php I am trying to use is here:
> http://code.google.com/p/antares4pymes/source/browse/trunk/library/System/Crypt/AES.php?r=20

That library does not appear to be doing CBC as far as I can tell.
Maybe they will agree if you use EBC instead?

> BLOCK_SIZE = 32

According to the docs, the block size for AES is 16, not 32.  It is
the key size that can be 16, 24, or 32.  But this should just result
in extra padding, so it probably doesn't explain the discrepancy.

> pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
> EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
> DecodeAES = lambda c, e:
> c.decrypt(base64.b64decode(e)).rstrip(PADDING)

Stylistic note: is it really necessary to use lambda here?  For
readability, just use def.  It's worth having to hit Enter a couple
extra times.

Cheers,
Ian

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web