Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 加密和解密AES_Php_Html_Encryption_Cryptography_Aes - Fatal编程技术网

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 = ...;
    }
    

    问题是每当你点击任何提交按钮时,你的全部数据都会被重新提交,并且一次又一次地加密字符串。令人难过的是,人们没有响应。