note 44728 added to function.mcrypt-encrypt

From: Date: Sat, 14 Aug 2004 14:31:42 +0000
Subject: note 44728 added to function.mcrypt-encrypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-74625@lists.php.net to get a copy of this message
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! ---- Manual Page -- http://www.php.net/manual/en/function.mcrypt-encrypt.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44728 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44728&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44728&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#74625) next »