dracolytch wrote:
I need to do some encryption/decryption on some strings,
so that I can pass information in the URL in plain sight.
Why is this preferable to sessions?
Unfortunately, I have little control over this particular
server, so mcrypt is out.
If you don't need anything industrial strength, the simple
Vernam algorithm should help:
function vernam_encrypt($strCryptThis, $strKey) {
$strEncrypted = '';
$n = strlen($strCryptThis);
$k = strlen($strKey);
$j = 0;
for ($i = 0; $i < $n; $i++) {
$iKeyChar = ord($strKey{$j});
$iStringChar = ord($strCryptThis{$i});
$iCryptChar = $iKeyChar ^ $iStringChar;
$strEncrypted = $strEncrypted . chr($iCryptChar);
if ($j >= $k) {
$j = 0;
} else {
$j = $j + 1;
}
}
return $strEncrypted;
}
function vernam_decrypt($strEncrypted, $strKey) {
$strDecrypted = '';
$n = strlen($strEncrypted);
$k = strlen($strKey);
$j = 0;
for ($i = 0; $i < $n; $i++) {
$iKeyChar = ord($strKey{$j});
$iStringChar = ord($strEncrypted{$i});
$iDeCryptChar = $iKeyChar ^ $iStringChar;
$strDecrypted = $strDecrypted . chr($iDeCryptChar);
if ($j >= $k) {
$j = 0;
} else {
$j = $j + 1;
}
}
return $strDecrypted;
}
Cheers,
NC