I've spent an embarrasing number of hours trying to get Libsodium.js to work.
See my fiddle demo (and code pasted below too).
I keep getting Error: wrong secret key for the given ciphertext
.
What I would prefer is to replicate this PHP example of function simpleEncrypt($message, $key)
into Libsodium.js.
But as a starter, I'd be happy even getting the basic sample from the Libsodium.js repo to work.
Any hints?
Here is the code (also shown in the working fiddle):
const _sodium = require("libsodium-wrappers");
const concatTypedArray = require("concat-typed-array");
(async () => {
await _sodium.ready;
const sodium = _sodium;
const utf8 = "utf-8";
const td = new TextDecoder(utf8);
const te = new TextEncoder(utf8);
const nonceBytes = sodium.crypto_secretbox_NONCEBYTES;
const macBytes = sodium.crypto_secretbox_MACBYTES;
let key = sodium.from_hex("724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed");
function encrypt_and_prepend_nonce(message, key) {
let nonce = sodium.randombytes_buf(nonceBytes);
var encrypted = sodium.crypto_secretbox_easy(message, nonce, key);
var combined2 = concatTypedArray(Uint8Array, nonce, encrypted);
return combined2;
}
function decrypt_after_extracting_nonce(nonce_and_ciphertext, key) {
if (nonce_and_ciphertext.length < nonceBytes + macBytes) {
throw "Short message";
}
let nonce = nonce_and_ciphertext.slice(0, nonceBytes);
let ciphertext = nonce_and_ciphertext.slice(nonceBytes);
return sodium.crypto_secretbox_open_easy(ciphertext, nonce, key);
}
function encrypt(message, key) {
var x = encrypt_and_prepend_nonce(message, key);
return td.decode(x);
}
function decrypt(nonce_and_ciphertext_str, key) {
var nonce_and_ciphertext = te.encode(nonce_and_ciphertext_str);
return decrypt_after_extracting_nonce(nonce_and_ciphertext, key);
}
var inputStr = "shhh this is a secret";
var garbledStr = encrypt(inputStr, key);
try {
var decryptedStr = decrypt(garbledStr, key);
console.log("Recovered input string:", decryptedStr);
console.log("Check whether the following text matches the original:", decryptedStr === inputStr);
} catch (e) {
console.error(e);
}
})();
This is what I do in https://emberclear.io :
tests: https://gitlab.com/NullVoxPopuli/emberclear/blob/master/packages/frontend/src/utils/nacl/unit-test.ts#L19
implementation: https://gitlab.com/NullVoxPopuli/emberclear/blob/master/packages/frontend/src/utils/nacl/utils.ts#L48
Snippet of implementation (in typescript):
Snippet of tests:
Wow, I finally got it working!
The parts that really helped me were:
function u_atob(ascii)
for Uint8Array)const concatTypedArray = require("concat-typed-array");
require("babel-core/register");
andrequire("babel-polyfill");
, which I still don't understand (https://stackoverflow.com/a/33527883/470749)Here is the working fiddle sandbox.
And in case that ever disappears, here are the important parts:
I think you're making this harder than it needs to be. For your typescript encryption for example, all you need to do is this:
You don't need all the extra libraries you're using. And on your PHP side, to decrypt you'd just do this:
You don't need to set the nonce multiple times. That second parameter to the encryption is the "additional data" parameter, and it can just be an empty string if it's an empty string on the decryption side as well.