php: Create Url Safe Encrypted String


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’.

';
$dd=mdecrypt($cc,"key-change-this");
echo $dd.'
'; //encrypted value passed in url ie: //http://yoursite.com/crypted.php?id=encrypted_string if (isset($_GET["id"])){ $ee=$_GET["id"]; echo $ee.'
'; 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.


17 responses to “php: Create Url Safe Encrypted String”

  1. 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, …

  2. 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).

  3. Hi again ,
    I know its old post but it helped me and might help to others.
    As I state before it decryption didnt work with :
    String to encrypt “Invoice-116.pdf ”

    Reason is that the encrypted string has space in the end.

    So in the function “mdecrypt” first line
    $input = trim(chop(url_base64_decode($input)));

    Trim and Chop remove this space and it result with corrupted string.

  4. Acrescentando Somatodrol à sua dieta, você vai
    poder amenizar os impactos dos treinos y también acentuar seus
    exercícios.

  5. Just want to say your article is as surprising.
    The clearness to your post is just excellent and that i can assume you’re an expert
    on this subject. Well with your permission let me to snatch your RSS
    feed to stay up to date with impending post. Thank you a million and please keep up the enjoyable work.

  6. Thanks for every other fantastic post. The place else could anybody get
    that type of information in such a perfect approach of
    writing? I have a presentation subsequent week, and I am at the search for such information.

  7. I almost never leave a response, however i did some searching and
    wound up here php: Create Url Safe Encrypted String – Joe Riggs Bloggs.
    And I actually do have 2 questions for you if it’s allright.
    Could it be simply me or does it give the impression like a few of these responses look like written by brain dead folks?
    😛 And, if you are posting at other online social
    sites, I’d like to follow everything new you have to post.
    Could you make a list of all of all your communal pages like your Facebook page, twitter feed, or linkedin profile?

  8. Along with the car sales, the Japanese used car sellers also supply the dismantled
    parts of the vehicles in cube containers
    of size 20ft or 40 ft. So, you are not required to move
    from one dealer to another for ideal model. According to Campbell Fuller, spokesman for the ICA, buyers need
    to contact their insurer before agreeing to
    a purchase.

  9. You have made some good points there. I checked on the neet to learn mmore abou the issue and found most
    individuals will go long with your views on this site.