Hi guys
I'm setting up a new site where people can play different word games. Now the id for each game is based on the auto increment number in the mysql db.
I thought this was a bit boring and didn't want to give away the number of games played etc so decided to locate some sort of encryption code to encrypt an id coming out of the db to display and urlencode so when it’s added to the querystring, it can be unencoded, decrypted and queried against the db. I found this:
It works quite well, only problem is for anything less than 100 it use '=' in the code.
Now this isn’t a technical issue as I urlencode()/urldecode() at either side but I thought it looked a bit ugly.
Anyone know of a neater looking version? There’s no security issue with it and the page checks to make sure the decrypted string is_numeric, it's purely an aesthetic issue.
Cheers
I'm setting up a new site where people can play different word games. Now the id for each game is based on the auto increment number in the mysql db.
I thought this was a bit boring and didn't want to give away the number of games played etc so decided to locate some sort of encryption code to encrypt an id coming out of the db to display and urlencode so when it’s added to the querystring, it can be unencoded, decrypted and queried against the db. I found this:
Code:
$key = "This is the key";
$string = "This is the string";
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 base64_encode($result);
}
function decrypt($string, $key) {
$result = '';
$string = base64_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;
}
It works quite well, only problem is for anything less than 100 it use '=' in the code.
Now this isn’t a technical issue as I urlencode()/urldecode() at either side but I thought it looked a bit ugly.
Anyone know of a neater looking version? There’s no security issue with it and the page checks to make sure the decrypted string is_numeric, it's purely an aesthetic issue.
Cheers