112 lines
2.7 KiB
PHP
112 lines
2.7 KiB
PHP
<?php
|
|
|
|
// WIP
|
|
|
|
// Caesar-Chiffre Funktion
|
|
function caesar($text, $shift) {
|
|
|
|
$textArr = str_split($text, 1);
|
|
// definition is gewohnheit aus angular
|
|
$textAscii = [];
|
|
$resAscii = [];
|
|
$resArr = [];
|
|
|
|
for($i = 0; $i > count($textArr); $i++){
|
|
// jeder buchstabe des textes wird zum array,
|
|
// in dessen ascii zeichen, $resAscii hinzugefügt
|
|
array_push($textAscii, ord($textArr[$i]));
|
|
// verschiebung
|
|
$resAscii[$i] =+ $shift;
|
|
// ascii buchstaben werden in ein wort gemacht
|
|
array_push($resArr, chr($resAscii[$i]));
|
|
}
|
|
// aus array ein wort machen
|
|
$result = implode("", $resArr);
|
|
|
|
$yo = print_r($textArr);
|
|
echo "<pre> $yo </pre>";
|
|
$yo = print_r($textAscii);
|
|
echo "<pre> $yo </pre>";
|
|
$yo = print_r($resAscii);
|
|
echo "<pre> $yo </pre>";
|
|
$yo = print_r($resArr);
|
|
echo "<pre> $yo </pre>";
|
|
|
|
return $result;
|
|
}
|
|
|
|
// Formularverarbeitung
|
|
$output = "";
|
|
$text = "";
|
|
$shift = 0;
|
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|
$text = $_POST["text"] ?? "";
|
|
$shift = intval($_POST["shift"] ?? 0);
|
|
|
|
if (isset($_POST["encrypt"])) {
|
|
$output = caesar($text, $shift);
|
|
}
|
|
|
|
if (isset($_POST["decrypt"])) {
|
|
$output = caesar($text, -$shift);
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Caesar Chiffre</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
background: #f4f4f4;
|
|
padding: 30px;
|
|
}
|
|
.box {
|
|
background: white;
|
|
padding: 20px;
|
|
max-width: 500px;
|
|
margin: auto;
|
|
border-radius: 8px;
|
|
}
|
|
textarea, input {
|
|
width: 100%;
|
|
padding: 10px;
|
|
margin-bottom: 10px;
|
|
}
|
|
button {
|
|
padding: 10px 20px;
|
|
margin-right: 10px;
|
|
}
|
|
label{
|
|
display: block;
|
|
margin-top: 10px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="box">
|
|
<h2>Caesar Chiffre</h2>
|
|
|
|
<form method="post">
|
|
<label for="text">Nachricht:</label>
|
|
<textarea name="text" rows="4"><?= htmlspecialchars($text) ?></textarea>
|
|
|
|
<label>Verschiebung:</label>
|
|
<input type="number" name="shift" min = 1 max = 26 value="<?= htmlspecialchars($shift) ?>">
|
|
<!--<input type="number" name="shift" value="<?= htmlspecialchars($shift) ?>">-->
|
|
|
|
<button type="submit" name="encrypt">Verschlüsseln</button>
|
|
<button type="submit" name="decrypt">Entschlüsseln</button>
|
|
|
|
<label for="result"> Verschlüsselte Nachricht:</label>
|
|
<textarea name="result" rows="4" readonly><?= htmlspecialchars($output ?: $text) ?></textarea>
|
|
</form>
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|