This is string cipher function:
<HTML>
<head>
<title></title>
</head>
<BODY>
<?php
//functions for num manipulation
function add_1 ($num)
{
return (($num+1) % 26);
}
function sub_1 ($num)
{
return (($num+25) % 26);
}
function swap_2 ($num)
{
if ($num == 0)
return ($num + 1);
else
return ($num - 1);
}
function swap_26 ($num)
{
return (25 - $num);
}
function lower_letter ($char_string)
{
return ((ord($char_string) >= ord('a')) &&
(ord($char_string) <= ord('z')));
}
function upper_letter ($char_string)
{
return ((ord($char_string) >= ord('A')) &&
(ord($char_string) <= ord('Z')));
}
function letter_cipher ($char_string, $code_func)
{
if (!upper_letter($char_string) ||
lower_letter($char_string))
return ($char_string);
if (upper_letter($char_string))
$base_num = ord ('A');
else
$base_num = ord ('a');
$char_num = ord ($char_string) - $base_num;
return (chr ($base_num + ($code_func ($char_num) % 26)));
}
function string_cipher ($message, $cipher_func)
{
$coded_message = "";
$message_length = strlen($message);
for ($index = 0;
$index < $message_length;
$index++)
$coded_message .= letter_cipher($message[$index], $cipher_func);
return ($coded_message);
}
//test cipher code
$original = "WHY WHY WHY must must ";
print ("Original msg is $original<BR>");
$coding_array = array('add_1','sub_1','swap_2','swap_26');
for ($count = 0; $count < sizeof($coding_array);
$count++)
{
$code= $coding_array[$count];
$coded_message = string_cipher($original,$code);
print ("$code encoding is: $coded_message<BR>");
}
?>
</BODY>
</HTML>
The result is:
Original msg is WHY WHY WHY must must
add_1 encoding is: XIZ XIZ XIZ must must
sub_1 encoding is: VGX VGX VGX must must
swap_2 encoding is: VGX VGX VGX must must
swap_26 encoding is: DSB DSB DSB must must
I dont know why lower letters dont work:bemused: