I don't think that's going to be possible.
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?
Finally I was able to use cryptjs to do this and this little sample code helped a lot https://github.com/brainfoolong/cryptojs-aes-php.
Of course I couldn't use Crypt::decrypt.
Please or to participate in this conversation.