The internal algorithm of PHP implementing URL encryption and decryption

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags .url address array broken content data discuz domain

Recent learning URL jump when new into three PHP encryption and decryption function is very good, looks like the discuz ... Use these encryption to decrypt the reason is because sometimes their own URL address was acquired after you want to break inside the value of the content you must know your key, no key, he should be broken for a while to know the content of your URL.

Pack them into a file and call it fun.php.

 

<?php


function Passport_encrypt ($txt, $key) {


Srand (Double) microtime () * 1000000);


$encrypt _key = MD5 (rand (0, 32000));


$ctr = 0;


$tmp = ';


for ($i = 0; $i < strlen ($txt); $i + +) {


$ctr = $ctr = = strlen ($encrypt _key)? 0: $ctr;


$tmp. = $encrypt _key[$ctr]. ($txt [$i] ^ $encrypt _key[$ctr + +]);


}


return Base64_encode (Passport_key ($tmp, $key));


}





function Passport_decrypt ($txt, $key) {


$txt = Passport_key (Base64_decode ($txt), $key);


$tmp = ';


for ($i = 0; $i < strlen ($txt); $i + +) {


$MD 5 = $txt [$i];


$tmp. = $txt [+ + $i] ^ $MD 5;


}


return $tmp;


}





function Passport_key ($txt, $encrypt _key) {


$encrypt _key = MD5 ($encrypt _key);


$ctr = 0;


$tmp = ';


for ($i = 0; $i < strlen ($txt); $i + +) {


$ctr = $ctr = = strlen ($encrypt _key)? 0: $ctr;


$tmp. = $txt [$i] ^ $encrypt _key[$ctr + +];


}


return $tmp;


}


?>

Here are some examples to deepen the understanding of these three cryptographic decryption functions

//string.php


<?php


include "fun.php";





$txt = "This is a test";


$key = "TestKey";


$encrypt = Passport_encrypt ($txt, $key);


$decrypt = Passport_decrypt ($encrypt, $key);





Echo $txt. <br><hr> ";


echo $encrypt. " <br><hr> ";


echo $decrypt. " <br><hr> ";


?>





//array.php


<?php


include "fun.php";





$array = Array (


"A" => "1",


"B" => "2",


"C" => "3",


"D" => "4"


);


//serialize produces a stored value, returns a string, Unserialize restores


$txt = serialize ($array);


$key = "TestKey";


$encrypt = Passport_encrypt ($txt, $key);


$decrypt = Passport_decrypt ($encrypt, $key);


$decryptArray = unserialize ($decrypt);





echo $txt. " <br><hr> ";


echo $encrypt. " <br><hr> ";


echo $decrypt. " <br><hr> ";


Echo $decryptArray. <br><hr> ";


?>

That's the key. When you want to jump to another URL, but also to ensure that your session is correct, you need to do a session. It seems that a company has a website and there is a forum, two places have to register and login, But do not want to let users in the home page to jump to the forum when the session fails, that is, log in once run the entire company

So how do you deal with the user's session?

Web pages are stateless, and if you want to continue using the session in a new page, you need to move the session from one place to another, as some may have thought, and I can call it by URL. and PHP has a variable that handles session, called $_ Session. Then convert the session that needs to be registered into an array. Well, you can write this:

//login.php


<?php


session_start ();


include "fun.php";


$_session["userid"];


$_session["username"];


$_session["Userpwd"];





header ("location:http://$domain/process.php?s=". UrlEncode (Passport_encrypt (serialize), " SessionKey "));


?>

In the example above, the $_session is converted into a stored data by using serialize, and then the data is encrypted by Passport_encrypt, and the reason for adding urlencode is that it is possible to generate unexpected coding when $_session is encrypted. So just in case (it turns out to be very effective)

Deal with the first

//process.php


<?php


session_start ();


include "fun.php";


$_session=unserialize (Passport_decrypt ($_get["s"], "SessionKey");


header ("location:http://$domain/index.php");


?>

First Use $_get["s"] to get the parameters of the URL, and then use Passport_decrypt to decrypt it, and then use Unserialize to restore its data to the original data, to this step, your page may be free to jump through the header.

This approach also involves security issues, if your URL address in the process of access by someone else, it is really embarrassed that people may not be able to decipher the contents of the URL, but others can also use this URL to login to your personal account Ah, mailbox accounts AH even bank accounts ( Of course, very few people will write this, I exception, haha) sounds very scared. But in fact you can cancel session processing on the jump page.

The following is the enhanced version of process.php

<?php


session_start ();


include_once "fun.php";


$_session=unserialize (Passport_decrypt ($_get["s"], "SessionKey");


if ((Time ()-$_session["Time") >30) {


header ("location:http://$domain/login.php");


unset ($_session["USERNAME"));


Unset ($_session["PASSWORD"));


}


Else


header ("location:http://$domain/index.php");


?>

Before you write this file, you have to set up the login

$_session["Time" = time ();

The main reason for setting this is to get the time on both sides, if the jump time more than 30 seconds, you can let it jump to the login.php login page, the slow speed of the customer is embarrassed but this also prevents if this URL was acquired, and this person did not log in 30 seconds, then I'm sorry. , timeout to log in again.

$_session["USERNAME"] and $_session["PASSWORD" These two things are user login needs to enter the username and password. The reason for canceling these two sessions is because if your URL is captured by someone, Although the person in more than 30 seconds to jump to the loign.php page, but those passed through the session is still valid, as long as the URL suffix login.php to index.php. That's him. Log in successfully.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.