我有这个函数的问题,它没有为我返回任何东西,我不知道我想获得两个字符串的异或的原因`
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (array_key_exists("back", $_POST)) {
header('Location: index.php');
exit;
}
}
function xor_this($string) {
// Let's define our key here
$key = ('ma');
// Our plaintext/ciphertext
$text =$string;
// Our output text
$outText = '';
// Iterate through each character
for($i=0;$i<strlen($text);)
{
for($j=0;$j<strlen($key);$j++,$i++)
{
$outText .= $text{$i} ^ $key{$j};
echo 'i='.$i.', '.'j='.$j.', '.$outText{$i}.'<br />'; //for debugging
echo $outText;
}
}
return $outText;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form name="confirm" action="index.php" method="POST">
<table>
<tr>
<td>La chaine a chiffrer :</td>
<td><?php echo $_POST['chaine']; ?></td>
</tr>
<tr>
<td>La cle de meme taille :</td>
<td><?php echo $_POST['cle']; ?></td>
</tr>
<tr>
<td>Le XOR :</td>
<td><?php echo xor_this($_POST['chaine']); ?></td>
</tr>
</table>
<input type="submit" name="back" value="retour"/>
</form>
</body>
</html>
`
你对这个问题有什么想法吗,我提前谢谢你
- - - - - - - - - - 编辑 - - - - - - - - - - - - -
我考虑了你的答案,但同样的问题:函数返回的字符串上没有显示任何内容,这是新代码:
<?php
session_start();
$flag = false;
function xor_this($chaine, $cle) {
$chiffre = '';
// Iterate through each character
for($i=0;$i<strlen($chiffre);)
{
$chiffre .= $chaine{$i} ^ $cle{$i};
echo $chiffre;
}
return $chiffre;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$data = array("chaine" => $_POST["chaine"], "cle" => $_POST["cle"]);
}
?>
<form name="logon" action="index.php" method="POST" >
<table>
<tr>
<td>La chaine a chiffrer :</td>
<td><input type="text" name="chaine" value="<?php if (!empty($data["chaine"])) echo $data["chaine"]; else echo ""; ?>" ></td>
</tr>
<tr>
<td>La cle de meme taille :</td>
<td><input type="text" name="cle" value="<?php if (!empty($data["cle"])) echo $data["cle"]; else echo ""; ?>" ></td>
<td>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (strlen($_POST['chaine']) <> strlen($_POST['cle']))
echo "la chaine et la cle sont de taille differente";
}
?>
</td>
</tr>
</table>
<input type="submit" value="Chiffrer">
<table>
<tr>
<td>La chaine chiffre : </td>
<td>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (strlen($_POST['chaine']) == strlen($_POST['cle']))
echo bin2hex(xor_this($_POST["chaine"],$_POST["cle"]));
}
?>
</td>
</tr>
</table>
</form>
</body>
</html>
你还有什么想法吗,谢谢