Forum Moderators: coopster
I have the need for a function (preferably a stock php function) that would encrypt a string of numbers in a fashion that could then be decrypted into it's original form at some later date.
I know md5() will encrypt, but of course you can't go the other way.
This doesn't need to be military/NSA strength by any means; I just need a way to scramble this string so that employees can't use it, but then be un-scrambled so management can.
Any thoughts?
Neophyte
Encode: base64_encode [us2.php.net]
Decode: base64_decode [us2.php.net]
So they want something that has a string/key pair.
I looked at mcrypt and my head started to swim - I still consider my self an apprentice at php - but I did come across the following function-pair script (that uses a key) in the user notes section (originally written by Anonymous and then modified by another user to include a base64-twist) that looks promising:
<?php
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;
}
?>
I haven't tried it yet...has anyone else? Anyone have an opinion on it?
Thanks to all so far for weighing in on this topic.
Neophyte
well, they have a bit of an issue, anything you do will be very easy to find the encryption method and run it through anything. Especially if you are using core php.
it might be simple if you had the admin on another site and could store the key there, but even then an employee could get it if they could see the code.
I guess the question is what type of employee they are trying to hide from. They can't hide this from anyone who can access code.
if the people they are trying to hide from don't have any possible access to code then you could use the function you found above and it should work just fine.