Here is a simple but effective one that doesn't require any extra modules/libraries, like mycrypt, to be installed on the server.
First define your encryption key.
Code: Select all
define ("OrdKey","j0w06ej3gw29jn3h1");
The key can be any alpha-numeric string of any length.
Here is the encoding function.
Code: Select all
function ord_crypt_encode($data) {
$result = '';
for($i=0; $i<strlen($data); $i++) {
$char = substr($data, $i, 1);
$keychar = substr(OrdKey, ($i % strlen(OrdKey))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return bin2hex($result);
}
This would encode your data.
Code: Select all
$mydata = "How now brown cow.";
$mydata_encoded = ord_crypt_encode($mydata);
Here is the decoding function.
Code: Select all
function ord_crypt_decode($data) {
$result = '';
$data = @pack("H" . strlen($data), $data);
for($i=0; $i<strlen($data); $i++) {
$char = substr($data, $i, 1);
$keychar = substr(OrdKey, ($i % strlen(OrdKey))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}
And decoding your data.
Code: Select all
$mydata = ord_crypt_decode($mydata_encoded);
It is not as fast as using a library like mcrypt but it does do the job quite well and will work on any server since it doesn't have any library requirements.