note 78920 deleted from function.base64-encode by nlopess

From: Date: Fri, 02 Nov 2007 15:31:14 +0000
Subject: note 78920 deleted from function.base64-encode by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-131946@lists.php.net to get a copy of this message
Note Submitter: keith at phpdiary dot org ---- A neat, simple and secure encrypt decrypt function that is URL safe. (meaning you can transport the value through a http get or post). Since this is based on the base64_encode() function, you should expect the encoded data to take about 33% more space than the original data. Usage: encrypt(’Welcome to phpdiary.org’,’secretkey’); This will show: 0NjRxuHS2YvZ6JPVy-LJ3czX8qHU1dk= Now, let’s reverse it. decrypt(’0NjRxuHS2YvZ6JPVy-LJ3czX8qHU1dk=’,’secretkey’); This will show: Welcome to phpdiary.org See, it works! And now the code.. ## http://www.phpdiary.org/articles/simple-php-encrypt-function/ function encrypt($string, $key) { $result = ''; for($i=0; $i<strlen ($string); $i++) { $char = substr($string, $i, 1); $keychar = substr($key, ($i % strlen($key))-1, 1); $char = chr(ord($char)+ord($keychar)); $result.=$char; } return base64url_encode($result); } function decrypt($string, $key) { $result = ''; $string = base64url_decode($string); for($i=0; $i<strlen($string); $i++) { $char = substr($string, $i, 1); $keychar = substr($key, ($i % strlen($key))-1, 1); $char = chr(ord($char)-ord($keychar)); $result.=$char; } return $result; } function base64url_encode($plainText) { $base64 = base64_encode($plainText); $base64url = strtr($base64, '+/', '-_'); return ($base64url); } function base64url_decode($plainText) { $base64 = strtr($plainText, '-_', '+/'); return base64_decode($base64); }

« previous php.notes (#131946) next »