2

I'm using AES to encrypt data and RSA public key to encrypt the AES key used to encrypt data.

I have encrypted the AES key with RSA in Java using RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING and now I would want to decrypt that RSA encrypted key in NodeJS app. Is it possible to decrypt that AES key?

So basically I'm looking for RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING equivalent in NodeJS. (I did look at Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding equivalent in Node.js but there isn't anything useful)

I looked at NodeJS Decipher and crypto.privateDecrypt but it seems that there is no equivalent for OAEPWITHSHA-256ANDMGF1PADDING in NodeJS

Do I need to change OAEPWITHSHA-256ANDMGF1PADDING to something that else, that could be decrypted in NodeJS?

1
  • Have a look at Bouncy Castle vs Java default RSA with OAEP to understand what RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING does. Most likely the problem is that you are using SHA-256 instead of SHA-1 for the MGF1 digest, as in the accepted answer to the linked question (which is wrong, at least if the default provider, i.e. the SunJCE provider, is used on the Java side). Commented Mar 24 at 7:45

2 Answers 2

1

I ended up using RSA/ECB/OAEPPadding for Java side and crypto.constants.RSA_PKCS1_OAEP_PADDING for NodeJS. This implementation is as secure as my original one and doesn't require any third party libraries.

Sign up to request clarification or add additional context in comments.

Comments

0

use below code for oaepHash 256 in java

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding"); 
OAEPParameterSpec oaepParams =new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);

and equivalent code in js

const decryptedString = crypto.privateDecrypt(
      {
        key: privateKey,
        padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
        oaepHash: 'sha256',
      },
      Buffer.from(encryptedString, 'base64') // Assuming the encrypted string is Base64 encoded
    );

as in node js same hash is used for mgf1 mask generation -- get OAEP padding algo and then passOAEP params to cipher -- Aamir

3 Comments

The question is seeking a solution in JavaScript, not Java.
You code mixes text and code without proper formatting which makes it very hard to read. Please reformat it and place code in separate sections and use the available code formatting style.
updated the format, equivalent js is there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.