-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I can't decrypt unencrypted #215
Comments
@alantoledo007 hi! Can you provide minimal snippet with bug? |
everytime i want decrypt, i need create any encrypt for can decrypt anything. If I remove that line, I get this error::
|
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: |
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 };
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 encryption Step2 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")) //works |
For 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:
|
I created a helper to get the public and private key. The pems files have been created with
new NodeRSA ({b:1024})
:i can encrypt with
publicKey.encrypt(data, "base64")
. And, i can decrypt withprivateKey.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:
without
publicKey.encrypt(""); //bug
the terminal returns next error:npm v8.6.0
node v16.14.2
node-rsa v1.1.1
Summary
reproduce error:
The text was updated successfully, but these errors were encountered: