AES - 加密字符串未解密。
AES - The encrypted string is not decrypting.
我有一个 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>
您必须重复使用 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 和加密数据的连接方式 "sent"。
正如其他人所说,如果要将其发送到某个地方,可能需要做一些事情,并且某些加密算法比其他算法更安全。
编辑:http://php.net/mcrypt_encrypt 在示例中更详细地解释了这些内容。
我在这里找到了答案 -->
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>
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>
decrypt.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
),
"[=12=]"
);
}
?>
<!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>
我有一个 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>
您必须重复使用 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 和加密数据的连接方式 "sent"。
正如其他人所说,如果要将其发送到某个地方,可能需要做一些事情,并且某些加密算法比其他算法更安全。
编辑:http://php.net/mcrypt_encrypt 在示例中更详细地解释了这些内容。
我在这里找到了答案 --> 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>
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>
decrypt.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
),
"[=12=]"
);
}
?>
<!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>