1

In my js code i am trying to generate public key from modulus and exponent using JSEncrypt library.

But the generated public key is not matched with java code.

Below is my code.

this.exponent='010001';
    this.modulus='96518069736851770465507126477764180178790654237995866867543110338910852589430926664353243433531145405111315650993549181579863577045594288695458795648936206204355932074042171926595601924679006398922132106252498346239856831108091821764242087251474691687777518072069350367687597158222422562390956429433989056209';
    var jsencrypt = new JSEncrypt(this.modulus, this.exponent);
    console.log('Base64 Public Key'+jsencrypt.getPublicKeyB64());

Expected Output:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJckBNJas36vkMxult53z3E5pX
1NTCEUe+eG5yagXyFskX8xQscJO5wGxzdUWmVmiXDGBKmdOTzRGUTDDUOE7crUyJ
BUUEp0V3xxs67SGtbEbmVCUe8nIyYoBF9twb2Nn9Zm767aJdysH0+uWNLvfGKtDUT
7GAgwxBjpxyjT1G0QIDAQAB

But Actual Output:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVwqhJzCHEePwxOjAOdaOFt7ACV
 PVShRzo6JFgqsQ/6Y20AW3ToNWzzEZcF4tnort5ltbHt6mvV6mifRixennZPcNkn4K
 mrjsf1fGE7V2x9Tg34ZqyLvCkfznInjXsiyBMjgitaUDaFX9jGBZNbsBGhhpqOGQdxs
 Sd3tTM4H2ywQIDAQAB

In my javascript code for the same modulus and exponent public key value changes every time.But in java public key remains same for same modulus and exponent.

Why the java and javascript public key mismatch.Is any other way to generate public key from modulus and exponent?

Any help will be greatly appreciated!!!

Selva
  • 1,620
  • 3
  • 33
  • 63
  • Do you have any documentation for the JSEncrypt constructor? The only thing I can find indicates that the first argument, if present, is the keysize, **not** the modulus. It then generates a new random RSA keypair of the given size and exponent. If the first argument cannot be parsed as an int then it defaults to 1024. That's would explain your results. – President James K. Polk Feb 19 '18 at 21:56
  • @JamesKPolk, thanks for the response, yes you are correct,then we cannot generate public key using modulus and exponent in jsencrypt? – Selva Feb 20 '18 at 05:52

0 Answers0