ROT13 transform
Simple example of the ROT13 algorithm. ROT13 is a simple function which rotates the
characters of a string 13 places forward.
Hint:
Since PHP 4+ you can use the PHP build-in function str_rot13.
This is just a example implementation to show how it works.
Author:
Chris Heschong
License:
Not given
Language:
PHP
Created:
04/17/2006
Updated:
12/10/2007
Tags:
encryption, string functions
Related links:
/** * ROT13 is a simple function which rotates the * characters of a string 13 places forward. * * @param string $string Input string * @return string Encrypted string */ function Rot13($string){ for($i=0; $i < strlen($string); $i++) { $c = ord($string[$i]); if ($c >= ord('n') & $c <= ord('z') | $c >= ord('N') & $c <= ord('Z')) $c -= 13; elseif ($c >= ord('a') & $c <= ord('m') | $c >= ord('A') & $c <= ord('M')) $c += 13; $string[$i] = chr($c); } return $string; }
$example_string = "ROT13 is easy..."; $rot13_encrypted = Rot13($example_string); print "ROT13 encrypted string: '$rot13_encrypted'\n"; $decrypted = Rot13($rot13_encrypted); print "Decrypted string: '$decrypted'\n"; /* Output: ROT13 encrypted string: 'EBG13 vf rnfl...' Decrypted string: 'ROT13 is easy...' */
Feel free to leave a message:
Add a comment
My solution:
function asciicesar15_crypt($uncrypted_password) {
$crypted_pw ="";
$strl_lng = strlen($uncrypted_password);
for($count = 0; $count < $strl_lng; $count++){
$digit = substr($uncrypted_password, $count);
$newdigit = chr(ord($digit) - 13);
$crypted_pw = $crypted_pw . $newdigit;
}
return $crypted_pw;
}
function asciicesar15_decrypt($uncrypted_password) {
$crypted_pw ="";
$strl_lng = strlen($uncrypted_password);
for($count = 0; $count < $strl_lng; $count++){
$digit = substr($uncrypted_password, $count);
$newdigit = chr(ord($digit) + 13);
$crypted_pw = $crypted_pw . $newdigit;
}
return $crypted_pw;
}