Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

246
Vistas
Cómo obtener una clave privada (en el formato correcto) con sodium-plus js (secretbox) de la página html para descifrar un mensaje

Puedo hacer eso: cifrar en la interfaz (sodium-plus.js) con clave pública desde el backend (PHP sodio)

Pero quiero hacer lo contrario (cifrar con php, descifrar con javascript) y tengo un problema.

Puedo obtener mi clave privada de mi página html (generada con php) como una cadena hexadecimal ( sodium_bin2hex(sodium_crypto_secretbox_keygen()) ), pero no puedo usarla con Sodium Plus.

Conozco este código para obtener la clave pública:

 let key = X25519PublicKey.from('...', "hex");

pero en mi caso eso no funciona y tengo un error al pasar esta variable en

 await sodium.crypto_secretbox_open(text, nonce, key);

Lo intenté solo con la cadena hexadecimal convertir a bin ( await sodium.sodium_hex2bin(key) ) pero tampoco funciona.

Aquí está mi código:

 define(function (require) { const { SodiumPlus } = require("sodium-plus"); }); let sodium; (async function () { if (!sodium) sodium = await SodiumPlus.auto(); let text = "...";//my text + nonce (at the end) in hex let nonce = text.substr(-48); text = text.substr(0, text.length - 48); let key = X25519PublicKey.from($("#key").text(), "hex");//get my private key in hex, on my html page text = await sodium.sodium_hex2bin(text); nonce = await sodium.sodium_hex2bin(nonce); let output = await sodium.crypto_secretbox_open(text, nonce, key); console.log(output.toString()); })();

gracias

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Aquí está mi solución con crypto_box y secret y public dos veces:

 define(function (require) { const { SodiumPlus } = require("sodium-plus"); }); let sodium; (async function () { if (!sodium) sodium = await SodiumPlus.auto(); let text = ""; //string in hex to be decrypted (see below for my php code server side) let nonce = text.substring(text.length -48); //nonce at the end of the string nonce = await sodium.sodium_hex2bin(nonce); //nonce hex to bin text = text.substring(0, text.length - 48); //text without nonce text = await sodium.sodium_hex2bin(text); //text hex to bin let publicKey = $("#key").text(); //to get my key in hex from my html page (key = secret key + public key), see below my php code $decryption_key let secretKey = publicKey.substring(0, 64);//to get the public key in hex secretKey = X25519SecretKey.from(secretKey, "hex"); //public key hex to array bin publicKey = publicKey.substring(publicKey.length -64); //same for public key publicKey = X25519PublicKey.from(publicKey, "hex"); let output = await sodium.crypto_box_open(text, nonce, secretKey, publicKey); output = output.toString(); // bin to string console.log(output); })();

Aquí está mi código php para cifrar, por ejemplo:

 $text = '...'; //text to encrypt $key = sodium_hex2bin( '...' ); //key in hex (got from an other code, see below $encryption_key) $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $text_encrypt = sodium_crypto_box($text, $nonce, $key); $text_encrypt = sodium_bin2hex($text_encrypt.$nonce); return $text_encrypt;

Mi código php para la clave de cifrado en php y la clave de descifrado en mi página html (luego obtenido por javascript)

 $keypair1 = sodium_crypto_box_keypair(); $keypair1_secret = sodium_crypto_box_secretkey($keypair1); $keypair1_public = sodium_crypto_box_publickey($keypair1); $keypair2 = sodium_crypto_box_keypair(); $keypair2_secret = sodium_crypto_box_secretkey($keypair2); $keypair2_public = sodium_crypto_box_publickey($keypair2); $encryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey($keypair1_secret, $keypair2_public); $encryption_key = sodium_bin2hex( $encryption_key ); //for my php encrypt code $decryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey($keypair2_secret, $keypair1_public); $decryption_key = sodium_bin2hex( $decryption_key ); //for my html page, then got by javascript
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda