AES Encryption
Since version 1.6.0 - Gen 3 and Gen 4 devices only
The AES API provides cryptographic functions for encrypting and decrypting data using the Advanced Encryption Standard (AES) algorithm.
Overview
AES encryption allows scripts to:
- Encrypt sensitive data before transmission
- Decrypt received encrypted data
- Support multiple AES modes (CBC, CFB, CTR, OFB, ECB)
- Use 128, 192, or 256-bit keys
API Reference
AES.encrypt()
Encrypts plain text message to encrypted cipher text.
| Property | Type | Description |
|---|---|---|
| ArrayBuffer | Data to be encrypted |
| ArrayBuffer | Encryption key (128, 192, or 256 bits) |
| string | Optional: AES mode - "CBC", "CFB", "CTR", "OFB", or "ECB" |
Returns: Encrypted message as ArrayBuffer
AES.decrypt()
Decrypts encrypted message to plain text.
| Property | Type | Description |
|---|---|---|
| ArrayBuffer | Data to be decrypted |
| ArrayBuffer | Decryption key (128, 192, or 256 bits) |
| string | Optional: AES mode - "CBC", "CFB", "CTR", "OFB", or "ECB" |
Returns: Decrypted message as ArrayBuffer
Complete Example
// AES encryption/decryption example using CBC mode
// Helper functions for hex conversion
function hex2a(hex) {
hex = hex.toString();
let str = "";
for (let i = 0; i < hex.length; i += 2) {
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
function toHex(buffer) {
let s = "";
for (let i = 0; i < buffer.length; i++) {
s += (256 + buffer[i]).toString(16).substr(-2);
}
return s;
}
function fromHex(hex) {
const arr = new ArrayBuffer(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
arr[i >> 1] = parseInt(hex.substr(i, 2), 16);
}
return arr;
}
// 256-bit key
const key = fromHex("dd469421e5f4089a1418ea24ba37c61bdd469421e5f4089a1418ea24ba37c61b");
function encrypt() {
const plainText = "This is one big, big secret no one should know!!";
console.log("Message to encrypt:", plainText);
const cypherText = AES.encrypt(plainText, key, {
mode: "CBC"
});
// Decrypt to verify
const decrypted = AES.decrypt(cypherText, key, {
mode: "CBC"
});
// Convert from ArrayBuffer to String
const decryptedText = hex2a(toHex(decrypted));
console.log("Decrypted message:", decryptedText);
return cypherText;
}
// Run encryption example
encrypt();
Supported Modes
| Mode | Description | Use Case |
|---|---|---|
| CBC | Cipher Block Chaining | General purpose, most common |
| CFB | Cipher Feedback | Stream-like encryption |
| CTR | Counter | Parallel encryption/decryption |
| OFB | Output Feedback | Stream cipher mode |
| ECB | Electronic Codebook | Simple but less secure |
Key Sizes
- 128-bit (16 bytes): Basic security
- 192-bit (24 bytes): Enhanced security
- 256-bit (32 bytes): Maximum security