php: Create Url Safe Encrypted String

Posted: August 6th, 2009 | Author: | Filed under: php | Tags: , , | 5 Comments »

The following code represents an easy way to create an encrypted string that can be passed in an URL.  While this method is adequate for email addresses, user names, and the like – it should not be used to encrypt mission-critical info or personal data such as Social Security numbers or credit card information.

Also, you will need to have the ‘mcrypt’ module enabled.  You can do this by opening your php.ini file and removing the ‘;’ just before ‘extension=php_mcrypt.dll’.

<?php
 
$cc=mencrypt("This is the string to be encrypted","key-change-this");
 
 
echo $cc.'<br />';
$dd=mdecrypt($cc,"key-change-this");
echo $dd.'<br />';
 
//encrypted value passed in url ie:
//http://yoursite.com/crypted.php?id=encrypted_string
if (isset($_GET["id"])){
$ee=$_GET["id"];
echo $ee.'<br />';
echo mdecrypt($ee,"key-change-this");
}
 
function mencrypt($input,$key){
$key = substr(md5($key),0,24);
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$encrypted_data = mcrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return trim(chop(url_base64_encode($encrypted_data)));
}
 
function mdecrypt($input,$key){
$input = trim(chop(url_base64_decode($input)));
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$key = substr(md5($key),0,24);
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$decrypted_data = mdecrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return trim(chop($decrypted_data));
}
 
function url_base64_encode($str){
return strtr(base64_encode($str),
array(
'+' => '.',
'=' => '-',
'/' => '~'
)
);
}
 
function url_base64_decode($str){
return base64_decode(strtr($str,
array(
'.' => '+',
'-' => '=',
'~' => '/'
)
));
}
?>

End Code.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • RSS
  • StumbleUpon

No related posts.


5 Comments on “php: Create Url Safe Encrypted String”

  1. 1 MarkofCain said at 11:14 am on October 30th, 2009:

    Thanks. Just what I was looking for. Is there a way to shorten the string — say down to 8 characters?

  2. 2 someone said at 4:41 pm on September 9th, 2010:

    his script has errors, try this

    for($i = 0; $i < 255; $i++){
    $cc=mencrypt($i,"key-change-this");
    echo $cc.'’;
    $dd=mdecrypt($cc,”key-change-this”);
    echo $dd.”;
    }

    error: 3, 31, …

  3. 3 Diana said at 1:21 pm on November 21st, 2010:

    You rock Joe. You saved me. I spent several hours until I found your blog.

    http://joe-riggs.com/blog/2009/08/php-create-url-safe-encrypted-string/

    Thanks.

  4. 4 Sherman Unkefer said at 12:10 pm on January 14th, 2011:

    I thought the __toString() would be useful. I was having the same problem that you are describing here. (did a google search and found your blog).

  5. 5 joe-riggs.com said at 1:44 am on June 5th, 2011:

    Php create url safe encrypted string.. Corking :)


Leave a Reply