那些日子我正在尝试加密功能,但是在最后几小时我遇到了一些问题。
首先,我使该功能仅使用字母和符号,并且工作完美,但是当我添加数字时,一切都开始出错了。
没有数字的代码:
function encryptVerman($text, $length_text) {
//GLOBAL VARIABLES
$textToNumbersArray = array("A" => 0, "B" => 1, "C" => 2, "D" => 3, "E" => 4, "F" => 5, "G" => 6,"H" => 7, "I" => 8, "J" => 9, "K" => 10, "L" => 11, "M" => 12, "N" => 13, "O" => 14, "P" => 15, "Q" => 16, "R" => 17, "S" => 18, "T" => 19, "U" => 20, "V" => 21, "W" => 22, "X" => 23, "Y" => 24, "Z" => 25);
$numbersToTextArray = array(0 => "A", 1 => "B", 2 => "C", 3 => "D", 4 => "E", 5 => "F", 6 => "G", 7 => "H", 8 => "I", 9 => "J", 10 => "K", 11 => "L", 12 => "M", 13 => "N", 14 => "O", 15 => "P", 16 => "Q", 17 => "R", 18 => "S", 19 => "T", 20 => "U", 21 => "V", 22 => "W", 23 => "X", 24 => "Y", 25 => "Z");
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//KEY
$charactersLength = strlen($characters);
$key = "";
for ($i=0; $i < $length_text; $i++) {
$key .= $characters[rand(0, $charactersLength - 1)];
$key = (string)$key;
}
//1. KEY AND TEXT TO NUMBERS
$keyToNumbers = array();
$textToNumbers = array();
for ($i=0; $i < strlen($key); $i++) {
foreach ($textToNumbersArray as $key_foreach_to => $value_foreach_to) {
if ($key[$i] == $key_foreach_to) {
$keyToNumbers[] = (int)$value_foreach_to;
}
if ($text[$i] == $key_foreach_to) {
$textToNumbers[] = (int)$value_foreach_to;
}
}
}
//2. SUM BETWEEN KEYNUMBERS AND TEXTNUMBERS
$sumKeyAndText = array();
for ($i=0; $i < count($keyToNumbers); $i++) {
$sumKeyAndText[] = ($keyToNumbers[$i] + $textToNumbers[$i]);
if ($sumKeyAndText[$i] > 25) {
$sumKeyAndText[$i] = $sumKeyAndText[$i] - 26;
} else {
$sumKeyAndText[$i];
}
}
for ($i=0; $i < count($sumKeyAndText); $i++) {
echo $key . " Key" . "<br>";
echo $text . " Text" . "<br>";
echo $keyToNumbers[$i] . " Key to Numbers" . "<br>";
echo $textToNumbers[$i] . " Text to Numbers" . "<br>";
echo $sumKeyAndText[$i] . " Sum Key and Text" . "<br>";
echo "<br>";
}
这里有html表格:
<form action="" method="POST">
<input type="text" name="text">
<input type="submit" name="submit">
</form>
<?php
include("includes/encrypt.php");
if(isset($_POST["submit"])) {
$text = "";
$text = $_POST["text"];
$text = strtoupper($text);
$text_length = strlen($text);
$text = (string)$text;
echo encryptVerman($text, $text_length);
}
?>
然后当我们将两个数组中的数字添加到随机密钥生成器中时,我们总结了10个if方法出现问题。
首先,如果键不是数字,则操作完成两次。但是当钥匙是一个字母时,操作系统会完成两次。
带数字的代码:
function encryptVerman($text, $length_text) {
//GLOBAL VARIABLES
$textToNumbersArray = array("A" => 0, "B" => 1, "C" => 2, "D" => 3, "E" => 4, "F" => 5, "G" => 6,"H" => 7, "I" => 8, "J" => 9, "K" => 10, "L" => 11, "M" => 12, "N" => 13, "O" => 14, "P" => 15, "Q" => 16, "R" => 17, "S" => 18, "T" => 19, "U" => 20, "V" => 21, "W" => 22, "X" => 23, "Y" => 24, "Z" => 25, "0" => 26, "1" => 27, "2" => 28, "3" => 29, "4" => 30, "5" => 31, "6" => 32, "7" => 33, "8" => 34, "9" => 35);
$numbersToTextArray = array(0 => "A", 1 => "B", 2 => "C", 3 => "D", 4 => "E", 5 => "F", 6 => "G", 7 => "H", 8 => "I", 9 => "J", 10 => "K", 11 => "L", 12 => "M", 13 => "N", 14 => "O", 15 => "P", 16 => "Q", 17 => "R", 18 => "S", 19 => "T", 20 => "U", 21 => "V", 22 => "W", 23 => "X", 24 => "Y", 25 => "Z", 26 => "0", 27 => "1", 28 => "2", 29 => "3", 30 => "4", 31 => "5", 32 => "6", 33 => "7", 34 => "8", 35 => "9");
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//KEY
$charactersLength = strlen($characters);
$key = "";
for ($i=0; $i < $length_text; $i++) {
$key .= $characters[rand(0, $charactersLength - 1)];
$key = (string)$key;
}
//1. KEY AND TEXT TO NUMBERS
$keyToNumbers = array();
$textToNumbers = array();
for ($i=0; $i < strlen($key); $i++) {
foreach ($textToNumbersArray as $key_foreach_to => $value_foreach_to) {
if ($key[$i] == $key_foreach_to) {
$keyToNumbers[] = (int)$value_foreach_to;
}
if ($text[$i] == $key_foreach_to) {
$textToNumbers[] = (int)$value_foreach_to;
}
}
}
//2. SUM BETWEEN KEYNUMBERS AND TEXTNUMBERS
$sumKeyAndText = array();
for ($i=0; $i < count($keyToNumbers); $i++) {
$sumKeyAndText[] = ($keyToNumbers[$i] + $textToNumbers[$i]);
if ($sumKeyAndText[$i] > 35) {
$sumKeyAndText[$i] = $sumKeyAndText[$i] - 36;
} else {
$sumKeyAndText[$i];
}
}
for ($i=0; $i < count($sumKeyAndText); $i++) {
echo $key . " Key" . "<br>";
echo $text . " Text" . "<br>";
echo $keyToNumbers[$i] . " Key to Numbers" . "<br>";
echo $textToNumbers[$i] . " Text to Numbers" . "<br>";
echo $sumKeyAndText[$i] . " Sum Key and Text" . "<br>";
echo "<br>";
}
谢谢!
答案 0 :(得分:0)
我发现了错误。发生的事情是数组上的“0”被重新接收为空值,因此我将其更改为“零”然后它起作用。在我只需要将“零”转换为“0”后就完成了!