<p>Hello Ian,</p>
<div>thanks, I found another php script but it is not working as well :/ What am I doing wrong? </div>
<div>And I have another question too: when I use text for encoding "Text for 1" and "Text for 11" the first letters of encoded strings are the same in both strings?</div>
<p>here is my py:<br># -*- coding: utf-8 -*-<br>from Crypto.Cipher import AES<br>import base64<br>import os</p>
<p># the block size for the cipher object; must be 16, 24, or 32 for AES<br>BLOCK_SIZE = 16</p>
<p># the character used for padding--with a block cipher such as AES, the value<br># you encrypt must be a multiple of BLOCK_SIZE in length.  This character is<br># used to ensure that your value is always a multiple of BLOCK_SIZE<br>
PADDING = '{'</p>
<p># one-liner to sufficiently pad the text to be encrypted<br>pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING</p>
<p># one-liners to encrypt/encode and decrypt/decode a string<br># encrypt with AES, encode with base64<br>EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))<br>DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)</p>

<p># generate a random secret key<br>secret = "passkeypasskeyaa" #os.urandom(BLOCK_SIZE)<br>iv='1234567890123456'<br># create a cipher object using the random secret<br>#cipher = AES.new(secret)<br>cipher = AES.new(secret, AES.MODE_CBC, iv) </p>

<p># encode a string<br>tajnytext ='Alice has a cat' <br>#tajnytext=tajnytext.encode('hex')<br>encoded = EncodeAES(cipher, tajnytext) #encoded = EncodeAES(cipher, 'password')</p>
<p>print encoded<br>print encoded.encode('hex')<br>#print 'Encrypted string:', encoded</p>
<p><br># decode the encoded string<br>decoded = DecodeAES(cipher, encoded)<br>print 'Decrypted string:', decoded</p>
<p> </p>
<p> </p>
<p><br>and my php:<br><?php<br> <br> // This PHP code snippet provides a basic understanding of <br> // PHP's AES encryption.</p>
<p> // The first thing to understand is the meaning of these constants:<br> // MCRYPT_RIJNDAEL_128<br> // MCRYPT_RIJNDAEL_192<br> // MCRYPT_RIJNDAEL_256<br> // You would think that MCRYPT_RIJNDAEL_256 specifies 256-bit encryption,<br>
 // but that is wrong.  The three choices specify the block-size to be used<br> // with Rijndael encryption.  They say nothing about the key size (i.e. strength)<br> // of the encryption.  (Read further to understand how the strength of the<br>
 // AES encryption is set.)<br> //<br> // The Rijndael encyrption algorithm is a block cipher.  It operates on discrete <br> // blocks of data.  Padding MUST be added such that<br>    // the data to be encrypted has a length that is a multiple of the block size.<br>
 // (PHP pads with NULL bytes)<br> // Thus, if you specify MCRYPT_RIJNDAEL_256, your encrypted output will always<br> // be a multiple of 32 bytes (i.e. 256 bits).  If you specify MCRYPT_RIJNDAEL_128,<br> // your encrypted output will always be a multiple of 16 bytes.<br>
 //<br> // Note: Strictly speaking, AES is not precisely Rijndael (although in practice <br> // they are used interchangeably) as Rijndael supports a larger range of block <br> // and key sizes; AES has a fixed block size of 128 bits and a key size of <br>
 // 128, 192, or 256 bits, whereas Rijndael can be specified with key and block <br> // sizes in any multiple of 32 bits, with a minimum of 128 bits and a maximum of <br> // 256 bits.<br> // In summary: If you want to be AES compliant, always choose MCRYPT_RIJNDAEL_128.<br>
 //<br> // So the first step is to create the cipher object:<br> $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');<br> <br> // We're using CBC mode (cipher-block chaining).  Block cipher modes are detailed<br>
 // here: <a href="http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation">http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation</a><br>  <br> // CBC mode requires an initialization vector.  The size of the IV (initialization<br>
 // vector) is always equal to the block-size.  (It is NOT equal to the key size.)<br> // Given that our block size is 128-bits, the IV is also 128-bits (i.e. 16 bytes).<br> // Thus, for AES encryption, the IV is always 16 bytes regardless of the <br>
 // strength of encryption.<br> // <br> // Here's some PHP code to verify our IV size:<br> //$iv_size = mcrypt_enc_get_iv_size($cipher);<br> //printf("iv_size = %d\n",$iv_size);<br> <br> // How do you do 256-bit AES encryption in PHP vs. 128-bit AES encryption???<br>
 // The answer is:  Give it a key that's 32 bytes long as opposed to 16 bytes long.<br> // For example:<br> $key256 = 'passkeypasskeyaa';<br> //$key128 = '1234567890123456';<br> <br> // Here's our 128-bit IV which is used for both 256-bit and 128-bit keys.<br>
 $iv =  '1234567890123456';<br> <br> //printf("iv: %s\n",bin2hex($iv));<br> //printf("key256: %s\n",bin2hex($key256));<br> //printf("key128: %s\n",bin2hex($key128));<br> <br> // This is the plain-text to be encrypted:<br>
 $cleartext = 'Alice has a cat';<br> //printf("plainText: %s\n\n",$cleartext);<br>  <br> // The mcrypt_generic_init function initializes the cipher by specifying both<br> // the key and the IV.  The length of the key determines whether we're doing<br>
 // 128-bit, 192-bit, or 256-bit encryption.  <br> // Let's do 256-bit encryption here:<br> if (mcrypt_generic_init($cipher, $key256, $iv) != -1)<br> {<br>  // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..<br>
  $cipherText = mcrypt_generic($cipher,$cleartext );<br>  mcrypt_generic_deinit($cipher);<br>  <br>  // Display the result in hex.<br>  printf("256-bit encrypted result:\n%s\n\n",bin2hex($cipherText));<br> }<br>
 <br> // Now let's do 128-bit encryption:<br> //if (mcrypt_generic_init($cipher, $key128, $iv) != -1)<br> //{<br>  // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..<br>//  $cipherText = mcrypt_generic($cipher,$cleartext );<br>
