Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda