Symmetric encryption
We first encountered symmetric encryption in Chapter 4, Symmetric Encryption in Node.js, where we covered two ciphers: AES and ChaCha20-Poly1305.
In this chapter, we'll focus solely on AES, given that it's the only symmetric cipher that's standardized in the WebCrypto APIs and available on all browsers, with a few different modes of operation. We'll look at encrypting and decrypting data using AES-GCM and AES-CBC, and then we'll use AES-KW (RFC 3394) for wrapping and unwrapping keys.
While we won't cover ChaCha20-Poly1305, you can find several packages on npm
that offer support for that, including implementations in pure JavaScript or that leverage native code via WebAssembly.
Encrypting and decrypting messages with AES
The WebCrypto APIs include two methods for encrypting and decrypting data:
crypto.subtle.encrypt(algorithm, key, data)
crypto.subtle.decrypt(algorithm, key, data)
Both methods have...