Skip to content

Instantly share code, notes, and snippets.

@fadhil-riyanto
Forked from LogIN-/encode_decode.php
Created May 14, 2021 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fadhil-riyanto/6da775b2d8535d7c330cc375b195be1c to your computer and use it in GitHub Desktop.
Save fadhil-riyanto/6da775b2d8535d7c330cc375b195be1c to your computer and use it in GitHub Desktop.
PHP custom encode decode functions
<?php
function encode($string,$key) {
$key = sha1($key);
$strLen = strlen($string);
$keyLen = strlen($key);
for ($i = 0; $i < $strLen; $i++) {
$ordStr = ord(substr($string,$i,1));
if ($j == $keyLen) { $j = 0; }
$ordKey = ord(substr($key,$j,1));
$j++;
$hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
}
return $hash;
}
function decode($string,$key) {
$key = sha1($key);
$strLen = strlen($string);
$keyLen = strlen($key);
for ($i = 0; $i < $strLen; $i+=2) {
$ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
if ($j == $keyLen) { $j = 0; }
$ordKey = ord(substr($key,$j,1));
$j++;
$hash .= chr($ordStr - $ordKey);
}
return $hash;
}
$encoded = encode("help me vanish" , "ticket_to_haven");
echo $encoded;
echo "\n";
$decoded = decode($encoded, "ticket_to_haven");
echo $decoded;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment