is there any way to make 2 way encryption/decryption for an integer (or string) Please note that I am not looking for encoding
i need something like this
crypting (100) --> 24694
crypting (101) --> 9564jh4 or 45216 or gvhjdfT or whatever ...
decrypting (24694) --> 100
I don't need encoding because it`s bijective
base64_encode(100) -->MTAw
base64_encode(101) -->MTAx
I hope I will find a way here to encrypt/decrypt PURE NUMBERS (computer love numbers, it's faster)
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;
}
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);
}
Have you tried looking into ROT-13?
More serious answer: from this SO answer, you can use:
function numhash($n) {
return (((0x0000FFFF & $n) << 16) + ((0xFFFF0000 & $n) >> 16));
}
numhash(42); // 2752512
numhash(numhash(42)); // 42
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With