简体   繁体   中英

RSA Decryption on PHP

I'm trying to use RSA on PHP, and so far I'm not sure what I'm doing wrong. So basically, I have an RSA-encrypted string that I got from a site, which I then check if it is the same with the public_key .

require_once 'vendor/autoload.php';
$rsa = new \phpseclib\Crypt\RSA();

$secure_signature = urldecode(*<base64 encoded signature>*);
$decodedSig = base64_decode($secure_signature);

$public_key = *<base64 encoded key>*;

$rsa->loadKey($public_key);
$res = $rsa->decrypt($decodedSig);

echo $res;

I came across an error where it says Decryption error . I then looked at other RSA public keys and noticed that it has the ----BEGIN PUBLIC KEY---- and ----END PUBLIC KEY---- texts on the front and at the end of the key, respectively.

So I changed the value of my $public_key .

$public_key = "---BEGIN PUBLIC KEY----\n*<base64 encoded key>*----END PUBLIC KEY----";

After that, it doesn't show any errors but it also doesn't show anything. How can I know if I did this right? Am I missing something?

I have gone through this. If you happen to var_dump the decrypt method response then you will get bool(false) or maybe NULL. Notifying that encryption process ie the way it has encrypted is not matching up with decryption process. Thus this issue. Check your encryption & decryption steps / process. Look at the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM