Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

node.js - Decrypting RSA with JavaScript

I am writing a web app which receives an RSA encrypted response from a server. It needs to decrypt this file.

So far, I have tried to use JSEncrypt to decrypt the file on the frontend. The issue seems to be that the backend is not encrypting the file properly. If I put both the private and public key on the frontend, I can encrypt and decrypt successfully. The issue seems to be the way that I am encrypting the response. When I read in the file in nodeJS and encrypt the file with the following code:

fs.readFile("rsaPublicKey", "utf8", (err, data) => {
  if (err) throw err;

  pubKey = data;
});
encryptedMessage = crypto.publicEncrypt(
  {
    key: pubKey,
    padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
    oaepHash: "sha256",
  },
  Buffer.from(message)
);

res.send({"response": encryptedMessage.toString("base64")});

The message is bigger than the same message being encrypted with JSEncrypt.

Node.JS message: WLptALzMMws/Qj8qzeYkQ1NyRknoBGX0+oHmtzd0Cwl/RmWnwt6wSJ1qdbk5GMPcEML5iqCISqTfPTSEC6M37KIJAgGLViPENKcvonT7qQbMsn0yftFMl9grn1oLQz567t3lWpdyuCa99xqG+tGsAAOK84HHCW+nprSH6+7olysTnSzzZWvvBl6VGTpmwtoBEGOnZ5C/XLwiW7b2UuzHsksIA1s55OkJMOOUA6neZiJIzHsJSHZGgigKvKwYNQbjhmEBbdNVSvCPIE/d9dpTtWNABcnQX7SCA6/sTZH/f0OnGGXOyYabhq84fdw/WwpouUBWsRQLQYJgKy3EqY/y/w==

JSEncrypt message: Sq9KQyp7KDqy1CBFRLtXm4ZAdxidgUNlp0d6X6xm3m+aBXKv4H7DVu0O40EMWeSWl3dQcBBC/oguJsoAz/GY//77ElIPIRuvPK4YIWPNq2fjoIgIs3Ew4I5TKAP4rph//NSlDLPc4ppXQjj/YO2238EHney9Wxxa9EZzE/p48arkxuEjB0gakWyVgTlF8x6H7LGsD4epS7RWJ0ua1kG1J6ZuMB82qBvq2MugLEuQamAfml4LtwWYFTJ/dIcAqVqrtHe6/F2oNGwXsE2GDEeZcFr4vTkejCs5dFcbbcgg/KVnROdGQHJlDGl0uUBy/2UNml3cT3FocjXkPGJa0zu3/g==

Is there any way to try and get this to work?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try my recently modded version of jsencrypt.

I was having the opposite issue: encrypting with private key client side was not being decrypted by .net backend using bouncycastle.

I made some code cleanup and revisited paddings to be interoperable for booth cases.

Give it a try, and let me know: https://github.com/michaeldisaro/JSEncrypt


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...