//  mcrypt_generic_deinit($cipher);<br>  <br>  // Display the result in hex.<br>//  printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText));<br>// }<br> <br> // -------<br> // Results<br> // -------<br>
 // You may use these as test vectors for testing your AES implementations...<br> // <br> // ------------------------<br> // 256-bit key, CBC mode<br> // ------------------------<br> // IV = '1234567890123456'  <br>
 //  (hex: 31323334353637383930313233343536)<br> // Key = '12345678901234561234567890123456'  <br> //  (hex: 3132333435363738393031323334353631323334353637383930313233343536)<br> // PlainText:<br> //  'The quick brown fox jumped over the lazy dog'<br>
 // CipherText(hex):<br> //  2fddc3abec692e1572d9b7d629172a05caf230bc7c8fd2d26ccfd65f9c54526984f7cb1c4326ef058cd7bee3967299e3</p>
<p> // <br> // ------------------------<br> // 128-bit key, CBC mode<br> // ------------------------<br> // IV = '1234567890123456'  <br> //  (hex: 31323334353637383930313233343536)<br> // Key = '1234567890123456'  <br>
 //  (hex: 31323334353637383930313233343536)<br> // PlainText:<br> //  'The quick brown fox jumped over the lazy dog'<br> // CipherText(hex):<br> //  f78176ae8dfe84578529208d30f446bbb29a64dc388b5c0b63140a4f316b3f341fe7d3b1a3cc5113c81ef8dd714a1c99<br>
 <br>?><br><br></p>
<div class="gmail_quote">2011/6/7 Ian Kelly <span dir="ltr"><<a href="mailto:ian.g.kelly@gmail.com">ian.g.kelly@gmail.com</a>></span><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">On Mon, Jun 6, 2011 at 4:19 PM, miamia <<a href="mailto:peterirbizon@gmail.com">peterirbizon@gmail.com</a>> wrote:<br>
> php I am trying to use is here:<br>> <a href="http://code.google.com/p/antares4pymes/source/browse/trunk/library/System/Crypt/AES.php?r=20" target="_blank">http://code.google.com/p/antares4pymes/source/browse/trunk/library/System/Crypt/AES.php?r=20</a><br>
<br>That library does not appear to be doing CBC as far as I can tell.<br>Maybe they will agree if you use EBC instead?<br><br>> BLOCK_SIZE = 32<br><br>According to the docs, the block size for AES is 16, not 32.  It is<br>
the key size that can be 16, 24, or 32.  But this should just result<br>in extra padding, so it probably doesn't explain the discrepancy.<br><br>> pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING<br>
> EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))<br>> DecodeAES = lambda c, e:<br>> c.decrypt(base64.b64decode(e)).rstrip(PADDING)<br><br>Stylistic note: is it really necessary to use lambda here?  For<br>
readability, just use def.  It's worth having to hit Enter a couple<br>extra times.<br><br>Cheers,<br><font color="#888888">Ian<br></font></blockquote></div><br>