Skip to main content
Version: 1.0

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.

PropertyTypeDescription

plainText

ArrayBuffer

Data to be encrypted

key

ArrayBuffer

Encryption key (128, 192, or 256 bits)

mode

string

Optional: AES mode - "CBC", "CFB", "CTR", "OFB", or "ECB"

Returns: Encrypted message as ArrayBuffer

AES.decrypt()

Decrypts encrypted message to plain text.

PropertyTypeDescription

cypherText

ArrayBuffer

Data to be decrypted

key

ArrayBuffer

Decryption key (128, 192, or 256 bits)

mode

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

ModeDescriptionUse Case
CBCCipher Block ChainingGeneral purpose, most common
CFBCipher FeedbackStream-like encryption
CTRCounterParallel encryption/decryption
OFBOutput FeedbackStream cipher mode
ECBElectronic CodebookSimple but less secure

Key Sizes

  • 128-bit (16 bytes): Basic security
  • 192-bit (24 bytes): Enhanced security
  • 256-bit (32 bytes): Maximum security