I am trying to encrypt multiple strings using same cipher with the code
var iv = _crypto.randomBytes(16).slice(0, 12);
var salt = _crypto.randomBytes(64);
var key = _crypto.pbkdf2Sync(symmetricKey, salt, 2145, 32, 'sha512');
var cipher = _crypto.createCipheriv('aes-256-gcm', key,iv);
var a = Buffer.concat([cipher.update("data1", 'utf8'), cipher.final()]);
var b = Buffer.concat([salt, iv, a]).toString('base64');
var c = Buffer.concat([cipher.update("data2", 'utf8'), cipher.final()]);
The execution failed in the last line without showing any error. "TypeError: error:00000000:lib(0):func(0):reason(0)"
On investigating further, I came to know that we can't use the cipher once we have done cipher.final(). But if I won't do it earlier(during encryption of data1), during decryption of "encrypted format of data1", it would fail since cipher.final returns any remaining enciphered contents which gets appended with the original encrypted string.
What's the best way to encrypt multiple strings then, or should I create separate ciphers for all the strings?