PHP Classes

Limited to 16 chars?

Recommend this page to a friend!

      AES Cipher  >  All threads  >  Limited to 16 chars?  >  (Un) Subscribe thread alerts  
Subject:Limited to 16 chars?
Summary:How can I go behond 16 chars?
Messages:8
Author:Fernando Martins
Date:2008-05-12 22:48:32
Update:2013-12-03 23:47:37
 

  1. Limited to 16 chars?   Reply   Report abuse  
Picture of Fernando Martins Fernando Martins - 2008-05-12 22:48:32
I've tried the 128 bit and it kinda works...
It works with "Alice has a cat", but it does not work with "Alice has a realy big, big, big cat"...

How can I go beyond this limitation?

  2. Re: Limited to 16 chars?   Reply   Report abuse  
Picture of Marcin Wisniowski Marcin Wisniowski - 2008-05-13 16:57:44 - In reply to message 1 from Fernando Martins
Yes, a single AES encoding cycle is limited to max 16 chars (it comes from specyfication). If You like to encode more then 16 chars please look at my new added class in this package: AESCipher.class.php. It uses AES.class.php to encode strings >16 chars.

  3. Re: Limited to 16 chars?   Reply   Report abuse  
Picture of Fernando Martins Fernando Martins - 2008-05-15 21:34:40 - In reply to message 2 from Marcin Wisniowski
Hello Marcin.

Thank you very much for your hawser, it worked just fine.

Now I have another problem. I have to crypt and decrypt messages to and from a Java application and I'm being successful and I think I'm doing something wrong.

I have the following 128 bit key, the bytes are on hex:
24 94 D1 ED 5D 16 CD F7 C2 04 B8 59 A7 59 FE A1

and I'm trying to decrypt the following text ($cryptext in the example bellow):
8QIfukhvm4ZDN9BefnifmqVIsni++xg06/gUA6+bsldiLqn9BNGE2Fru2aZNtm0eD4lAK1fXg4ut
OAOc5cJfu7OqFb109oSTU1VLbl1H47CqX9GhchCKIyaxyYOUw8Hjzr2G+Ko78/T0CodmhQ7Wzg==

that corresponds to my message, the following text lines from a file:
#Sun May 04 23:41:11 GMT 2008
#blá blá blá
license_id=345345
machine_id=00\:18\:F3\:6B\:A7\:06

Here's what I'm doing:
require_once('./AESCipher.class.php');
$Cipher = new AESCipher(AES::AES256);
$key_128bit = '2494D1ED5D16CDF7C204B859A759FEA1';
[...]
$result = $Cipher->decrypt($cryptext, $key_128bit);

Both $cryptext and $result are different from what I was expecting (as explained above).

What am I doing wrong here?

  4. Re: Limited to 16 chars?   Reply   Report abuse  
Picture of Marcin Wisniowski Marcin Wisniowski - 2008-05-16 19:52:50 - In reply to message 3 from Fernando Martins
AESCipher Class generates Key from password (text) as in example (test3.php) if You like to use your generated hex key You must edit AESCipherprivate::_generateKey() method
just pass through e key to return. This patch should help :)

  5. Re: Limited to 16 chars?   Reply   Report abuse  
Picture of Fernando Martins Fernando Martins - 2008-05-17 23:35:08 - In reply to message 4 from Marcin Wisniowski
Hello again.

The workaround did not worked as I expected...

Tank you very much for your time and explanations.

  6. Re: Limited to 16 chars?   Reply   Report abuse  
Picture of Daniel Williams Daniel Williams - 2011-02-04 20:59:32 - In reply to message 5 from Fernando Martins
In order to make this work for characters more than 16 do the following:

require("AESCipher.class.php");
$cipher = new AESCipher(AES::AES256);

$key = "SET YOUR KEY";

// to encrypt
$encrypted = base64_encode($cipher->encrypt($plaintext, $key));

// to decrypt
echo $cipher->decrypt(base64_decode($encrypted), $key);

the reason you must use base64_encode is that without it the return is binary data that you would have to store into a BLOB entity in mysql. However, if you base64 encode it you can store it in a regular varchar or text field.

You just have to base64_decode it to decrypt it. If you store the encryption in a varchar field without base64_encode you will not be able to decrypt it.

Hope this helps someone.

  7. Re: Limited to 16 chars?   Reply   Report abuse  
Picture of Chad Johnson Chad Johnson - 2013-12-03 23:47:37 - In reply to message 2 from Marcin Wisniowski
I have the same problem but it is with decryption. I can encrypt large strings but cannot decrypt them. I am Using the AES Cipher class. only part of the string is decrypted.


  8. Re: Limited to 16 chars?   Reply   Report abuse  
Picture of P. David Flammer P. David Flammer - 2014-08-29 00:22:19 - In reply to message 7 from Chad Johnson
It looks like there is a problem in the hexToString function in AES. Replace it with this, and it should be fixed:

public function hexToString($hex) {
$str="";
for($i=0; $i<strlen($hex); $i=$i+2 ) {
$str .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $str;
}