AES Encryption Modes
AES-GCM, AES-CBC, AES-CTR

Introduction

When it comes to encryption, the Advanced Encryption Standard (AES) offers several modes of operation that cater to different security needs. Among these modes, AES-GCM (Galois/Counter Mode), AES-CBC (Cipher Block Chaining), and AES-CTR (Counter Mode) stand out. This article delves into the differences between these modes, their performance, key sizes, resource usage, and when to use each one.

AES Encryption

AES-GCM (Galois/Counter Mode)

AES-GCM is a modern encryption mode that provides both encryption and integrity. It combines the counter mode (CTR) for encryption and the Galois mode for authentication. This mode is widely used due to its efficiency and security.

async function encryptAESGCM(plainText, key) {
  const iv = window.crypto.getRandomValues(new Uint8Array(12)); // 12-byte IV for GCM

  const ciphertext = await window.crypto.subtle.encrypt(
    {
      name: "AES-GCM",
      iv: iv,
    },
    key,
    new TextEncoder().encode(plainText),
  );

  return { ciphertext, iv };
}

How AES-GCM Works:

AES-CBC (Cipher Block Chaining)

AES-CBC is a block cipher mode that uses an initialization vector (IV) to encrypt data in blocks. Each block of plaintext is XORed with the previous ciphertext block before being encrypted.

async function encryptAESCBC(plainText, key) {
  const iv = window.crypto.getRandomValues(new Uint8Array(16)); // 16-byte IV for CBC

  const ciphertext = await window.crypto.subtle.encrypt(
    {
      name: "AES-CBC",
      iv: iv,
    },
    key,
    new TextEncoder().encode(plainText),
  );

  return { ciphertext, iv };
}

How AES-CBC Works:

AES-CTR (Counter Mode)

AES-CTR is a block cipher mode that turns a block cipher into a stream cipher. It generates a unique keystream by encrypting successive values of a counter and then XORs this keystream with the plaintext.

async function encryptAESCTR(plainText, key) {
  const counter = window.crypto.getRandomValues(new Uint8Array(16)); // 16-byte counter for CTR

  const ciphertext = await window.crypto.subtle.encrypt(
    {
      name: "AES-CTR",
      counter: counter,
      length: 64, // Length of the counter in bits
    },
    key,
    new TextEncoder().encode(plainText),
  );

  return { ciphertext, counter };
}

How AES-CTR Works:

Comparison Table

FeatureAES-GCMAES-CBCAES-CTR
ConfidentialityYesYesYes
AuthenticationYesNoNo
IntegrityYesNoNo
PerformanceHighMediumHigh
Key Size128, 192, or 256 bits128, 192, or 256 bits128, 192, or 256 bits
Resources UsageModerate (slightly higher)ModerateLow
UsageIoT, messaging, network protocolsLegacy systems, non-critical applicationsHigh-performance, parallel processing
Use CasesSecure communication, TLS, IPsecFile encryption, database encryptionStreaming data, real-time encryption

Conclusion

Choosing the right AES mode depends on the specific requirements of your application. AES-GCM is the most secure option when both confidentiality and integrity are needed, while AES-CBC is suitable for data at rest. AES-CTR excels in performance for streaming applications. Understanding the differences and use cases of each mode will help you make informed decisions in your encryption strategy.

Latest blog posts

Explore the world of programming and cybersecurity through our curated collection of blog posts. From cutting-edge coding trends to the latest cyber threats and defense strategies, we've got you covered.