Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sodium.crypto_secretbox () or decrypted.toString(); does not handle UTF8 characters #50

Open
agnasg opened this issue May 11, 2022 · 2 comments

Comments

@agnasg
Copy link

agnasg commented May 11, 2022

Not sure if this is an issue or maybe I need to put charset="UTF-8" somewhere but if the original plaintext has UTF8 characters the resulting decrypted text returns garbage.
For example:
plaintext = 'El camión tenía piñas';

ciphertext.toString('hex') == bbbbe889db929838ddd6b62b0712479655c4abb10890bdd835d3298436234746d19da673c8

decrypted.toString() == El cami�n ten�a pi�as

I was about to swap the bytes but it seemed crazy, so I preferred to ask.

@zodman
Copy link

zodman commented Feb 27, 2023

try decrypted.toString('latin1') works for me but only in Spanish;

Or

new_plaintext = Buffer.from(plaintext).toString('base64')
...
Buffer.from(decrypted.toString(),'base64').toString()

@zodman
Copy link

zodman commented Feb 27, 2023

const { SodiumPlus } = require('sodium-plus');

(async function() {
    // Select a backend automatically
    let sodium = await SodiumPlus.auto();

    let key = await sodium.crypto_secretbox_keygen();
    let nonce = await sodium.randombytes_buf(24);
    let message = 'my unicode ÑÖ 中国 😉';

   let messageEncoded = Buffer.from(message,"utf8");
    let ciphertext = await sodium.crypto_secretbox(messageEncoded, nonce, key);
    let decrypted = await sodium.crypto_secretbox_open(ciphertext, nonce, key);
    console.log(decrypted.toString());
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants