我已经像这样加密了我的令牌和数据json对象,并将其重定向到一个带有警卫角的子域Web应用:
// access website Wordpress/jQuery/Crypto-js 3.1.9-1
let encrypted = CryptoJS.AES.encrypt(JSON.stringify(response), SECRET_PASSPHRASE);
window.location.href = APP_HOME + "?access=" + encrypted;
重定向工作也一样,但是当我的警卫试图删除“访问”参数时,我遇到了这个错误。
Error: Malformed UTF-8 data
at Object.stringify (core.js:478)
at WordArray.init.toString (core.js:215)
at AuthGuard.push../src/app/_guards/auth/auth.guard.ts.AuthGuard.decryptData (auth.guard.ts:62)
at AuthGuard.push../src/app/_guards/auth/auth.guard.ts.AuthGuard.canActivate
我的解密功能-Angular WebApp-我尝试了很多变体,因为我发现很多人都遇到相同的错误,并出现了“格式错误的UTF-8数据”错误。
/* Angular - Crypto-js 3.1.9-1 */
import * as CryptoJS from 'crypto-js';
...
decryptData(data) {
try {
const bytes = CryptoJS.AES.decrypt(data.toString(), this.encryptSecretKey);
let decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
//let aa = CryptoJS.enc.Utf8.stringify(bytes)
// let aa = bytes.toString(CryptoJS.enc.Utf8);
console.log('decryptedData ==>', decryptedData);
return JSON.stringify(decryptedData);
} catch (e) {
console.log(e);
}
}