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

314
Vistas
Javascript Photo Encrypt with CryptoJS to PHP Decrypt with Openssl

I am trying to encrypt a photo in the frontend and send it to the backend to be decrypted.

As a note, I am encrypting the aeskey too and I'm sending to the back end the encrypted photo and the encrypted aeskey. On the back end I'm using a privatekey to decrypt the aeskey (tested it and I'm successfully decrypting this aeskey).

I've 'succeeded' in decrypting the photo but it's giving a totally different result than the string that was before the frontend encryption.

 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;

    }

And the code on the 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);
    
    }

Aditional note

  • in php, if I try to decrypt it without OPENSSL_ZERO_PADDING it won't decrypt even thought it's the correct key and IV. I've tried to find out why but I didn't understand it pretty well so it would help if anyone has a good explanation

  • The image is transformed to string using the FileReader.readAsDataURL() method

P.S. It's my first post so if I didn't give enough info please tell me and i'll update the post

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