带有特殊字符的AES加密问题

时间:2020-09-25 08:37:06

标签: javascript angular encryption aes

我正在尝试使用AES角度加密密钥。当字符串中没有特殊字符时,加密逻辑工作良好,但是如果有特殊字符,解密时我将得到垃圾字符。下面是我在angular中的加密逻辑

apikey_encrypt('N33yKKHeX/2jhlTomUQ5reYypP6rCk8MrHrkjSJSerNEAce3tVhvP656NE5rJbrquDLSQ0i5p1B5C1nK6XYR','prod') {
    var db_env;
    if (db_config === 'prod') {
        db_env = 'EiOjLcOtbfX46zrX8kcLp4ZXzOyJe19AwBf9xXATbLJceqnDjCx1FvMxRDhrEqHJrC2nBH33o3N7BR/nzHoN'
    }
    else {
        db_env = environment.apiKey
    }
    var cur_timestamp = Math.floor(Date.now());
    var concatenated_api_key = 'N33yKKHeX/2jhlTomUQ5reYypP6rCk8MrHrkjSJSerNEAce3tVhvP656NE5rJbrquDLSQ0i5p1B5C1nK6XYR'+'&qit'+cur_timestamp+'/tin'
    const key = CryptoJS.enc.Utf8.parse(db_env);
    const iv = CryptoJS.enc.Utf8.parse(environment.ivstring);
    var encrypted = CryptoJS.AES.encrypt(concatenated_api_key, key, {
    // instead of message try some string or “9876543210”
    iv: iv,
    keySize: 256,
    padding: CryptoJS.pad.NoPadding,
    mode: CryptoJS.mode.CTR
    });
    // var encrypted_mob = salt.toString() + iv.toString() +
    encrypted = encrypted.toString();
    const headers = { 'Content-type': 'application/json', 'x-api-key': encrypted }
    return headers;
}

0 个答案:

没有答案