forge icon indicating copy to clipboard operation
forge copied to clipboard

Encryption in Node decryption in Java getting error like SYSTEM Error Error Performing RSA Encryption javax.crypto.BadPaddingException: Decryption error

Open sathish-er opened this issue 4 years ago • 3 comments

we are facing the problem in RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING encryption in node.js decryption in java.

below i can share my code in node:

var nodeForgeRsaEncryption =  function (data,publicKeyPem){
  var buf = forge.util.createBuffer(data, 'utf8');
  publicKey = forge.pki.publicKeyFromPem(publicKeyPem);
  let datas_temp = publicKey.encrypt(buf.bytes(),'RSA-OAEP',{
    md: forge.md.sha256.create(),
    mgf1: {
      md: forge.md.sha1.create()
    }
  });
  datas_temp = forge.util.encode64(datas_temp);
  return datas_temp;
};

For decryption purpose i can use online tool to check the decryption part below i can share the link which i use to test the java decryption link - https://8gwifi.org/RSAFunctionality?keysize=4096

public and private key available in link itself can you guide me how to fix this issue

sathish-er avatar May 09 '21 12:05 sathish-er

It happened to me too, did you solve it? Can you provide a reference?

bald-head avatar Oct 13 '23 06:10 bald-head

function RSAOAEPPadding(data) { const publicKeyPem = Your public key PEM format data const publicKey = forge.pki.publicKeyFromPem(publicKeyPem)

var buf = forge.util.createBuffer(data, 'utf8');

const encryptedBytes = publicKey.encrypt(buf.bytes(), 'RSA-OAEP', { md: forge.md.sha256.create(), mgf1: { md: forge.md.sha1.create() } })

return forge.util.encode64(encryptedBytes) }

Check the PEM format

watharindukumara avatar May 14 '24 07:05 watharindukumara