Question

I have a problem with RSA now. I have same modulus and exponent in Javascript and PHP., both use PKCS#1 in javascript, I use http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.js

var rsa = new RSAKey();
rsa.setPublic("modulus","ex");
var result = rsa.encrypt(text);

and in PHP, I use 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);

But 2 result are different. Can any please tell me the reason. Thank you very much

Was it helpful?

Solution

They're different because PKCS#1 employs randomized padding.

Check out the following URL:

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

Hit encrypt multiple times. The resultant ciphertext will be different each time.

What's important is that you're able to decrypt it - not that the ciphertext's are different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top