首页 > 解决方案 > 如何使用 openssl 解密此字符串

问题描述

我在解密使用 openssl 加密的字符串时遇到了一些问题。我无权更改加密代码,但我有读取权限:

加密代码(无法修改)

<?php 
$key = hex2bin("24a5d2b96b9aee2fb515c94fb36da508");
$encryptTxt = openssl_encrypt(
    "txt to encrypt",
    'AES-128-ECB',
    $key
);
?>

<a href="decrypt.php?un=<?php echo bin2hex(base64_decode($encryptTxt)) ?>">link</a>

这是我尝试解密的方式:

解密.php


$ciphertext = $_GET['un'];

$cipher = "aes-128-ecb";
$key = hex2bin("24a5d2b96b9aee2fb515c94fb36da508");

$original_plaintext = openssl_decrypt($ciphertext, $cipher, $key);
echo "text= " . $original_plaintext;

解密页面上不返回解密后的文本

标签: phpencryptionopenssl

解决方案


已解决:我将decrypt.php 更新为以下内容,并返回了解密后的文本

$ciphertext = $_GET['un'];
$ciphertext = hex2bin($ciphertext);
$ciphertext = base64_encode($ciphertext);

$cipher = "aes-128-ecb";
$key = hex2bin("24a5d2b96b9aee2fb515c94fb36da508");

$original_plaintext = openssl_decrypt($ciphertext, $cipher, $key);
echo "text= " . $original_plaintext;

推荐阅读