Académique Documents
Professionnel Documents
Culture Documents
<head>
<title> CIFRADO PLAYFAIR </title>
</head>
<body>
<?
function playfair_decipher($key, $ciphertext){
$plaintext="";
$i = 0;
$length = strlen($ciphertext);
$a="";$b="";$a_ind=""; $b_ind=""; $a_row=""; $b_row=""; $a_col=""; $b_col="";//
se inicializan las variables
//se limpia la frace para que pueda ser cifrada
for($i = 0; $i < $length; $i+=2){
$a = strtolower($ciphertext[$i]);
$b = strtolower($ciphertext[$i+1]);
$a_ind=strpos($key, $a );
$b_ind=strpos($key, $b );
//se hacen los arreglos para tener columnas y filas
$a_row = bcdiv($a_ind,5,0);
$b_row = bcdiv($b_ind,5,0);
$a_col = $a_ind % 5;
$b_col = $b_ind % 5;
//se ponen las reglas de algoritmo dependiendo de la situacion es lo que
hara
if($a_row == $b_row){
if($a_col == 0){
$plaintext[$i] = $key[$a_ind + 4];
$plaintext[$i+1] = $key[$b_ind - 1];
}else if($b_col == 0){
$plaintext[$i] = $key[$a_ind - 1];
$plaintext[$i+1] = $key[$b_ind + 4];
}else{
$plaintext[$i] = $key[$a_ind - 1];
$plaintext[$i+1] = $key[$b_ind - 1];
}
}else if($a_col == $b_col){
if($a_row == 0){
$plaintext[$i] = $key[$a_ind + 20];
$plaintext[$i+1] = $key[$b_ind - 5];
}else if($b_row == 0){
$plaintext[$i] = $key[$a_ind - 5];
$plaintext[$i+1] = $key[$b_ind + 20];
}else{
$plaintext[$i] = $key[$a_ind - 5];
$plaintext[$i+1] = $key[$b_ind - 5];
}
}else{
$plaintext[$i] = $key[5*$a_row + $b_col];
$plaintext[$i+1] = $key[5*$b_row + $a_col];
}
}
return $plaintext;
}
=
=
=
=
bcdiv($a_ind,5,0);
bcdiv($b_ind,5,0);
$a_ind % 5;
$b_ind % 5;
if($a_row == $b_row){
//aqu estan los diferentes casos para poder regresar a el mensaje en clar
o original
//es muy similar a la forma del cifrado solo que se hacen las operacione
s inversas.
if($a_col == 4){
$ciphertext[$i] = $key[$a_ind - 4];
$ciphertext[$i+1] = $key[$b_ind + 1];
}else if($b_col == 4){
$ciphertext[$i] = $key[$a_ind + 1];
$ciphertext[$i+1] = $key[$b_ind - 4];
}else{
$ciphertext[$i] = $key[$a_ind + 1];
$ciphertext[$i+1] = $key[$b_ind + 1];
}
}else if($a_col == $b_col){
if($a_row == 4){
$ciphertext[$i] = $key[$a_ind - 20];
$ciphertext[$i+1] = $key[$b_ind + 5];
}else if($b_row == 4){
$ciphertext[$i] = $key[$a_ind + 5];
$ciphertext[$i+1] = $key[$b_ind - 20];
}else{
$ciphertext[$i] = $key[$a_ind + 5];
$ciphertext[$i+1] = $key[$b_ind + 5];
}
}else{
$ciphertext[$i] = $key[5*$a_row + $b_col];
$ciphertext[$i+1] = $key[5*$b_row + $a_col];
}
}while ($i < $length);
return $ciphertext;
}
function prepare_for_playfair($originaltext){
$originaltext="";
$ciphertext="";
$result="";
$plaintext="";
//para poder hacer que funcione dinamicamente al momento de colocar nuestra key,
se le va a restar al abecedario
//despues de ello se le agregan las letras al principio de nuestra cadena de let
ras y tenemos la tabla coorespondiente
$key="abcdefghiklmnopqrstuvwxz";
if(isset($_POST["abecedario"])){
$abecedario=$_POST["abecedario"];
$longitud=strlen($abecedario);
for($i=0;$i<$longitud;$i++){
$key=str_replace($abecedario[$i], "", $key);
}
$key=sprintf("%s$key",$_POST["abecedario"]);
}
if(isset($_POST["cadena"])&&isset($_POST["criptar"])){
$originaltext=$_POST["cadena"];
$plaintext=prepare_for_playfair($originaltext);
$plaintext=implode("",$plaintext);//paso arreglo a cadena
$ciphertext=playfair_encipher($key, $plaintext);
$ciphertext=implode("",$ciphertext);//paso arreglo a cadena
}
if(isset($_POST["cadena"])&&isset($_POST["desencriptar"])){
$ciphertext=$_POST["cadena"];
$result=playfair_decipher($key, $ciphertext);
$result=implode("",$result);//paso arreglo a cadena
}
?>
<?printf("Mcla original: %s<br>depurado:
%s<br>Cripto: %s<br>Mcla:
%s <hr>\n",
$originaltext,$plaintext,$ciphertext,$result);?>
<p> </p>
<form name="frm" method="post">
<p>Cadena <input type="text" name="cadena" />
<p>Key <input type="text" name="abecedario" />
<input type="submit" name="criptar" value="Cifra" />
<input type="submit" name="desencriptar" value="Descifra
" />
</p>
</form>
<p> </p>
</body>
</html>