Coastal Web
01-28-2010, 06:28 PM
Hi guys,
I frequently use these little encrypt/decrypt functions for various reason, alot of times the info is passed through URLS.
function encrypt($sData, $sKey='mysecretkey'){
$sResult = '';
for($i=0;$i<strlen($sData);$i++){
$sChar = substr($sData, $i, 1);
$sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
$sChar = chr(ord($sChar) + ord($sKeyChar));
$sResult .= $sChar;
}
return encode_base64($sResult);
}
function decrypt($sData, $sKey='mysecretkey'){
$sResult = '';
$sData = decode_base64($sData);
for($i=0;$i<strlen($sData);$i++){
$sChar = substr($sData, $i, 1);
$sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
$sChar = chr(ord($sChar) - ord($sKeyChar));
$sResult .= $sChar;
}
return $sResult;
}
function encode_base64($sData){
$sBase64 = base64_encode($sData);
return strtr($sBase64, '+/', '-_');
}
function decode_base64($sData){
$sBase64 = strtr($sData, '-_', '+/');
return base64_decode($sBase64);
}
Which will result in something like this:
01-28-2010 gets encrypted into: YJOSpaycmGJhYQ==
I was wondering if there would be an easy way to make it so that this function would only use alpha numeric characters (to make it more url safe).
...A-Z, a-z, 0-9...
I didn't write this code and can't remember where l found it but if l was able to tweak it so that it only used alpha numeric that would be awesome.
Any ideas, or alternate function snippets you can share?
Thanks!
I frequently use these little encrypt/decrypt functions for various reason, alot of times the info is passed through URLS.
function encrypt($sData, $sKey='mysecretkey'){
$sResult = '';
for($i=0;$i<strlen($sData);$i++){
$sChar = substr($sData, $i, 1);
$sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
$sChar = chr(ord($sChar) + ord($sKeyChar));
$sResult .= $sChar;
}
return encode_base64($sResult);
}
function decrypt($sData, $sKey='mysecretkey'){
$sResult = '';
$sData = decode_base64($sData);
for($i=0;$i<strlen($sData);$i++){
$sChar = substr($sData, $i, 1);
$sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
$sChar = chr(ord($sChar) - ord($sKeyChar));
$sResult .= $sChar;
}
return $sResult;
}
function encode_base64($sData){
$sBase64 = base64_encode($sData);
return strtr($sBase64, '+/', '-_');
}
function decode_base64($sData){
$sBase64 = strtr($sData, '-_', '+/');
return base64_decode($sBase64);
}
Which will result in something like this:
01-28-2010 gets encrypted into: YJOSpaycmGJhYQ==
I was wondering if there would be an easy way to make it so that this function would only use alpha numeric characters (to make it more url safe).
...A-Z, a-z, 0-9...
I didn't write this code and can't remember where l found it but if l was able to tweak it so that it only used alpha numeric that would be awesome.
Any ideas, or alternate function snippets you can share?
Thanks!