Php AES-加密字符串未解密。

Php AES-加密字符串未解密。,php,encryption,cryptography,aes,Php,Encryption,Cryptography,Aes,我有一个3php文件,一个是index.php原始字符串在哪里,encrypt.php我在哪里加密原始字符串,最后是decrypt.php我在哪里解密,但问题是当我尝试解密它时,结果仍然是加密的,但加密不同。有人能帮我解密吗 这是我点击加密按钮的图片 这是加密的密码。 这是解密的问题,输出应该是“fwf2”,但它是不同的 下面是index.php的代码 <!DOCTYPE html> <html> <head> <title><

我有一个3php文件,一个是
index.php
原始字符串在哪里,
encrypt.php
我在哪里加密原始字符串,最后是
decrypt.php
我在哪里解密,但问题是当我尝试解密它时,结果仍然是加密的,但加密不同。有人能帮我解密吗

这是我点击加密按钮的图片

这是加密的密码。

这是解密的问题,输出应该是“fwf2”,但它是不同的

下面是
index.php的代码

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="encrypt.php">
Original String <input type="text" name="text">
<input type="submit" name="encrypt" value="Encrypt" href="encrypt.php">
</form>
</body>
</html>
<?php
$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['text'];

    $encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv);

}


?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="decrypt.php">
Encrypted String <input type="text" style="width:500px;" name="encrypted" value="<?php echo $encrypted_string; ?>">
<input type="submit" name="decrypt" value="Decrypt" href="decrypt.php">
</body>
</html>
<?php
$secret_key = "thisismykey12345";
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);

if(isset($_POST['decrypt'])){

     $encrypted_string = $_POST['encrypted'];
     $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" action="encrypt.php">
Decrypted String <input type="text" name="decrypted" style="width:500px;" value="<?php echo  $decrypted_string ?>">
</body>
</html>
下面是
decrypt.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="encrypt.php">
Original String <input type="text" name="text">
<input type="submit" name="encrypt" value="Encrypt" href="encrypt.php">
</form>
</body>
</html>
<?php
$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['text'];

    $encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv);

}


?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="decrypt.php">
Encrypted String <input type="text" style="width:500px;" name="encrypted" value="<?php echo $encrypted_string; ?>">
<input type="submit" name="decrypt" value="Decrypt" href="decrypt.php">
</body>
</html>
<?php
$secret_key = "thisismykey12345";
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);

if(isset($_POST['decrypt'])){

     $encrypted_string = $_POST['encrypted'];
     $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" action="encrypt.php">
Decrypted String <input type="text" name="decrypted" style="width:500px;" value="<?php echo  $decrypted_string ?>">
</body>
</html>


解密字符串您必须重新使用iv,以相同的方式初始化加密和解密:

// Encryption
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv_enc  = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$str_enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, 'thisismykey12345', 'hallops', MCRYPT_MODE_CBC, $iv_enc);
$encrypted = $iv_enc . $str_enc;

// Decryption
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);

$iv_dec = substr($encrypted, 0, $iv_size); // Extract iv
$str_dec = substr($encrypted, $iv_size); // Extract encrypted string

echo mcrypt_decrypt(
    MCRYPT_RIJNDAEL_256, 
    'thisismykey12345', 
    $str_dec,
    MCRYPT_MODE_CBC, 
    $iv_dec
);
--> hallops
注意iv和加密数据连接在一起并“发送”在一起的方式

正如其他人所说,如果要将此信息发送到某个地方,可能需要做一些事情,而且某些加密算法比其他算法更安全


编辑:在示例中更详细地解释这些内容。

您必须重新使用iv,以相同的方式初始化加密和解密内容:

// Encryption
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv_enc  = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$str_enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, 'thisismykey12345', 'hallops', MCRYPT_MODE_CBC, $iv_enc);
$encrypted = $iv_enc . $str_enc;

// Decryption
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);

