lance faustino
lance faustino

Reputation: 3

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;
}

this code snippet works on normal encryption but when i tried encrypting 'test\testemployee' the character '\t' gets omitted (same with "\n" "\r"). Is there a way to avoid this?

Upvotes: 0

Views: 82

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 94088

AES in CBC mode doesn't care about what you encypt: it simply encrypts the bytes that you supply (after padding it to the right size in most implementations).

Does data[key] contain the right characters? If so then you need to explicitly encode to e.g. UTF-8 before calling update. Otherwise you need to make sure that data[key] gets assigned the right value of course.

Note that using let message = data[key] followed by encrypted = cipher.update(message) would show you the contents of a message in the debugger; don't be hasty!

Upvotes: 2

Related Questions