Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

omniware's avatar

Laravel 5.1 Encrypt in JavaScript and Decrypt in Laravel.

Hi I need to implement the Laravel's Crypt::encrypt($payload, $key) in Javascript/Jquery, that generate the cipher_text in a format that can be decrypted using Laravel's Crypt::decrypt function.

The Laravel's default Crypt functions uses APP_KEY as key to encrypt and decrypt. But as I have my own key to be used I used the following code to encrypt and decrypt.

private function encrypt($plain_text, $key)
{
    /* set the key for encryption */
    $encrypter = new \Illuminate\Encryption\Encrypter(substr($key, 0, 32), Config::get('app.cipher'));
    /* encrypt the plain text */
    $encrypted = $encrypter->encrypt($plain_text);

    return $encrypted;
}

public function decrypt($cipher_text, $key)
{
    /* set the key for decryption */
    $decrypter = new \Illuminate\Encryption\Encrypter(substr($key, 0, 32), Config::get('app.cipher'));
    /* decrypt the cipher text */
    $decrypted = $decrypter->decrypt($cipher_text);

    return $decrypted;
}

The only issues now is how to encrypt in Javascript. I tried CryptoJS but the encrypted string that it generates is very different from what Crypt::encrypt generate, and I get Exception DecryptException in BaseEncrypter.php line 45:The payload is invalid.

Can you please help?

0 likes
4 replies
omniware's avatar

If I do not want to use Laravel Crypt and achieve encrypting in AES-256-CBC on Javascript client side and decrypt on Laravel side, what are my options.

I know SSL already encrypts things, but I have peculiar situation.

Please or to participate in this conversation.