I can't decrypt unencrypted
alantoledo007 opened this issue · 8 comments
I created a helper to get the public and private key. The pems files have been created with new NodeRSA ({b:1024}):
const fs = require("fs");
const NodeRSA = require("node-rsa");
const getKeys = () => {
const publicPem = fs.readFileSync("./public.pem", "utf8");
const privatePem = fs.readFileSync("./private.pem", "utf8");
const privateKey = new NodeRSA(privatePem);
const publicKey = new NodeRSA(publicPem);
return {
privateKey,
publicKey,
privatePem,
publicPem,
};
};
const RSA = {
getKeys,
};
module.exports = { RSA };i can encrypt with publicKey.encrypt(data, "base64"). And, i can decrypt with privateKey.decrypt(encryptedData,'utf8').
but...
I can't decrypt if I didn't create any encryption before. Just to give you an idea, I have to do this for the decryption to work:
publicKey.encrypt(""); //bug
const decryptedData= privateKey.decrypt(encryptedRSA, "utf8");without publicKey.encrypt(""); //bug the terminal returns next error:
throw Error('Error during decryption (probably incorrect key). Original error: ' + e);
^
Error: Error during decryption (probably incorrect key). Original error: Error: error:25078067:DSO support routines:win32_load:could not load the shared library
at NodeRSA.module.exports.NodeRSA.$$decryptKey (C:\....\project-name\node_modules\node-rsa\src\NodeRSA.js:301:19)
at NodeRSA.module.exports.NodeRSA.decrypt (C:\...\project-name\node_modules\node-rsa\src\NodeRSA.js:249:21)
at Object.decrypt (C:\....\MyHelper.js:20:38)
at Object.<anonymous> (C:\...\index.js:7:37)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
npm v8.6.0
node v16.14.2
node-rsa v1.1.1
Summary
// Works
const encryptedData = publicKey.encrypt(data, "base64");
const decryptedData = privateKey.decrypt(encryptedData , "utf8");// Not works
// const encryptedData = publicKey.encrypt(data, "base64");
const decryptedData = privateKey.decrypt("your base64 encryption", "utf8");// Works
publicKey.encrypt("");
const decryptedData = privateKey.decrypt("your base64 encryption", "utf8");reproduce error:
- Encrypt anything.
- Copy the encrypted result.
- remove or comment out the lines of code that do the encryption
- try decrypt. (not works)
@alantoledo007 hi! Can you provide minimal snippet with bug?
publicKey.encrypt("");
everytime i want decrypt, i need create any encrypt for can decrypt anything. If I remove that line, I get this error::
throw Error('Error during decryption (probably incorrect key). Original error: ' + e);
^
Error: Error during decryption (probably incorrect key). Original error: Error: error:25078067:DSO support routines:win32_load:could not load the shared library
at NodeRSA.module.exports.NodeRSA.$$decryptKey (C:\....\project-name\node_modules\node-rsa\src\NodeRSA.js:301:19)
at NodeRSA.module.exports.NodeRSA.decrypt (C:\...\project-name\node_modules\node-rsa\src\NodeRSA.js:249:21)
at Object.decrypt (C:\....\MyHelper.js:20:38)
at Object.<anonymous> (C:\...\index.js:7:37)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
```
I mean the working piece of the code with the bug
My test works fine
const NodeRSA = require("../src/NodeRSA")
const key = new NodeRSA("-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIBPQIBAAJBAPjFVq2K1qm59eS70nhpnh+Yx4/jaoulc2nzWw0AG6wwr29igI7q\n" +
"moBFFwyGJtTGY5DczVC1yA/Y0O9nynwn+3MCAwEAAQJBAN9K+i9RRLcZ5N3HvPY6\n" +
"DyVbVB0SSstH2d2LAvobU7tsPrKnudtBPb9jj2m9LEeM60JZNS3OthkTm5b0PgoJ\n" +
"IiECIQD9HyclvA9XdQ+iieY/WZN+IN1AUWFR+1sf+bfnMNV3wwIhAPuZhSx2ihYH\n" +
"iNM9rNDyx3q15pBz5VPRivLU8+8fF+KRAiEAj/xIqI5xq65LVopbD25FGFpZgVzJ\n" +
"n3j8PRQwKLL+u0ECIQDFOyjBnT9MW6Wv6uZBekBT+qp+zMuWdGpXSAbdieNgcQIh\n" +
"ANHW+0rRgieQtrs/Gz5bbtMKG7T8zirWkd3Av+FEVwQM\n" +
"-----END RSA PRIVATE KEY-----");
//console.log(key.encrypt("abc", "base64"))
console.log(key.decrypt("gUUaWQFC4NP22bUW+5qJUOTJQpk9DYWY7PvvLzCbEIYtg1GYSarVPg42DfpGpgsMAUubxZBA7HdX8oSJ/jhSAw==", "utf8"))Also, original error: error:25078067:DSO support routines:win32_load:could not load the shared library looks like some node error
const { RSA, Wallet } = require("../core");
const { publicKey, privateKey } = RSA.getKeys();
const create = () => {
const wallet = Wallet.create();
const address = wallet.address;
const encryptedWallet = Wallet.encrypt(wallet.privateKey);
const encryptedRSAwallet = publicKey.encrypt(
JSON.stringify(encryptedWallet),
"base64"
);
return { address, hash: encryptedRSAwallet };
};
const decrypt = (encryptedRSA) => {
publicKey.encrypt(""); //bug
const encrypredWalled = privateKey.decrypt(encryptedRSA, "utf8");
const wallet = Wallet.decrypt(JSON.parse(encrypredWalled));
return wallet;
};
const WalletUtils = { create, decrypt };
module.exports = { WalletUtils };RSA.js
const NodeRSA = require("node-rsa");
const getKeys = () => {
const publicPem = fs.readFileSync("./public.pem", "utf8");
const privatePem = fs.readFileSync("./private.pem", "utf8");
const privateKey = new NodeRSA("your private key");
const publicKey = new NodeRSA("you public key");
return {
privateKey,
publicKey,
};
};
const RSA = {
getKeys,
};
module.exports = { RSA };core.js
module.exports = {
...require("./RSA"),
};I can't run & debug this. Can you provide snippet without dependencies?
Step1
const NodeRSA = require("node-rsa");
const privateKey = new NodeRSA("your private key");
const publicKey = new NodeRSA("you public key");
const encrypt = ( data ) => {
const encryptedData= publicKey.encrypt(
JSON.stringify(data),
"base64"
);
return encryptedData
};
const decrypt = (encryptedData) => {
const decryptedData= privateKey.decrypt(encryptedData, "utf8");
return decryptedData;
};
const encryptedData = encrypt({foo:"var"})
console.log(encryptedData) // returns a base64 encryptionStep2
const NodeRSA = require("node-rsa");
const privateKey = new NodeRSA("your private key");
const publicKey = new NodeRSA("you public key");
const encrypt = ( data ) => {
const encryptedData= publicKey.encrypt(
JSON.stringify(data),
"base64"
);
return encryptedData
};
const decrypt = (encryptedData) => {
const decryptedData= privateKey.decrypt(encryptedData, "utf8");
return decryptedData;
};
// const encryptedData = encrypt({foo:"var"})
// console.log(encryptedData) // returns a base64 encryption
console.log(decrypt("base64 encryption")) //error: throw Error('Error during decryption (probably incorrect key). Original error: ' + e);My solution:
const NodeRSA = require("node-rsa");
const privateKey = new NodeRSA("your private key");
const publicKey = new NodeRSA("you public key");
const encrypt = ( data ) => {
const encryptedData= publicKey.encrypt(
JSON.stringify(data),
"base64"
);
return encryptedData
};
const decrypt = (encryptedData) => {
publicKey.encrypt(""); //############## SOLUTION HERE ############
const decryptedData= privateKey.decrypt(encryptedData, "utf8");
return decryptedData;
};
// const encryptedData = encrypt({foo:"var"})
// console.log(encryptedData) // returns a base64 encryption
console.log(decrypt("base64 encryption")) //worksFor me it just works fine. As I said, probably node bug, or may be your system. Try to install different node version or test it on other system.
i fix it。because node-rsa use pkcs1_oaep. so need to add code:
key.setOptions({ encryptionScheme: 'pkcs1' });