Click to See Complete Forum and Search --> : Encrypt/Decrypt a variable?


Lubox
03-23-2007, 08:10 AM
Hi

I need to send a parameter in the url (ie ...&myvar=123456), but I don't want the people using the link to know that it says 123456. But the receiving page itself needs to know that it is 123456.

So I need to encrypt the value somehow (before creating the link of course), and then decrypt it (i think).

Any built in functions which can do this for me?

Thanks
Lubox

itbeings
03-23-2007, 08:29 AM
if the url values are not sensitive.... you can use
'base64_encode('')' and decode it with 'base64_decode('')'
e.g
$value = base64_encode('My value');

$view = base64_decode('$value');

Lubox
03-23-2007, 08:32 AM
It is sensitive, I do not want people to be able to see that myvalue does really contain 123456 (or whatever). I'm currently trying to figure out mcrypt, but has just started to look at it.....

bokeh
03-23-2007, 08:40 AM
It is sensitiveIn what way is it sensitive? Whatever you use people will be able to access the URL. Or are you trying to obscure the URL of the previous and next page?

Lubox
03-23-2007, 08:46 AM
In what way is it sensitive? Whatever you use people will be able to access the URL. Or are you trying to obscure the URL of the previous and next page?

It's sensitive in the way that I don't want people following the link I created on one page to know the value of the parameter. But the page they land on should be able to know the value.

For example on the first page:


$myvalue = "123456";
$key = "mysecretkey";

$linkvalue = encrypt($myvalue,$key);
//linkvalue = "JHGSBSHJSHGKSGHYUUJSHSB12345"

$link .= "&myvalue=$linkvalue";

echo $link;


Then on the landing page of the link:


$key = "mysecretkey";
$myvalue = decrypt($_GET["myvalue"],$key);

//$myvalue is now "123456"


Lubox

bokeh
03-23-2007, 10:31 AM
<?php

$key = 'When shall we three meet again.';
$target = 'Please, Obscure Me!';

$encrypted = SimpleCipher($target, $key); // you would need to URL encode this for use as a $_GET value
$decrypted = SimpleCipher($encrypted, $key, true);

echo "Here's the source version: <b>$target</b><br>\n";
echo "Here's the encrypted version: <b>$encrypted</b><br>\n";
echo "Here's the decrypted version: <b>$decrypted</b><br>\n";


function SimpleCipher($target, $key, $decode = false)
{
for($i = 0; $i <256; $i++) $chars_array[] = chr($i);
$chars = implode($chars_array);
foreach($chars_array as $row)
{
$i = 0;
foreach($chars_array as $column)
{
$table[$row][$column] = $chars[$i++];
}
$chars = substr($chars, 1).substr($chars, 0, 1);
}
if(!$target or !$key) return false;
$len = strlen($target);
while(strlen($key) < $len) $key .= $key;
$output = '';
for($i = 0; $i < $len; $i++)
{
if($decode)
{
$letter_array = array_keys($table[$key[$i]], $target[$i]);
$output .= $letter_array[0];
}
else
{
$output .= $table[$key[$i]][$target[$i]];
}
}
return $output;
}

?>

Lubox
03-23-2007, 12:23 PM
Hi thank you for your effort. I won't pretend to have understood exactly how it works, but it is rather slow. If I'm generating 20 links using this function it'll take 4 seconds on my computer...

bokeh
03-23-2007, 04:45 PM
Hi thank you for your effort. I won't pretend to have understood exactly how it works, but it is rather slow. If I'm generating 20 links using this function it'll take 4 seconds on my computer...I just ran a test: 5000 words, (that's 10 A4 pages of 10pt font) time taken 230 milliseconds.

Either your computer is a 286 or there is a terrible bottleneck elsewhere in your code causing that.

Lubox
03-23-2007, 05:09 PM
Hm, maybe I did something wrong here, or maybe my comp is really strange atm..

This takes 4 seconds:


<?php
set_time_limit(0);

for($u = 0; $u <20; $u++){
$key = 'I was made for loving you BABY';
$target = 'Please, Obscure Me ffs!';

$encrypted = SimpleCipher($target, $key);
$decrypted = SimpleCipher($encrypted, $key, true);

}


function SimpleCipher($target, $key, $decode = false){

for($i = 0; $i <256; $i++){
$chars_array[] = chr($i);
}
$chars = implode($chars_array);
foreach($chars_array as $row)
{
$i = 0;
foreach($chars_array as $column)
{
$table[$row][$column] = $chars[$i++];
}
$chars = substr($chars, 1).substr($chars, 0, 1);
}
if(!$target or !$key){
return false;
}
$len = strlen($target);
while(strlen($key) < $len){
$key .= $key;
}
$output = '';
for($i = 0; $i < $len; $i++)
{
if($decode)
{
$letter_array = array_keys($table[$key[$i]], $target[$i]);
$output .= $letter_array[0];
}
else
{
$output .= $table[$key[$i]][$target[$i]];
}
}

return $output;
}

?>

bokeh
03-23-2007, 05:47 PM
This takes 4 secondsWhen looped like that it does appear to show a problem. Anyway I modified the function a bit and now that loop runs in 2 milliseconds.<?php

$key = 'I was made for loving you BABY';
$target = 'Please, Obscure Me ffs!';

$start = microtime(true);
for($u = 0; $u < 20; $u++)
{
$encrypted = SimpleCipher($target, $key);
$decrypted = SimpleCipher($encrypted, $key, true);
}
echo '<br>'.(microtime(true) - $start);



function SimpleCipher($target, $key, $decode = false)
{
static $chars;
static $chars_array;
if(!$target or !$key) return false;
if(!$chars)
{
for($i = 0; $i <256; $i++)
{
$chars_array[] = chr($i);
}
$chars = implode($chars_array);
foreach($chars_array as $row)
{
$i = 0;
foreach($chars_array as $column)
{
$table[$row][$column] = $chars[$i++];
}
$chars = substr($chars, 1).substr($chars, 0, 1);
}
}
$len = strlen($target);
while(strlen($key) < $len) $key .= $key;
$output = '';
for($i = 0; $i < $len; $i++)
{
if($decode)
{
$letter_array = array_keys($table[$key[$i]], $target[$i]);
$output .= $letter_array[0];
}
else
{
$output .= $table[$key[$i]][$target[$i]];
}
}
return $output;
}

?>

NightShift58
03-23-2007, 10:44 PM
$key = 'When shall we three meet again.';
$target = 'Please, Obscure Me!'; $key = 'I was made for loving you BABY';
$target = 'Please, Obscure Me ffs!'; What's going on here?

pcthug
03-24-2007, 12:05 AM
$key = 'When shall we three meet again.';
$target = 'Please, Obscure Me!'; $key = 'I was made for loving you BABY';
$target = 'Please, Obscure Me ffs!'; What's going on here?
:D

Back on topic; if this information is so secure, you should probably be storing/retrieveing via sessions or cookies on a per user basis.

bokeh
03-24-2007, 04:58 AM
What's going on here?What do you mean?

bokeh
03-24-2007, 11:23 AM
Oops! There was an error in that code. Try this:function SimpleCipher($target, $key, $decode = false)
{
static $table;
if(!$target or !$key) return false;
if(empty($table))
{
for($i = 0; $i <256; $i++)
{
$chars_array[] = chr($i);
}
$chars = implode($chars_array);
foreach($chars_array as $row)
{
$i = 0;
foreach($chars_array as $column)
{
$table[$row][$column] = $chars[$i++];
}
$chars = substr($chars, 1).substr($chars, 0, 1);
}
}
$len = strlen($target);
while(strlen($key) < $len) $key .= $key;
$output = '';
for($i = 0; $i < $len; $i++)
{
if($decode)
{
$letter_array = array_keys($table[$key[$i]], $target[$i]);
$output .= $letter_array[0];
}
else
{
$output .= $table[$key[$i]][$target[$i]];
}
}
return $output;
}

Lubox
03-24-2007, 12:14 PM
:D

Back on topic; if this information is so secure, you should probably be storing/retrieveing via sessions or cookies on a per user basis.

;)

The problem is that the page I'm going to use this in, is actually called as an external webpage from a system which does not make it possible to use sessions/cookies, because it will belong to the server handling the page, not the end user. Pretty stupid, and of course possible to work out, but not at this point.

But the stuff seems to be working as far as I can see..

Thanks all.

bokeh
03-24-2007, 12:22 PM
the stuff seems to be working as far as I can see..Did you try this modified code?

NightShift58
03-25-2007, 06:39 AM
What do you mean?It starting looking more like a dating site than a PHP one...

MrCoder
03-26-2007, 03:48 AM
It starting looking more like a dating site than a PHP one...

Single variable seeks new assignment since I have just had a bad break; after a case: var_dumped me and defined("MyHeart", "Broken");

NightShift58
03-26-2007, 03:58 AM
continue...

MrCoder
03-26-2007, 04:04 AM
Shouldn't that be "continue;"? :P

itbeings
03-26-2007, 04:15 AM
break;
What is going on?

MrCoder
03-26-2007, 04:56 AM
He started it *points at NightShift58*