0

I have same problem as this question. I see the answer but I couldn't understand how I can do it. Is there any other suggestions?

My code is:

var eccrypto = require("eccrypto");
var w,actual;
var publicKey = Buffer.from([4, 86, 82, 58, 244, 11, 140, 41, 132, 245, 184, 162, 163, 98, 49, 119, 168, 235, 252, 50, 6, 91, 147, 191, 190, 61, 65, 63, 101, 164, 132, 213, 188, 106, 26, 203, 171, 215, 240, 151, 7, 193, 10, 151, 103, 107, 1, 135, 117, 225, 5, 41, 55, 57, 18, 205, 98, 178, 82, 135, 170, 111, 188, 98, 57],'hex');

var privateKey= Buffer.from([238, 239, 199, 101, 188, 134, 13, 13, 195, 172, 125, 168, 225, 189, 72, 148, 225, 200, 127, 218, 204, 11, 150, 146, 180, 243, 195, 109, 200, 119, 50, 20],'hex');

eccrypto.encrypt(publicKey, Buffer.from("message")).then(function(encrypted) {
    console.log(encrypted)

    let encoded =JSON.stringify(encrypted)
    w=encoded;
    console.log(encoded)

    actual = JSON.parse((encoded))
    console.log(actual)
});

eccrypto.decrypt(privateKey,actual).then(function(plaintext) {
    console.log("Message to part B:", plaintext.toString()); 
});

When I use actual variable I have this error:

Uncaught (in promise) Error: Bad public key

this the output of encrypted :

enter image description here

this the output of encoded :

enter image description here

and this the output of actual "there is some things changes i think, is not it ?": enter image description here thank you advance.

norah
  • 69
  • 1
  • 10
  • decrypt is expecting a Buffer where it is being given a JSON. Maybe that is why promise is failing. Try sending encrypted eccrypto.decrypt(privateKey, encrypted) – alchemist95 Jun 10 '20 at 17:45
  • After fixing the error from `encrypt`, see [this](https://stackoverflow.com/q/23667086/1048572) for the next problem – Bergi Jun 10 '20 at 17:47
  • @alchemist95 actually this is work but i need to save encrypted at this format : `let encoded =JSON.stringify(encrypted)` to use it somewhere then i want to convert it again to the original format using `var actual = JSON.parse((encoded))` Is my way wrong ? – norah Jun 10 '20 at 18:18
  • @Bergi thank you very much, but i use async and await to handle this problem i think it works. – norah Jun 10 '20 at 18:25
  • @norah Cool, it should work when you switched from `.then()` to `await`, but the code you posted in the question didn't use `async`/`await`. – Bergi Jun 10 '20 at 18:29
  • @Bergi yeah because it a long code and i am just show the related code of my question, thanks a lot, i appreciate your help. – norah Jun 10 '20 at 18:57
  • adding an image in answer. See if that helps @norah – alchemist95 Jun 10 '20 at 19:22
  • @alchemist95 i update the question, Can you have a look ? thank you. – norah Jun 10 '20 at 20:10

2 Answers2

1

While parsing, you need to send a reviver function, which conditionally parses the object. If you see how encoded looks, you will understand why the reviver function needs to be written this way.

enter image description here

alchemist95
  • 759
  • 2
  • 9
  • 26
  • it does not work, actual variable it is an object, have a look to the question to understand what i mean, i updated it. thank you – norah Jun 10 '20 at 20:15
  • JSON.parse(JSON.stringify)) is modifying the Buffer type which is why decrypt will keep failing. In order for decrypt() to work, you need to send a Buffer type object, not a regular JSON object. – alchemist95 Jun 10 '20 at 20:34
  • you mean in my case i have to make it like this: `JSON.parse(encrypted)` ? if it yes, it give me this error : SyntaxError: Unexpected token o in JSON at position 1 .. if not, can you explain it to me further. thanks a lot. – norah Jun 10 '20 at 21:15
  • If I understand correctly, you want to convert encrypted into a string. Correct? Secondly, you want to be able to retrieve the encypted buffer from that string. Right? – alchemist95 Jun 10 '20 at 22:12
  • JSON.parse expects a string, that's why it is failing. Ideally, `encoded = encrypted.toString('hex'); const actual = Buffer.from(encoded) ;` should solve your problem – alchemist95 Jun 10 '20 at 22:13
  • yeah, at the end i want: actual = encrypted, when i do what you suggest and try `console.log(encoded)` the result is : [object Object] it does not give me a real string! – norah Jun 10 '20 at 23:57
  • Please check answer – alchemist95 Jun 11 '20 at 06:44
0

I think you need to remove the "hex" encoding parameter from your Buffer.from.

ardean
  • 152
  • 7