amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.05k stars 1.16k forks source link

[Bug] SEA.decrypt strip Quotation Marks - created with JSON.stringify #1188

Closed shemeshg closed 2 years ago

shemeshg commented 2 years ago

running this

        const test=JSON.stringify("s")
        console.log(test)
        const a=await SEA.encrypt(test,"password")
        console.log(a)
        const b=await SEA.decrypt(a,"password")
        console.log(b)

results this

"s"
SEA{"ct":"WMjln0w3ofJSOuxM/frgQB3ZQA==","iv":"A7WZ0UVMx7K4+oEp8x6u","s":"iDeeDCEaM3IA"}
s

However Expected/correct result is this

"s"
SEA{"ct":"WMjln0w3ofJSOuxM/frgQB3ZQA==","iv":"A7WZ0UVMx7K4+oEp8x6u","s":"iDeeDCEaM3IA"}
"s"
draeder commented 2 years ago

You should be encrypting with a pair.

var data = 'some data to encrypt'

var pair = await SEA.pair();
var enc = await SEA.encrypt(data, pair);

Then, you may decrypt with the same pair.

var dec = await SEA.decrypt(data, pair);
console.log(dec) // 'some data to encrypt'

See: https://gun.eco/docs/SEA#quickstart

shemeshg commented 2 years ago

Hi

Thanks for the response, With pair produces the same undesired result (of stripped Quotation Marks)

data is: "some data to encrypt"
decrypted is: some data to encrypt

for

    const data = JSON.stringify('some data to encrypt')

    const pair = await SEA.pair();
    const enc = await SEA.encrypt(data, pair as IGunCryptoKeyPair);
    const dec = await SEA.decrypt(enc, pair as IGunCryptoKeyPair);
    console.log(`data is: ${data}`)
    console.log(`decrypted is: ${dec}`)

The data string looses it's Quotation Marks

If you'll try to JSON.parse(dec) it will fail. while JSON.parse(data) will succeed

draeder commented 2 years ago

What I don't understand is why you're trying to JSON.stringify() what is already a string?

shemeshg commented 2 years ago

Ok, just tried with

const data = {test:'some data to encrypt'}

and it worked fine.

Did not know, the SEA.encrypt accept Object and does the Stringify for me already I thought that encrypt might reject non String object as parameter.

Thanks