note 44728 modified in function.mcrypt-encrypt by cece
| From: | cece@php.net | Date: | Wed, 03 Nov 2004 02:20:12 +0000 |
| Subject: | note 44728 modified in function.mcrypt-encrypt by cece | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79528@lists.php.net to get a copy of this message | ||
[Editor's note: original script posted by
andrewcare at execulink dot com
07-Jul-2004 04:45]
I needed to add
trim($data)
to andrew's example for it to remove some leftover hex garbage from the decrypted string.
Here's the working example:
<?php
$data = "Plaintext"; // Data to encrypt
(http://www.ciphersbyritter.com/glossary.htm#Encryption)
$key = "Secret"; // Encryption key (http://www.ciphersbyritter.com/glossary.htm#Key)
$td = MCRYPT_RIJNDAEL_256; // Encryption cipher (http://www.ciphersbyritter.com/glossary.htm#Cipher)
$iv_size = mcrypt_get_iv_size($td, MCRYPT_MODE_ECB); // Dependant on cipher/mode combination
(http://www.php.net/manual/en/function.mcrypt-get-iv-size.php)
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); // Creates an IV
(http://www.ciphersbyritter.com/glossary.htm#IV)
echo "Original data: $data<br />";
$encrypted_data = mcrypt_encrypt($td, $key, $data, MCRYPT_MODE_CBC, $iv); // Encrypts data
(http://www.php.net/manual/en/function.mcrypt-encrypt.php)
echo "Encrypted data: " . bin2hex($encrypted_data) . "<br />"; // bin2hex
to compensate for random character values
$data = mcrypt_decrypt($td, $key, $encrypted_data, MCRYPT_MODE_CBC, $iv); // Decrypts data
(http://www.php.net/manual/en/function.mcrypt-decrypt.php)
echo trim($data);
?>
Thanks andrew, you saved me some time! Hope this saves someone else!
--was--
I needed to add
trim($data)
to andrew's example for it to remove some leftover hex garbage from the decrypted string.
Here's the working example:
<?php
$data = "Plaintext"; // Data to encrypt
(http://www.ciphersbyritter.com/glossary.htm#Encryption)
$key = "Secret"; // Encryption key (http://www.ciphersbyritter.com/glossary.htm#Key)
$td = MCRYPT_RIJNDAEL_256; // Encryption cipher (http://www.ciphersbyritter.com/glossary.htm#Cipher)
$iv_size = mcrypt_get_iv_size($td, MCRYPT_MODE_ECB); // Dependant on cipher/mode combination
(http://www.php.net/manual/en/function.mcrypt-get-iv-size.php)
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); // Creates an IV
(http://www.ciphersbyritter.com/glossary.htm#IV)
echo "Original data: $data<br />";
$encrypted_data = mcrypt_encrypt($td, $key, $data, MCRYPT_MODE_CBC, $iv); // Encrypts data
(http://www.php.net/manual/en/function.mcrypt-encrypt.php)
echo "Encrypted data: " . bin2hex($encrypted_data) . "<br />"; // bin2hex
to compensate for random character values
$data = mcrypt_decrypt($td, $key, $encrypted_data, MCRYPT_MODE_CBC, $iv); // Decrypts data
(http://www.php.net/manual/en/function.mcrypt-decrypt.php)
echo trim($data);
?>
Thanks andrew, you saved me some time! Hope this saves someone else!
http://php.net/manual/en/function.mcrypt-encrypt.php