js-nacl icon indicating copy to clipboard operation
js-nacl copied to clipboard

newbie question

Open MilaSy opened this issue 6 years ago • 3 comments

Hi TonyG In my php I'm using sodium_bin2hex(sodium_crypto_box_publickey(sodium_crypto_box_keypair())) which I pass to the javascript: bpkeypair=''; nacl_factory.instantiate(function(nacl){ akeypair=nacl.crypto_box_keypair(); message=nacl.encode_utf8(msg); nonce=nacl.crypto_box_random_nonce(); }); nacl_factory.instantiate(function(acl){ ctext=acl.crypto_box(message,nonce,bpkeypair,akeypair.boxSk); n2h=acl.to_hex(nonce); });

when I pass "cipher text" and the "nonce to hex" back to php through ajax and try to decrypt the message using: $jsnonce=sodium_hex2bin($n2h); $keydc=$keysk.$pk; $text=sodium_crypto_box_open($ctext,$jsnonce,$keydc);

I get this error: Uncaught SodiumException: keypair size should be SODIUM_CRYPTO_BOX_KEYPAIRBYTES.

I'll attach the two files Im using to play around. Please help? Thanks very much

test.docx testrespoonse.docx

MilaSy avatar Oct 01 '18 12:10 MilaSy

Hi @EmileDew,

I had crypto_box and crypto_secretbox compatibility with PHP 5.6.

Here's an example I used for proof of concept with PHP 5.6 example.php.txt example.ajax.php.txt

If you are using PHP 7.1 or higher replace "\Sodium" with "sodium_" in PHP code.

RicardoSette avatar Nov 09 '18 16:11 RicardoSette

Thank you so much @RicardoSette :) Going to check it out now :D

MilaSy avatar Nov 10 '18 06:11 MilaSy

Hi @EmileDew. In this snippet...

$keydc=$keysk.$pk;
$text=sodium_crypto_box_open($ctext,$jsnonce,$keydc);

... it looks like you're passing the public key to crypto_box_open? You probably need to supply the recipient's secret key (and the sender's public key).

@RicardoSette, thanks for supplying your example code!

tonyg avatar Nov 19 '18 13:11 tonyg