test_batch/chefr.php
2025-03-15 08:27:47 +03:00

105 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Шифрование/Расшифровка</title>
</head>
<body>
<form method="post" action="">
<label for="text">Введите текст:</label><br>
<textarea id="text" name="text" rows="4" cols="50"></textarea><br>
<input type="radio" id="encrypt" name="action" value="encrypt" checked>
<label for="encrypt">Шифровать</label><br>
<input type="radio" id="decrypt" name="action" value="decrypt">
<label for="decrypt">Расшифровывать</label><br>
<input type="submit" value="Отправить">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$text = $_POST['text'];
$action = $_POST['action'];
// Пример словаря для шифрования и расшифровки
$encryptionDictionary = [
'a' => 'xyz',
'b' => 'qwe',
'c' => 'rty',
'd' => 'uio',
'e' => 'pas',
'f' => 'dfg',
'g' => 'hjk',
'h' => 'lzx',
'i' => 'cvb',
'j' => 'nm',
'k' => 'bnm',
'l' => 'poi',
'm' => 'lkj',
'n' => 'mnb',
'o' => 'poiuytrewq',
'p' => 'asdfghjkl',
'q' => 'zxcvbnm',
'r' => 'qwertyuiop',
's' => 'asdfghjklzxcvbnm',
't' => 'qwertyuiopasdfghjkl',
'u' => 'zxcvbnmqwertyuiop',
'v' => 'asdfghjklzxcvbnmqwertyuiop',
'w' => 'qwertyuiopasdfghjklzxcvbnm',
'x' => 'zxcvbnmqwertyuiopasdfghjkl',
'y' => 'qwertyuiopasdfghjklzxcvbnm',
'z' => 'zxcvbnmqwertyuiopasdfghjkl'
];
// Создаем словарь для расшифровки
$decryptionDictionary = array_flip($encryptionDictionary);
function encryptText($text, $dictionary) {
$encryptedText = '';
$text = strtolower($text); // Приводим текст к нижнему регистру для простоты
for ($i = 0; $i < strlen($text); $i++) {
$char = $text[$i];
if (isset($dictionary[$char])) {
$encryptedText .= $dictionary[$char];
} else {
$encryptedText .= $char; // Если символ не найден в словаре, оставляем его без изменений
}
}
return $encryptedText;
}
function decryptText($text, $dictionary) {
$decryptedText = '';
$text = strtolower($text); // Приводим текст к нижнему регистру для простоты
$keys = array_keys($dictionary);
$values = array_values($dictionary);
while ($text != '') {
$found = false;
foreach ($values as $index => $value) {
if (strpos($text, $value) === 0) {
$decryptedText .= $keys[$index];
$text = substr($text, strlen($value));
$found = true;
break;
}
}
if (!$found) {
$decryptedText .= $text[0]; // Если символ не найден в словаре, оставляем его без изменений
$text = substr($text, 1);
}
}
return $decryptedText;
}
if ($action == 'encrypt') {
$result = encryptText($text, $encryptionDictionary);
} else {
$result = decryptText($text, $decryptionDictionary);
}
echo "<h2>Результат:</h2>";
echo "<pre>$result</pre>";
}
?>
</body>
</html>