I've been researching SubtleCrypto: for a project, and here's a summary of what I found useful.
Ready to learn more? Subscribe to our newsletter for weekly tutorials and tips.
正文
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since January 2020.
This feature is available only in This feature is available in It takes as its arguments a
to encrypt with, some algorithm-specific parameters, and the data to encrypt (also known as "plaintext").
which will be fulfilled with the encrypted data (also known as "ciphertext"). encrypt(algorithm, key, data)
containing the data to be encrypted (also known as the containing the "ciphertext".
The promise is rejected when the following exceptions are encountered:
Raised when the requested operation is not valid for the provided key (e.g., invalid encryption algorithm, or invalid key for the specified encryption algorithm).
Raised when the operation failed for an operation-specific reason (e.g., algorithm parameters of invalid sizes, or AES-GCM plaintext longer than 2
The Web Crypto API provides four algorithms that support the
, and they're all based on the same underlying cipher, AES (Advanced Encryption Standard).
The Web Crypto API supports three different AES modes: CBC (Cipher Block Chaining)
, which includes checks that the ciphertext has not been modified by an attacker.
attacks, in which an attacker can ask the system to decrypt arbitrary messages, and use the result to deduce information about the
While it's possible to add authentication to CTR and CBC modes, they do not provide it by default and when implementing it manually one can easily make minor, but serious mistakes.
GCM does provide built-in authentication, and for this reason it's often recommended over the other two AES modes.
More Details
There are a few more points worth noting. First, browser compatibility varies across different browsers. Second, performance optimization is crucial when handling large amounts of data. Finally, key management is also an important consideration.
If you found this useful, please like and share! Follow for more content on this topic.
Reference: SubtleCrypto: encrypt() method - Web APIs | MDN