用 \t \r \n nodejs 加密字符串

Encrypting string with \t \r \n nodejs

module.exports.crypt = async ({ secretKey, ivKey }, data) => {

 let encryptedData = {};
 for (key in data) {
   const cipher = crypto.createCipheriv('aes-256-cbc', secretKey, ivKey);
   encrypted = cipher.update(data[key]);
   encrypted = Buffer.concat([encrypted, cipher.final()]);
   encryptedData[key] = encrypted.toString('base64');
 }
  return encryptedData;
}

此代码片段适用于普通加密,但当我尝试加密 'test\testemployee' 字符 '\t' 被省略(与 "\n" "\r" 相同)。有没有办法避免这种情况?

CBC 模式下的 AES 不关心您加密的内容:它只是加密您提供的字节(在大多数实现中将其填充到正确的大小之后)。

data[key] 是否包含正确的字符?如果是这样,那么您需要明确编码为例如调用 update 之前的 UTF-8。否则,您当然需要确保 data[key] 被分配了正确的值。

请注意,使用 let message = data[key] 后跟 encrypted = cipher.update(message) 会在调试器中显示消息的内容;不要着急!