Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

394
Views
Cifrar con la biblioteca Javascript SJCL, descifrar con PHP

Estoy usando esta biblioteca: https://github.com/bitwiseshiftleft/sjcl

Quiero cifrar una cadena del lado del cliente, pasarla a través de la URL y luego descifrarla en el backend.

Este es el JS:

 <script src="https://bitwiseshiftleft.github.io/sjcl/sjcl.js"></script> <script> var ciphertext = sjcl.encrypt("password", "Hello World!") var plaintext = sjcl.decrypt("password", ciphertext) console.log(ciphertext) console.log(plaintext) </script>

Resultados.

consola.log (texto sin formato) :

 {"iv":"XA+HFebsM7WiVy0Ko8EEuA==","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"f4BX8a9fUPM=","ct":"a4LesnrsT7C6MmkxZifSw7FsDyI="}

consola.log(texto cifrado) :

 Hello World!

Como puede ver, el cifrado y el descifrado funcionan según lo previsto con JS. El problema viene cuando trato de descifrarlo usando PHP:

 $password = 'password'; $input = json_decode('{"iv":"XA+HFebsM7WiVy0Ko8EEuA==","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"f4BX8a9fUPM=","ct":"a4LesnrsT7C6MmkxZifSw7FsDyI="}', true); $digest = hash_pbkdf2('sha256', $password, base64_decode($input['salt']), $input['iter'], 16, true); $cipher = $input['cipher'] . '-' . $input['ks'] . '-' . $input['mode']; $ct = substr(base64_decode($input['ct']), 0, - $input['ts'] / 8); $tag = substr(base64_decode($input['ct']), - $input['ts'] / 8); $iv = base64_decode($input['iv']); $adata = $input['adata']; $dt = openssl_decrypt($ct, $cipher, $digest, OPENSSL_RAW_DATA, $iv, $tag, $adata); var_dump($dt); while ($msg = openssl_error_string()) { echo $msg . "\n"; }

El resultado de var_dump($dt) es false , que es lo que sucede cuando openssl_decrypt no puede descifrar la cadena. Seguramente me estoy perdiendo algo, ¿alguien podría ayudarme a notar qué es?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Lo tengo funcionando cuando uso el modo 'gcm' con un iv de 128 bits.

 var key = 'password'; var p = { mode: 'gcm', iv: sjcl.random.randomWords(4, 0) }; var encrypted = sjcl.encrypt(key, 'Hello World', p);

Esto conduce a la salida, por ejemplo:

 {"iv":"/Jyo0DNWt0CNUW6AYkpnBw==","v":1,"iter":10000,"ks":128,"ts":64,"mode":"gcm","adata":"","cipher":"aes","salt":"jFSuZEEICq8=","ct":"7+M0Wv79zKXu4SUYJPZAIIBYgw=="}

Esta salida JSON se usa como entrada en PHP:

 $input = json_decode('<JSON output from above goes here>', true);

que se puede pasar al descifrado como se menciona en el código de ejemplo anterior:

 $password = 'password'; $digest = hash_pbkdf2('sha256', $password, base64_decode($input['salt']), $input['iter'], 0, true); $cipher = $input['cipher'] . '-' . $input['ks'] . '-' . $input['mode']; $ct = substr(base64_decode($input['ct']), 0, - $input['ts'] / 8); $tag = substr(base64_decode($input['ct']), - $input['ts'] / 8); $iv = base64_decode($input['iv']); $adata = $input['adata']; $dt = openssl_decrypt($ct, $cipher, $digest, OPENSSL_RAW_DATA, $iv, $tag, $adata); var_dump($dt);

Esto funcionó para mí usando PHP versión 7.1.9

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!