I have a Laravel 5.7 application which encrypts an object and returns the encrypted object to my VueJS application.
PLEASE NOTE the below code is for explanation purposes and I am not using in this way, however, the concept is.
Laravel Function triggered by a route
public function license() {
var data = [{id: 1}, {expiry_date: '2019-02-25T10:47:12+00:00'}];
$encrypted = \Crypt::encrypt(JSON.stringify(data));
return $encrypted;
}
Vue JS Method for Retrieving Object
checkLicense() {
this.$http.get(LARAVEL_ROUTE).then(res => {
var key = 'LARAVEL_APP_KEY'; // NOT REAL
var bytes = CryptoJS.AES.decrypt(res.data, 'LARAVEL_APP_KEY');
var plaintext = bytes.toString(CryptoJS.enc.Utf8);
});
}
The above produces the following:
Uncaught (in promise) Error: Malformed UTF-8 data
Things I've tried:
I have checked the LARAVEL_APP_KEY and I can see that it is prefixed with 'base64:' so I have tried removing this from my VueJS method where I declare the key but this made no difference.
I have also removed the object and tried encrypting a string which produces the same result as above.
Additional question:
I'd also like to encrypt the string / object with something other than the LARAVEL_APP_KEY as I don't want to store that value in my VueJS application. The key doesn't need to be super secure however I would rather not use the LARAVEL_APP_KEY