While searching for some thing, I found a string encryption code sample for Visual Basic, and I just wanted the same in php, and did a port. Just like what F. Balena has done over there, this function is also used for both, encrypt as well as decrypt.
<?php
function stringEncrypt($str, $pass){
$strlen = strlen($str);
$passIndex = 0;
$passLen = strlen($pass);
// null passwords wont encode
if(strlen($pass) == 0)
return $str;
for ($i = 0; $i < $strlen; $i++){
// get the next char in the password
$passChr = ord($pass[($i % $passLen)]);
// encrypt one character in the string
$str[$i] = chr(ord($str[$i]) ^ $passChr);
// modify the character in the password (avoid overflow)
$pass[($i % $passLen)] = chr(($passChr + 17) & 255);
}
return $str;
}