AES
AES block size will be determined be the size of the key. Here are the block size for the variout key sizes:
Key size | Block size |
---|---|
16 bytes | 128 bits |
24 bytes | 128 bits |
32 bytes | 128 bits |
IV must be 16 bytes long.
Key & IV
The easiest way to obtain a key or an iv is by using the randomBytes utility function.
const aes_key = Cryptopp.utils.randomBytes(32);
const aes_iv = Cryptopp.utils.randomBytes(16);
Key & IV can be also passed as a string. Both the string key and string iv must be HEX
encoded because UTF-8 allows variable length, multi-byte characters, so a string that is 16 characters long may not be 16 bytes long.
Encrypt
Cryptopp.AES.encrypt(message, key, iv, 'cbc');
Parameters
Parameter | Type | Required | Default |
---|---|---|---|
data | string ArrayBuffer | ✅ | |
key | HEX-encoded string ArrayBuffer | ✅ | |
iv | HEX-encoded string ArrayBuffer | ✅ | |
mode | "ecb" "cbc" "cbc_cts" "cfb" "ofb" "ctr" "xts" | ✅ | |
encodeTo | "hex" "base64" "base64url" | ❌ | "base64" |
Returns: encoded string
, base64 by default
Decrypt
Cryptopp.AES.decrypt(encrypted_message, key, iv, 'cbc');
Parameters
Parameter | Type | Required | Default |
---|---|---|---|
data | string ArrayBuffer | ✅ | |
key | HEX-encoded string ArrayBuffer | ✅ | |
iv | HEX-encoded string ArrayBuffer | ✅ | |
mode | ecb cbc cfb ofb ctr | ✅ | |
dataEncoding | utf8 hex base64 base64url | ❌ | base64 |