Php 加密和解密AES
我想在我单击Php 加密和解密AES,php,html,encryption,cryptography,aes,Php,Html,Encryption,Cryptography,Aes,我想在我单击加密按钮后加密原始字符串,输入元素将不清除,在我单击解密按钮后,它将解密。我的问题是在我单击解密后,解密没有值,只有加密正在移动。有人能帮我吗 这是我单击解密后的输出。 这是我的密码 <?php /* * PHP mcrypt - Basic encryption and decryption of a string */ error_reporting(E_ALL ^ E_NOTICE); $secret_key = "thisismykey12345"; $iv =
加密按钮后加密原始字符串,输入元素将不清除,在我单击解密按钮后,它将解密
。我的问题是在我单击解密
后,解密
没有值,只有加密正在移动。有人能帮我吗
这是我单击解密后的输出。
这是我的密码
<?php
/*
* PHP mcrypt - Basic encryption and decryption of a string
*/
error_reporting(E_ALL ^ E_NOTICE);
$secret_key = "thisismykey12345";
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
if(isset($_POST['encrypt'])){
$string = $_POST['ostring'];
$encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv);
}
else if(isset($_POST['decrypt'])){
$decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secret_key, $encrypted_string, MCRYPT_MODE_CBC, $iv);
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post">
Original String <input type="text" name="ostring" value="<?php echo $string; ?>"><br>
<input type="submit" name="encrypt" value="Encrypt"><br>
Encrypted String <input type="text" style="width:500px;" name="encrypted" value="<?php echo $encrypted_string; ?>"><br>
<input type="submit" name="encrypt" value="Decrypt"><br>
Decrypted String <input type="text" style="width:500px" name="decrypted" value="<?php echo $decrypted_string; ?>"><br>
</form>
</form>
</body>
</html>
原始字符串此部分有两个逻辑缺陷:
if(isset($_POST['encrypt'])){
$string = $_POST['ostring'];
$encrypted_string = ...;
}
else if(isset($_POST['decrypt'])){
$decrypted_string = ...$encrypted_string...;
}
$decrypted\u string
永远不会被设置,因为它依赖于$encrypted\u string
。但是$encrypted_string
仅在执行路径进入第一个if
块并跳过elseif
块时才存在李>
此外,在加密之前,即使需要,您也不会检查ostring
是否可用
将两个执行路径分别放入if
块中:
if(isset($_POST['encrypt'],$_POST['ostring'])){
$encrypted_string = ...;
}
if(isset($_POST['decrypt'],$encrypted_string)){
$decrypted_string = ...;
}
问题是每当你点击任何提交按钮时,你的全部数据都会被重新提交,并且一次又一次地加密字符串。令人难过的是,人们没有响应。