backer Posted February 25, 2009 Share Posted February 25, 2009 Hi, I'm not a PHP developer and has some problems with a script that I'm trying to do with AES. Have taking lines from different places and came up with this (look on code). The problem when decrypt back I get some strange characters in the end that looks like this; ���� ...please help this noob <?php $text = "The quick brown fox jumped over the lazy dog"; $key = "12345678901234561234567890123456"; $iv = "1234567890123456"; echo "Orginal values"; echo "</br>Text: " . $text; echo "</br>Key: " . $key; echo "</br>IV: " . $iv; echo "</br></br>"; $feedback = encrypt($text, $key, $iv); echo "</br></br>"; $feedback = decrypt($feedback["result"], $feedback["key"], $feedback["iv"]); echo "</br></br>"; // Values from another system (not php) // String is the same; The quick brown fox jumped over the lazy dog. $text = "4d3b2ca3ee35b3e58270c4270b909384d57670ba7e3e57d0798e4c5a15267bc3d4547154c4f4abaf1c325e89f34e7100"; $key = "3132333435363738393031323334353631323334353637383930313233343536"; $iv = "3a16ff9192289f9546afb2aebb8d122a"; echo "Other Sevice values"; $feedback = decrypt($text, $key, $iv); function encrypt($text,$key,$iv) { $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv_size = mcrypt_enc_get_iv_size($cipher); $key256 = $key;//'12345678901234561234567890123456'; $iv = $iv;//'1234567890123456'; // 16bits // This is the plain-text to be encrypted: $cleartext = $text; // Let's do 256-bit encryption here: if (mcrypt_generic_init($cipher, $key256, $iv) != -1) { // PHP pads with NULL bytes if $cleartext is not a multiple of the block size.. $cipherText = mcrypt_generic($cipher,$cleartext ); mcrypt_generic_deinit($cipher); // Display the result in hex; echo "</br>Encrypt; " . $cleartext; echo "</br> Encrypted (hex): ".bin2hex($cipherText); echo "</br> Key (hex): ".bin2hex($key256); echo "</br> IV (hex): ".bin2hex($iv); /* * Example result * Encrypt; The quick brown fox jumped over the lazy dog * Encrypted (hex): 2fddc3abec692e1572d9b7d629172a05caf230bc7c8fd2d26ccfd65f9c54526984f7cb1c4326ef058cd7bee3967299e3 * Key (hex): 3132333435363738393031323334353631323334353637383930313233343536 * IV (hex): 31323334353637383930313233343536 */ return array("result"=>bin2hex($cipherText), "key"=>bin2hex($key256), "iv"=>bin2hex($iv)); } } function decrypt($encyption, $key, $iv) { $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv_size = mcrypt_enc_get_iv_size($cipher); $key256 = hex2bin($key); $iv = hex2bin($iv); $cleartext = hex2bin($encyption); // Let's do 256-bit decryption here: if (mcrypt_generic_init($cipher, $key256, $iv) != -1) { // PHP pads with NULL bytes if $cleartext is not a multiple of the block size.. $cipherText = mdecrypt_generic($cipher,$cleartext ); mcrypt_generic_deinit($cipher); // Display the result echo "</br>Decrypt; " . $cleartext; echo "</br> result: ".$cipherText; echo "</br> Key: ".($key256); echo "</br> IV: ".($iv); /* * Example result * Decrypt; The quick brown fox jumped over the lazy dog���� * Decrypt (string): 2fddc3abec692e1572d9b7d629172a05caf230bc7c8fd2d26ccfd65f9c54526984f7cb1c4326ef058cd7bee3967299e3 * Key (string): 12345678901234561234567890123456 * IV (string): 1234567890123456 */ } } function hex2bin($h) { if (!is_string($h)) return null; $r=''; for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); } return $r; } ?> Link to comment https://forums.phpfreaks.com/topic/146866-aes-encryptdecrypt/ Share on other sites More sharing options...
backer Posted February 26, 2009 Author Share Posted February 26, 2009 Anyone, help!! Link to comment https://forums.phpfreaks.com/topic/146866-aes-encryptdecrypt/#findComment-771673 Share on other sites More sharing options...
haku Posted February 26, 2009 Share Posted February 26, 2009 Check the data before it's encrypted (echo it to the screen), to confirm where the problem is. Usually those marks are due to charsetproblems. Link to comment https://forums.phpfreaks.com/topic/146866-aes-encryptdecrypt/#findComment-771674 Share on other sites More sharing options...
backer Posted February 26, 2009 Author Share Posted February 26, 2009 Possible to force strings or php page to be utf-8 somehow? Link to comment https://forums.phpfreaks.com/topic/146866-aes-encryptdecrypt/#findComment-771679 Share on other sites More sharing options...
Mchl Posted February 26, 2009 Share Posted February 26, 2009 Yes. Make sure your html page is declared as utf-8. Save your PHP source as UTF-8 without BOM. Use mbstring functions for operations on UTF-8 strings. Link to comment https://forums.phpfreaks.com/topic/146866-aes-encryptdecrypt/#findComment-771682 Share on other sites More sharing options...
backer Posted February 26, 2009 Author Share Posted February 26, 2009 Really need some help here, =( tried putting this at top <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> page sez it's utf-8, but when I'm trying this out it still says that my text is ASCII. $text = utf8_encode("The quick brown fox jumped over the lazy dog"); echo "type; ".mb_detect_encoding($text); Link to comment https://forums.phpfreaks.com/topic/146866-aes-encryptdecrypt/#findComment-771693 Share on other sites More sharing options...
backer Posted February 26, 2009 Author Share Posted February 26, 2009 Found the problem, the string convertion problem, ASCII characters didn't cut of \0 characters. Do a trim on it. Link to comment https://forums.phpfreaks.com/topic/146866-aes-encryptdecrypt/#findComment-771699 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.