Skip to content
Advertisement

How can I use publicExponent as 65537 in RSA-OAEP algorithm in JavaScript?

Actually, I am using RSA-OAEP with SHA-256ANDMGF1PADDING for encryption and decryption in JavaScript using Web crypto API. The actual scenario I need to do is in java I am able to encrypt and decrypt using a public and private key with the same algorithm specifications. but there it uses a 65537 public exponent .now what I need to do is encrypt some message in JavaScript using public key and decrypt it in java. but I found that in JavaScript for the above-mentioned algorithm specification it uses Public exponent as 5. so I want to change it to 65537. I tried to use the following code in JavaScript.

  window.crypto.subtle.generateKey({
     name: "RSA-OAEP",
     modulusLength: 2048,
     publicExponent: new Uint8Array([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x01]),
     hash: {name: "SHA-256"},
    }, 
true, 
 ["encrypt", "decrypt"]);

but still, the public exponent of the generated public key is 5. what wrong I am doing? The final requirement of me is, Encrypt some message in JavaScript using RSA-OAEP with SHA-256ANDMGF1PADDING and which will be able to decrypt in java using the private key and vice versa.

Advertisement

Answer

Use this. It is equivalent to 65537:

 publicExponent: new Uint8Array([0x01, 0x00, 0x01])

The publicExponent is a BigInteger Each element in the uint8 array is a non negative integer in the range 0..256

  1. BigInteger
typedef Uint8Array BigInteger;

The BigInteger typedef is a Uint8Array that holds an arbitrary magnitude unsigned integer in big-endian order.

[0x01, 0x00, 0x01] = 00000001 00000000 00000001 = 65537
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement