Home > Enterprise >  phpseclib 2 RSA decryption works fine but phpseclib 3 RSA decryption not work properly
phpseclib 2 RSA decryption works fine but phpseclib 3 RSA decryption not work properly

Time:07-15

phpseclib2 sample - working

use phpseclib\Crypt\RSA;
$rsa = new RSA();
$rsa->loadKey($PRIVATE_KEY);
$key = $rsa->decrypt(base64_decode($a));

phpseclib3 sample - not working

use phpseclib3\Crypt\PublicKeyLoader;

$private = PublicKeyLoader::load($PRIVATE_KEY);
$key = $private->decrypt(base64_decode($a));

CodePudding user response:

Both V2 and V3 use OAEP by default for RSA, but V2 uses SHA1 as the default for the OAEP and MGF1 digest, while V3 uses SHA256 (s. here and here).
So in order for a decryption that works with V2 to also work with V3, the digests in V3 must be explicitly set to SHA1:

$decrypted = $private->withHash("sha1")->withMGFHash("sha1")->decrypt(base64_decode($a));

Note that SHA1 is considered insecure, but there are no known security issues in the context of OAEP, see here for more details.
However, there is no reason not to switch to SHA256 (as far as possible), if only to support the elimination of SHA1 from the ecosystem.

  • Related