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

317
Views
Javascript Photo Encrypt con CryptoJS a PHP Decrypt con Openssl

Estoy tratando de cifrar una foto en la interfaz y enviarla al servidor para que la descifre.

Como nota, también estoy cifrando la aeskey y estoy enviando al back-end la foto cifrada y la aeskey cifrada. En el back-end, estoy usando una clave privada para descifrar la aeskey (la probé y estoy descifrando con éxito esta aeskey).

"Logré" descifrar la foto, pero está dando un resultado totalmente diferente al de la cadena que estaba antes del cifrado de la interfaz.

 public encryptPhoto(string) { string.replace('data:image/jpeg;base64,',''); let iv = CryptoJS.enc.Hex.parse("abcdef9876543210abcdef9876543210"); let encAesKey = CryptoJS.enc.Hex.parse(this.aesKey); this.encryptedAESImage = CryptoJS.AES.encrypt(string, encAesKey, {iv:iv}); return this.encryptedAESImage.toString(); } //generates a random aeskey public aesKeyGenerator(length: number) { let result = ''; let characters = 'abcdef0123456789'; let charactersLength = characters.length; for ( var i = 0; i < 32; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; }

Y el código en el backend

 public function photoSubmit(Request $request){ $user = Auth::user(); $sa_ID = $user->id; $input = $request->all(); $key = Storage::get('/keys/key.pem'); // stored private key $decryptedkey = openssl_pkey_get_private($key); //extract private key if(openssl_private_decrypt(base64_decode($input['aeskey']), $aes_decrypted, $decryptedkey)){ //decrypt aeskey $final_key = hex2bin($aes_decrypted); $final_IV = hex2bin("abcdef9876543210abcdef9876543210"); $decripted_photo = openssl_decrypt($input['photo'], 'AES-256-CBC', $aes_decrypted,OPENSSL_ZERO_PADDING, $final_IV); Storage::put('ci/test.txt', $decripted_photo); //stores the string in a file so i can compare the string when it can't be sent as a message return response()->json([ 'error' => 0, 'message' => utf8_encode($decripted_photo) ], 200); } return response()->json([ 'error' => 0, 'message' => 'A aparut o eroare' ], 200); }

nota adicional

  • en php, si trato de descifrarlo sin OPENSSL_ZERO_PADDING, no se descifrará aunque sea la clave correcta y IV. Traté de averiguar por qué, pero no lo entendí muy bien, por lo que ayudaría si alguien tiene una buena explicación.

  • La imagen se transforma en una cadena usando el método FileReader.readAsDataURL()

PD: es mi primera publicación, así que si no brindé suficiente información, dímelo y actualizaré la publicación.

about 4 years ago · Juan Pablo Isaza
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!