PHP Encrypt and Decrypt Code

This PHP functions will help you to encrypt and decrypt your string to the complex code that no one can understand the original string.

For the security reason, this function should be used to encode your secret data to the unreadable string. However, we might know the MD5 or SHA which is the popular PHP built-in encrypting function and cannot be decrypted in the previous time. Now, MD5 is already cracked down with some online website or library that can do that job well.

Now, we introduce you the hand-made functions that is reliable and secured enough to work with your project. So, now let's see how it works.

INSTRUCTION

In your PHP files, place them at the top of your file or anywhere before where it will be used.
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.

Here is how to use:
$mypassword = encodeString('thismypassword','thisismykey');
//this will return: 26p5f4n4h4r4l464r4n4o4v5l474

Similar Tutorials

Comments