Melhor código
Boa tarde pessoal,
Fiz uma implementação, e gostaria de sugestões de como melhor/reduzir códigos, obrigado!
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<center>
<B> P Q</B>
<form action=codifica.php method=post name="codificar" target="">
<select name="P">
<?php
foreach($primos as $pr)
echo "<option>" . $pr . "</option>";
?>
</select>
<select name="Q">
<?php
foreach($primos as $pr)
echo "<option>" . $pr . "</option>";
?>
</select>
<br><br>
<input type="submit" name="Operacao" value="Codificar">
<input type="submit" name="Operacao" value="Decodificar">
</form>
</center>
</body>
</html>
codifica.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<center>
<B> P Q</B>
<br>
<?php//pega os valores escolhidos e imprime na tela
$P=$_POST["P"];
$Q=$_POST["Q"];
$Z = ($P-1)*($Q-1);
echo " " . $P . " " . $Q;
echo "<B><BR> E</B>";
echo "<BR>
<form action=final.php method=post name=codificar target=>";
if($_POST["Operacao"]=="Codificar"){
$maxLength = 50;//limita em 30 caracteres
$Operacao = "Codificar";
echo "<select name=E>";
for($i=0;$i<100;$i++)
echo "<option>" . calculaE($Z) . "</option>";
echo "</select>";
}else{
$maxLength = 180;//limita em 30 caracteres
$Operacao = "Decodificar";
echo "<input name=E type=text value=0000 size=4 maxlength=4 />";
}
echo "<br><br>
<input name=txtMensagem type=text value=\"DIGITE A MENSAGEM AQUI\" size=120 maxlength=".$maxLength." />
<br>
<input type=submit name=Operacao value=".$Operacao.">
<input type=hidden name=P value=".$P.">
<input type=hidden name=Q value=".$Q.">
</form>";
?>
</center>
</body>
</html>
final.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
echo "<center>
<B> P Q</B>
<br>";
//pega os valores escolhidos e atribui variaveis globais
$P=$_POST["P"];
$Q=$_POST["Q"];
$E=$_POST["E"];
$Mensagem=$_POST["txtMensagem"];$MensagemCod=array();//será usado para pegar os valores das letras
$Z = ($P-1)*($Q-1);
$N = $P * $Q;
$Length = 0;
//passa a mensagem para maiúscula e remove os espaços
function upperMensagem($Mens){
return strtoupper($Mens);
}
if($_POST["Operacao"]=="Codificar"){
$Mensagem = upperMensagem($Mensagem);
$chaves = gera_chaves ($P,$Q,$E);
$D = $chaves[2];
echo " " . $P . " " . $Q;
echo "<BR><B> E D</B>";
echo "<BR> " . $E . " " . $D;
$encoded = encripta ($Mensagem, $E, $N);
echo "<br><br>Mensagem Original: <br><b>$Mensagem</b>\n";
echo "<br><br>Mensagem Codificada: <br><b>$encoded</b>\n";
}else{
$chaves = gera_chaves ($P,$Q,$E);
$D = $chaves[2];
echo " " . $P . " " . $Q;
echo "<BR><B> E D</B>";
echo "<BR> " . $E . " " . $D;
$decoded = decripta($Mensagem, $D, $N);
echo "<br><br>Mensagem Original: <br><b>$Mensagem</b>\n";
echo "<br><br>Mensagem Decodificada: <br><b>$decoded</b>\n";
}
?>
</center>
</body>
</html>Discussão (2)
Carregando comentários...