在将数据插入mySQL数据库之前,我正在尝试使用phpseclib加密PII。 我似乎可以对某些内容进行加密,并将其放入数据库,但是当我回想起它时,我无法对其进行解密。
任何想法我做错了什么?
<?php
$con=mysqli_connect("localhost","XXXXXXX","XXXXXXX","XXXXXXX");
include('phpseclib/Crypt/RSA.php');
$secret= 'Hello World';
$pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCunhp5XEEptnE/+jhfdTF1ryjr 756tUJs26RTKgVa/6isdO+SzTdHRvsOqle8Ze58Y9Qj8goVGkcsxrSHr+nEWcN6O oQK+e1Inux5PDwVOE9kiWbjDN4hs41+d6ZgdN6l8h4bhPeAVgQoS2F4a2TKMyLY5 dyzwj7RK98mLwbfaNwIDAQAB";
$rsa = new Crypt_RSA();
$rsa->loadkey($pubkey);
$name = "apersonalname";
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt($secret);
$storeme = base64_encode($ciphertext);
echo $storeme;
mysqli_query($con, "INSERT INTO `enctest`(`Name`, `SecretMessage`) VALUES ('$name','$storeme')");
$findsecret = mysqli_query($con, "SELECT * FROM `enctest` WHERE `Name`='$name'");
while($row = mysqli_fetch_array($findsecret))
{
$sm = $row['SecretMessage'];
};
$return = base64_decode($sm);
$rsa = new Crypt_RSA();
$rsa->loadKey('MIICXQIBAAKBgQCunhp5XEEptnE/+jhfdTF1ryjr756tUJs26RTKgVa/6isdO+Sz TdHRvsOqle8Ze58Y9Qj8goVGkcsxrSHr+nEWcN6OoQK+e1Inux5PDwVOE9kiWbjD N4hs41+d6ZgdN6l8h4bhPeAVgQoS2F4a2TKMyLY5dyzwj7RK98mLwbfaNwIDAQAB AoGBAJBl1/ix7IUFBOPmFIYD0LHD9nAdCDfEZ4Zh+rp3eHhdueeD5tzLpo3vVbkU IdQN4YPu5c6CVJoF6GUdCecthCOvQtHtIZq3Gc3Bg6e7lnc3TXek1rBvIooexwj7 wqi+kHOxDZIo271CHccZCOmimBIPfA2x+LpdEa41ftJpxdM5AkEA4JbhbCaCGTQe ra9vGs/GIa61Rhemp8Ei/CrQwE8IgeMTFEnWj7qz23851uwPcdXUV4KvN/JtsFnr m2wyd501KwJBAMcKDO/XDgHuZc9X06lp0rkTqH6ToP7iZFhGvQV7TPt0AsJ2ew8z BKXAAwAE1aVWmt1V2OfX2JgXYUJGjX9eASUCQQDIMzc25QzL0r4WLXBpAhWoKMjG dobEu1GoX7I9+zWmMvjq+PceoFt7qmmwwDT0UJw8de4jfSNyNHXl0gkavzSJAkAO bQ4O/BlJzouWgZkXuFHLlD8sHWsXzjTTqRzeHtevifMBQTEeIuZ9VPNitvb9Ks/C pww3kY9jRp6H/TEYQCn5AkBp/tVM5fh7zp3D0K22oj/0N4aDMPMJ+PiaIMVz8ATj sqX4tZ0dDJ1tInz7uiDM1WH/8JV48kI6EQ8V2ekct51T');
echo $rsa->decrypt($return);
mysqli_close($con);
?>
答案 0 :(得分:2)
您正在创建两个Crypt_RSA对象,但只在第一个上执行$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
。也可以在第二个上做,你应该好好去!