$iv_dec = substr($encrypted, 0, $iv_size); // Extract iv
$str_dec = substr($encrypted, $iv_size); // Extract encrypted string

echo mcrypt_decrypt(
    MCRYPT_RIJNDAEL_256, 
    'thisismykey12345', 
    $str_dec,
    MCRYPT_MODE_CBC, 
    $iv_dec
);
--> hallops
注意iv和加密数据连接在一起并“发送”在一起的方式

正如其他人所说,如果要将此信息发送到某个地方,可能需要做一些事情,而且某些加密算法比其他算法更安全

编辑:在示例中更详细地解释这些内容。

我在这里找到了答案-->

这是密码 index.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="encrypt.php">
Original String <input type="text" name="text">
<input type="submit" name="encrypt" value="Encrypt" href="encrypt.php">
</form>
</body>
</html>

原始字符串
encrypt.php

<?php
$iv = mcrypt_create_iv(
    mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),
    MCRYPT_DEV_URANDOM
);
if(isset($_POST['encrypt'])){
$key = "thisismykey12345";
$string = $_POST['text'];

$encrypted = base64_encode(
    $iv .
    mcrypt_encrypt(
        MCRYPT_RIJNDAEL_128,
        hash('sha256', $key, true),
        $string,
        MCRYPT_MODE_CBC,
        $iv
    )
);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="decrypt.php">
Encrypted String <input type="text" style="width:500px;" name="encrypted" value="<?php echo $encrypted; ?>">
<input type="submit" name="decrypt" value="Decrypt" href="decrypt.php">
</body>
</html>

加密字符串我在这里找到了答案-->

这是密码 index.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="encrypt.php">
Original String <input type="text" name="text">
<input type="submit" name="encrypt" value="Encrypt" href="encrypt.php">
</form>
</body>
</html>

原始字符串
encrypt.php

<?php
$iv = mcrypt_create_iv(
    mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),
    MCRYPT_DEV_URANDOM
);
if(isset($_POST['encrypt'])){
$key = "thisismykey12345";
$string = $_POST['text'];

$encrypted = base64_encode(
    $iv .
    mcrypt_encrypt(
        MCRYPT_RIJNDAEL_128,
        hash('sha256', $key, true),
        $string,
        MCRYPT_MODE_CBC,
        $iv
    )
);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="decrypt.php">
Encrypted String <input type="text" style="width:500px;" name="encrypted" value="<?php echo $encrypted; ?>">
<input type="submit" name="decrypt" value="Decrypt" href="decrypt.php">
</body>
</html>


加密字符串您似乎将加密数据视为文本,而不是二进制数据。要将is视为文本(即在HTTP请求中传递,您需要对其进行编码/解码,例如Base64),仅供参考,您应避免ECB&MCRYPT_RIJNDAEL_256不是AES256@Alex那么那是什么加密算法,先生?@nethken,它是Rijndael,块大小为256位。AES算法是Rijndael,块大小为128位,密钥为128、192或256位。PHP/mcrypt中的键大小是通过查看键大小本身来设置的;如果使用MCRYPT_RIJNDAEL_128算法且密钥为256位/32字节,则该算法为AES-256。PS mcrypt是一个过时的陷阱,ECB也是一个可怕的陷阱。你似乎把加密的数据当作文本来处理,而不是二进制数据。要将is视为文本(即在HTTP请求中传递,您需要对其进行编码/解码,例如Base64),仅供参考,您应避免ECB&MCRYPT_RIJNDAEL_256不是AES256@Alex那么那是什么加密算法,先生?@nethken,它是Rijndael,块大小为256位。AES算法是Rijndael,块大小为128位,密钥为128、192或256位。PHP/mcrypt中的键大小是通过查看键大小本身来设置的;如果使用MCRYPT_RIJNDAEL_128算法且密钥为256位/32字节,则该算法为AES-256。PS mcrypt是一个过时的陷阱,欧洲央行也很可怕。谢谢你的努力,先生。谢谢你的努力,先生。