-
-
Save akbarabustang/e7e608efdead98b2bf85d33972a32c45 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