我的代码不适用于Unicode字符(特殊字符)
我的代码-
static convertStringToBytes(toConvert) {
if (typeof toConvert !== 'string') {
return;
}
// Used to convert a string to a utf8 compatible string
toConvert = unescape(encodeURIComponent(toConvert));
return toConvert.split('')
.map(char => char.charCodeAt(0));
}
static convertBytesToString(toConvert) {
if (!Array.isArray(toConvert)) {
return;
}
toConvert = toConvert
.map(x => String.fromCharCode(x))
.join('');
// Used to convert a string to a utf8 compatible string
return decodeURIComponent(escape(toConvert));
}
这些是在加密和解密方法中使用的方法,
let str1 = 'Password123456';
let ecd = this._encryptionService.encryptedString(str1, this.key);
console.log("ecd 1", ecd);
let dcd = this._encryptionService.decryptedString(ecd, this.key);
console.log("dcd 1", encodeURIComponent(dcd));
let str2 = 'öä#';
let ecd2 = this._encryptionService.encryptedString(str2, this.key);
console.log("ecd 2", ecd2);
let dcd2 = this._encryptionService.decryptedString(ecd2, this.key);
console.log("dcd 1", dcd2);
对于使用特殊字符的密码,它不起作用并给出错误信息。 URI malformed at decodeURIComponent (<anonymous>)