Full working script with sample encryption catalog given here.
<?php
$encript = array(
'a'=>'GG',
'b'=>'HH',
'c'=>'II',
'A'=>'DD',
'B'=>'EE',
'B'=>'FF',
'1'=>'AA',
'2'=>'BB',
'3'=>'CC');
function decrypt($str,$encript)
{
$decript = array_flip($encript);
$str_arr = explode(",",$str);
$dec = "";
foreach($str_arr as $val)
{
$dec .= $decript[strtoupper(trim($val))];
}
return $dec;
}
function encrypt($str,$encript)
{
$str_arr = str_split($str);
$dec = "";
foreach($str_arr as $val)
{
$dec .= $encript[trim($val)].",";
}
return $dec;
}
$cypher = "AA,BB,DD,CC,EE,FF,GG,HH";
$text = "Ab1Ca";
echo decrypt($cypher,$encript);
echo "<br/>";
echo encrypt($text,$encript);
?>
- First we have to create the mapping for each character as associative array.
- In decrypt function first we have to flip the array.
- Then have to loop through each encoding string and build the real text from flipped array.
- In encryption split the string as single letter array.
- Loop through the array and build encryption using the mapping array.
The working out put available at following url: http://sugunan.net/demo/str1.php