function encodeString($string,$key) { $key = sha1($key); $strLen = strlen($string); $keyLen = strlen($key); $j = 0; $hash =""; 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 decodeString($string,$key) { $key = sha1($key); $strLen = strlen($string); $keyLen = strlen($key); $j = 0; $hash = ""; 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; }
The function has 2 parameters: string & key
String: the input string to be encrypted or decrypted
Key: the chars that is used to secure your encrypted string. It's like the password to decrypt your encrypted string. It means that even though someone has this function and they also know your encrypted string, he/she still cannot get your original string from your encrypted one. The reason is that he/she needs to know your key used to encrypted.
$mypassword = encodeString('thismypassword','thisismykey'); //this will return: 26p5f4n4h4r4l464r4n4o4v5l474