【发布时间】:2016-07-19 13:05:36
【问题描述】:
我有一个 3 个 php 文件,其中一个是 index.php 原始字符串在那里,encrypt.php 我将在哪里加密原始字符串,最后是 decrypt.php 我将在哪里解密它但问题是当我尝试解密它的结果仍然是加密的,但不一样的加密它是不同的。有人可以帮我解密吗?
这是我点击加密的图片
这是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