RSA在javascript中与PHP不同

Sum*_*yen 0 javascript php rsa phpseclib jsbn

我现在遇到RSA问题.我在Javascript和PHP中有相同的模数和指数.在javascript中都使用PKCS#1,我使用http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.js

var rsa = new RSAKey();
rsa.setPublic("modulus","ex");
var result = rsa.encrypt(text);
Run Code Online (Sandbox Code Playgroud)

在PHP中,我使用http://phpseclib.sourceforge.net/

require_once 'Crypt/RSA.php';
$rsa = new Crypt_RSA();
$n="modulus";
$e="ex";
$rsa->modulus= new Math_BigInteger($n,16);
$rsa->publicExponent= new Math_BigInteger($e,16);
$key=$rsa->getPublicKey();
$rsa->loadKey($key);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt("1234");
echo bin2hex($ciphertext);
Run Code Online (Sandbox Code Playgroud)

但是2结果是不同的.可以请告诉我原因.非常感谢你

小智 5

它们是不同的,因为PKCS#1采用随机填充.

请查看以下网址:

http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.html

多次加密.得到的密文每次都不同.

重要的是你能够解密它 - 而不是密文是不同的.