So… i tried to implement this design, and for some reason the decypher breaks down after about… 19 letters. (‘Break down’ meaning the decypher no longer correctly produces the correct letter.)
function encypher($instring) {
$lft = "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
$rgt = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
$lab = str_split($lft);
$rab = str_split($rgt);
$split = str_split(str_replace(' ','',$instring));
$outstring = "";
foreach($split AS $char) {
//Phase 1: Encode Character
$x = array_search(strtoupper($char),$rab); //Current position of the letter.
$outstring .= $lab[$x];
//Phase 2: Permute Left Alphabet
for($i = 0; $i < $x; $i++) {
array_push($lab,array_shift($lab));
}
$temp = $lab[1];
for($i = 2; $i < 14; $i++) {
$lab[$i-1] = $lab[$i];
}
$lab[13] = $temp;
//Phase 3: Permute Right Alphabet
for($i = 0; $i <= $x; $i++) {
array_push($rab,array_shift($rab));
}
$temp = $rab[2];
for($i = 3; $i < 14; $i++) {
$lab[$i-1] = $lab[$i];
}
$lab[13] = $temp;
}
return $outstring;
}
function decypher($instring) {
$lft = "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
$rgt = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
$lab = str_split($lft);
$rab = str_split($rgt);
$split = str_split(str_replace(' ','',$instring));
$outstring = "";
foreach($split AS $char) {
//Phase 1: Decode Character
$x = array_search($char,$lab); //Current position of the letter.
$outstring .= $rab[$x];
//Phase 2: Permute Left Alphabet
for($i = 0; $i < $x; $i++) {
array_push($lab,array_shift($lab));
}
$temp = $lab[1];
for($i = 2; $i < 14; $i++) {
$lab[$i-1] = $lab[$i];
}
$lab[13] = $temp;
//Phase 3: Permute Right Alphabet
for($i = 0; $i <= $x; $i++) {
array_push($rab,array_shift($rab));
}
$temp = $rab[2];
for($i = 3; $i < 14; $i++) {
$lab[$i-1] = $lab[$i];
}
$lab[13] = $temp;
}
return $outstring;
}
$x = encypher("Moo cow jump quick black fox");
echo $x;
echo "<br>".decypher($x);
Outputs:
YSDBDKIFFUMTFFZMALDZPPF
MOOCOWJUMPQSGJZSSHIKPTN
any idea where i’ve screwed up?
EDIT: Teach me to be lazy. Spotted the error. Permuting the right alphabet i forgot to update $lab into $rab.