norah
norah

Reputation: 69

How to convert the object of Uint8Array to string?

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.

Upvotes: 0

Views: 739

Answers (2)

alchemist95
alchemist95

Reputation: 794

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

Upvotes: 1

ardean
ardean

Reputation: 162

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

Upvotes: 0

Related Questions