【问题标题】:AES - The encrypted string is not decrypting.AES - 加密字符串未解密。
【发布时间】:2016-07-19 13:05:36
【问题描述】:

我有一个 3 个 php 文件,其中一个是 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>

这里是encrypt.php

<?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>

这里是decrypt.php

<?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>

【问题讨论】:

  • 您似乎将加密数据视为文本,而不是二进制数据。将其视为文本(即在 HTTP 请求中传递它,您需要对其进行编码/解码,例如 Base64)
  • 仅供参考,您应该避免使用 ECB 和 MCRYPT_RIJNDAEL_256 不是 AES 256
  • @Alex K. 那先生是什么类型的加密算法?
  • @nethken 这是 Rijndael,块大小为 256 位。 AES 算法是 Rijndael,块大小为 128 位,密钥为 128、192 或 256 位。 PHP/mcrypt 中的密钥大小是通过查看密钥大小本身来设置的;如果您使用算法 MCRYPT_RIJNDAEL_128 并且密钥为 256 位 / 32 字节,则该算法为 AES-256。 PS mcrypt 是过时的陷阱,ECB 也很可怕。

标签: php encryption cryptography aes


【解决方案1】:

您必须重复使用 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 和加密数据连接并一起“发送”的方式。

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

编辑:http://php.net/mcrypt_encrypt 在示例中更详细地解释了这些内容。

【讨论】:

  • 谢谢(写了例子:P)
  • 感谢您的努力,先生。
【解决方案2】:

我在这里找到了答案--> Best way to use PHP to encrypt and decrypt passwords?

这里是代码 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

<?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>

解密.php

<?php
$key = "thisismykey12345";

if(isset($_POST['decrypt'])){
$encrypted = $_POST['encrypted'];

$data = base64_decode($encrypted);
$iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));

$decrypted = rtrim(
    mcrypt_decrypt(
        MCRYPT_RIJNDAEL_128,
        hash('sha256', $key, true),
        substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)),
        MCRYPT_MODE_CBC,
        $iv
    ),
    "\0"
);
}

?>
<!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; ?>">
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2011-12-22
    相关资源
    最近更新 更多