Encrypting and decrypting
Encrypting and decrypting are very simple using the Encrypt class, provided you have the correct keys generated for your given algorithms. This is usually simple, since most applications will only use one algorithm and one key throughout their application, but if you are using multiple different encryption algorithms in different scenarios, you need to make sure to always match up the correct encryption keys with the correct encryption algorithms. Additionally, anything encrypted with a given algorithm must be decrypted using that same algorithm and key, or else it will fail. This may be obvious to you, but to many this can be an easy and confusing mis-step when building encryption for their application.
const encrypted = Encrypt.encrypt('helloworld', {
algorithm: 'aes-256-gcm',
key: process.env.APP_ENCRYPTION_KEY!,
})
const decrypted = Encrypt.decrypt(encrypted, {
algorithm: 'aes-256-gcm',
key: process.env.APP_ENCRYPTION_KEY!,
})
console.log(decrypted)
// 'helloworld'