note 105166 added to function.mcrypt-encrypt

From: Date: Mon, 01 Aug 2011 10:24:02 +0000
Subject: note 105166 added to function.mcrypt-encrypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-182035@lists.php.net to get a copy of this message
Simple DES-ECB binary string encryption/decryption (no initialization vector). I implement PKCS7-padding the right way. This is better than Mcrypt default zero-padding. <?php function encrypt($str, $key) { # Add PKCS7 padding. $block = mcrypt_get_block_size('des', 'ecb'); if (($pad = $block - (strlen($str) % $block)) < $block) { $str .= str_repeat(chr($pad), $pad); } return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); } function decrypt($str, $key) { $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); # Strip padding out. $block = mcrypt_get_block_size('des', 'ecb'); $pad = ord($str[($len = strlen($str)) - 1]); if ($pad && $pad < $block && preg_match( '/' . chr($pad) . '{' . $pad . '}$/', $str ) ) { return substr($str, 0, strlen($str) - $pad); } return $str; } ?> I am using these functions to encrypt query strings: <?php const KEY = "\xc8\xd9\xb9\x06\xd9\xe8\xc9\xd2"; # Change this # Send encrypted GET query string $query = encrypt('field1=value1&field2=value2', KEY); echo '<a href="script.php?' . urlencode($query) . '">link</a>'; # Receive encrypted GET query string $query = decrypt(urldecode($_SERVER['QUERY_STRING']), KEY); parse_str($query, $query); $query = filter_var_array($query, array(...)); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 92.27.17.145 ---- Manual Page -- http://www.php.net/manual/en/function.mcrypt-encrypt.php Edit -- https://master.php.net/note/edit/105166 Del: integrated -- https://master.php.net/note/delete/105166/integrated Del: useless -- https://master.php.net/note/delete/105166/useless Del: bad code -- https://master.php.net/note/delete/105166/bad+code Del: spam -- https://master.php.net/note/delete/105166/spam Del: non-english -- https://master.php.net/note/delete/105166/non-english Del: in docs -- https://master.php.net/note/delete/105166/in+docs Del: other reasons-- https://master.php.net/note/delete/105166 Reject -- https://master.php.net/note/reject/105166 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#182035) next »