You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.5 KiB
60 lines
1.5 KiB
<?php
|
|
|
|
function encrypt($data, $key)
|
|
{
|
|
$encrypted = '';
|
|
for ($i = 0; $i < strlen($data); $i++) {
|
|
$encrypted .= $data[$i] ^ $key[$i % strlen($key)];
|
|
}
|
|
return base64_encode($encrypted);
|
|
}
|
|
|
|
function decrypt($data, $key)
|
|
{
|
|
$data = base64_decode($data);
|
|
$decrypted = '';
|
|
for ($i = 0; $i < strlen($data); $i++) {
|
|
$decrypted .= $data[$i] ^ $key[$i % strlen($key)];
|
|
}
|
|
return $decrypted;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$data = $_POST['data'] ?? '';
|
|
$key = $_POST['key'] ?? '';
|
|
|
|
if (!empty($data) && !empty($key)) {
|
|
if (isset($_POST['encrypt'])) {
|
|
$encrypted_data = encrypt($data, $key);
|
|
echo "加密後的資料:" . $encrypted_data . "<br>";
|
|
} elseif (isset($_POST['decrypt'])) {
|
|
$decrypted_data = decrypt($data, $key);
|
|
echo "解密後的資料:" . $decrypted_data . "<br>";
|
|
}
|
|
} else {
|
|
echo "請輸入資料和金鑰。";
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html>
|
|
|
|
<head>
|
|
<title>加密解密表單</title>
|
|
</head>
|
|
|
|
<body>
|
|
<form method="post" action="">
|
|
<label for="data">請輸入要加密或解密的資料:</label><br>
|
|
<input type="text" id="data" name="data"><br><br>
|
|
|
|
<label for="key">請輸入金鑰:</label><br>
|
|
<input type="text" id="key" name="key" value="masada@<?php echo date("Y"); ?>"><br><br>
|
|
|
|
<input type="submit" name="encrypt" value="加密資料">
|
|
<input type="submit" name="decrypt" value="解密資料">
|
|
</form>
|
|
</body>
|
|
|
|
</html>
|