1
0
Fork 0
This commit is contained in:
Chenx221 2024-01-29 12:55:37 +08:00
parent 5f737cb5ab
commit 37c769d1e7
5 changed files with 159 additions and 0 deletions

9
15.test5.php Normal file
View File

@ -0,0 +1,9 @@
<?php
//Mhash已被hash替代
var_dump(hash_algos()); //支持的散列算法
echo '<br>';
$f = file_get_contents('data/7z2301-x64.exe'); //这里使用7-zip安装包为例
echo 'CRC32:' . strtoupper(hash('crc32b', $f));
echo '<br>';
echo 'SHA256:' . strtoupper(hash('sha256', $f));

7
15.work1.php Normal file
View File

@ -0,0 +1,7 @@
<?php
/*
* 实践练习1早在前面15.test2就已经完成
* 原题中的crypt()和md5()属于过时内容
* 我已经使用更现代的方式password_hash()和password_verify()实现
*/
include '15.test2.php';

54
15.work2.api.php Normal file
View File

@ -0,0 +1,54 @@
<?php
function apply_xor(string $data, string $key) : string
{
$result = '';
$l = strlen($data);
$kl = strlen($key);
for ($i = 0; $i < $l; $i++) {
// 获取当前字符的密钥字符
$keyChar = $key[$i % $kl];
// 对当前字符进行按位异或运算
$substr = $data[$i] ^ $keyChar;
$result .= $substr;
}
return $result;
}
function custom_encrypt(string $raw, string $key) : string
{
return apply_xor($raw, $key);
}
function custom_decrypt(string $encrypted, string $key) : string
{
return apply_xor($encrypted, $key);
}
// 如果请求是 POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 获取 POST 请求的原始数据
$post_data = file_get_contents('php://input');
// 将 JSON 数据解码为 PHP 数组
$data = json_decode($post_data, true);
// 如果有明文
if (isset($data['raw_text'])) {
// 加密
$raw_text = $data['raw_text'];
$key = $data['key'];
$enc_text = custom_encrypt($raw_text, $key);
echo $enc_text;
exit; // 加密完成后终止脚本执行
}
// 如果有密文
if (isset($data['enc_text'])) {
// 解密
$enc_text = $data['enc_text'];
$key = $data['key'];
$raw_text = custom_decrypt($enc_text, $key);
echo $raw_text;
exit; // 解密完成后终止脚本执行
}
}

88
15.work2.php Normal file
View File

@ -0,0 +1,88 @@
<?php
/*
* 已知问题:
* 不支持ascii码以外字符
* key长度超过data长度时,结果时data长度下key的计算结果
* 所以还是用现成的比较好的加密算法为好
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>加密解密</title>
</head>
<body>
<table>
<tr>
<td>
<label for="raw_text1">明文:</label>
</td>
<td>
<textarea name="raw_text" id="raw_text1"></textarea>
</td>
<td>
<button name="ev_bt" id="ev_bt1" onclick="req_enc()">加密↓</button>
</td>
</tr>
<tr>
<td>
<label for="key1">密钥:</label>
</td>
<td>
<textarea name="key" id="key1" required></textarea>
</td>
</tr>
<tr>
<td>
<label for="enc_text1">密文:</label>
</td>
<td>
<textarea name="enc_text" id="enc_text1"></textarea>
</td>
<td>
<button name="ev_bt" id="ev_bt2" onclick="req_dec()">解密↑</button>
</td>
</tr>
</table>
<script>
function req_enc() {
const rawText = document.getElementById("raw_text1").value;
const key = document.getElementById('key1').value;
const data = {raw_text: rawText, key: key};
fetch('15.work2.api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.text())
.then(data => {
document.getElementById("enc_text1").value = data;
})
.catch(error => console.error('Error:', error));
}
function req_dec() {
const encText = document.getElementById("enc_text1").value;
const key = document.getElementById('key1').value;
const data = {enc_text: encText, key: key};
fetch('15.work2.api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.text())
.then(data => {
document.getElementById("raw_text1").value = data;
})
.catch(error => console.error('Error:', error));
}
</script>
</body>

1
data/.gitignore vendored
View File

@ -1,3 +1,4 @@
/010 Editor 12.0.1 (1) (1).exe.protected
/010 Editor 12.0.1 (1).exe.protected
/010 Editor 12.0.1.exe.protected
/7z2301-x64.exe