-
-
Save fadhil-riyanto/6da775b2d8535d7c330cc375b195be1c to your computer and use it in GitHub Desktop.
PHP custom encode decode functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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