Skip to content
LOCZH/安大略 · 加拿大待机OK/--:--:--EST
M4M4RK_YUportfolio
  • 项目
    项目Overview
    • 作品精选案例与项目记录
    • 游戏可玩原型与游戏开发日志
  • 影像
    影像Overview
    • 档案影像合集与视觉实验
    • 商店印刷品、海报和限量物件
  • 日志
    日志Overview
    • 博客长篇开发日志与现场笔记
    • 笔记短观察、链接与代码片段
  • 资源
    资源Overview
    • 工具38 款浏览器内开发工具
    • 链接每日使用的开发与设计书签
  • 关于
  • 联系
EN

同步 · dev.to / @markyu

Encrypting with Block Ciphers: A Guide to AES, CBC, and More

In today's digital world, block ciphers are fundamental to online encryption. They handle most of...

发布日期
May 4 '24
·
阅读时长
6 min read
·
点赞
9
securitycryptographyblockcipherencryption
在 dev.to 查看

Image description

In today's digital world, block ciphers are fundamental to online encryption. They handle most of the encryption on the internet, securing our communications and data. In this blog post, we'll explore the key aspects of block ciphers, delve into its popular modes of operation, as well as take a closer look at the Advanced Encryption Standard (AES).

What Are Block Ciphers?

Block ciphers are the workhorse of encryption on the internet today. They handle the bulk of the encryption that is done over the internet. Block ciphers encrypt a fixed-length chunk of bits called a block where each block is encrypted separately.

Block ciphers are made up of a family of three functions and are specified as follows:

  1. A keygen function that accepts a security parameter 𝑘 and outputs a random 𝑘-bit key: 𝑘→{0,1}𝑘

  2. An encryption that accepts a 𝑏-bit plaintext and 𝑘-bit key and outputs a 𝑏-bit ciphertext: {0,1}𝑏×{0,1}𝑘→{0,1}𝑏

  3. A decryption that accepts a 𝑏-bit ciphertext and 𝑘-bit key and outputs a 𝑏-bit plaintext: {0,1}𝑏×{0,1}𝑘→{0,1}𝑏

An important parameter in block ciphers is the block length 𝑏. It determines how large of chunks the overall ciphertext and plaintext will be parsed into.

An ideal block cipher requires the encryption to be injective and surjective (bijective). This means that each plaintext maps to a unique ciphertext and each ciphertext maps to a unique plaintext. This creates an encryption method that essentially just creates a permutation (shuffle) of 𝑏-bit strings.

Image description

If we have a 𝑏-bit block, there is 2𝑏 possible input plaintext messages and 2𝑏 possible output ciphertext messages. There are also 2𝑏! Permutations of 2𝑏 elements. If the key used is 𝑘-bits, then there are 2𝑘 possible key values, and each key “chooses” some permutation from the permutation space. 2𝑘 is often much smaller than 2𝑏! So there’s usually a large number of permutations that aren’t possible for a cipher.

A block cipher can be thought of as a large collection of codebooks. A codebook is a book with a list of plaintext elements on one side and a list of associated ciphertext elements on the other. The key is simply used to choose which book to use.

Image description

Modes of Operation

A modern block cipher typically encrypts 16 bits at a time, so to encrypt a large plaintext, it must first be parsed into smaller blocks. The mode of operation defines the method used to combine a block cipher and encrypt a large block of data.

Electronic Codebook Mode (ECB)

Image description

The simplest mode of operation is the electronic codebook mode (ECB). It takes the plaintext, divides it into individual 𝑏-bit blocks, and encrypts each one separately using the same key. A 16-bit data block using an 8-bit block cipher will first be divided into 2 blocks, encrypted, each with the same key, and then recombined to create a 16-bit ciphertext.

If using a block size of 128 bits, then the codebook would have 2128 entries and be so large that you could never write it down on paper.

A limitation of this mode of operation is that on a long block of repeated data, patterns will start to emerge, revealing information about both the key and the plaintext. This mode of operation is not secure under eavesdropping (IND-EAV).

Cipher Block Chaining (CBC)

Image description

The idea of cipher block chaining (CBC) mode is to exclusive or (XOR) the plaintext of every block with the ciphertext of the previous block. This combined value is the data that is then put into the encryption function.

Using this mode of operation, even large blocks of structured data lose their structure due to the feed-forward nature of the encryption mode.

Since the first block of plaintext has no previous block of ciphertext to use, an initialization vector (IV) is used to get things going.

Due to the symmetry of the XOR operation, when decrypting, you can follow the same path as encryption, just in reverse. Note that only the vertical arrows reverse direction in the diagrams to the left, as the previous ciphertext block is still used to decrypt the plaintext following it.

Image description

Image description

An essential property of the mode of operation is that it easily allows for randomized encryption. Different IVs produce different ciphertexts on the same plaintext, and this property can be used to protect the encryption method from chosen plaintext attacks. Note that the IV is not a secret, is sent along with the ciphertext, can only be used once, and must not be predictable.

Counter Mode (CTR)

Image description Counter mode (CTR) is different from CBC mode as you never actually put the plaintext through the encryption function. Instead, you encrypt a counter, and XOR the result with the plaintext. When encrypting using CTR mode, the first IV is decided upon, and then the subsequent IVs simply increment by 1. So, in an example where the IV is 2 bits, the first block might be 00, the next 01, and so on….

Decryption is the exact same as encryption, on the inputs to the XOR function are reversed. Notice that decryption involves the encryption method of the block cipher. This model is not vulnerable to padding oracle attacks, has a simple implementation, and the computation is parallelizable, pre-processable, and random access. However, this mode is not safe for small block lengths (<128-bits).

Advanced Encryption Standard (AES)

Image description

Advanced encryption standard (AES) is the most commonly used block cipher on the internet today. At a high level, the AES cipher uses a 128-bit block. This means it takes 128 bits of plaintext and produces 128 bits of ciphertext. The key can either be 128-bits, 192-bits, or 256-bits. The plaintext is combined with the key through multiple passes of a round.

Each stage has identical rounds. The key is changed at every round using a key schedule, which creates different sub-keys for each round.

AES uses 2 types of operations:

  1. Operations that act on bytes (8-bits)

  2. Operations that act on bits

Image description

The first operation is called SubBytes and is a bytewise substitution. A byte has 256 substitution possibilities so the substition lookup table (s-box) has 256 entries. There are 256! potential s-boxes that can be created, but instead of choosing a random permutation, AES designers chose an algebraic relationship. The output byte is the algebraic inverse of the input byte in what’s called a galwa field, and for good measure a constant value is added.

S-boxes are designed to be highly non-linear. The idea is that a small change in the input to induce a large change in the output.

The next step in AES is called shiftRows where all 16-bytes are arranged as a 4x4 square and a circular shift is applied. The top row is unchanged, the second row is shifted by 1 byte, the third row is shifted by 2 bytes, and the final row is shifted by 3 bytes.

The third operation is called MixColumns and applies galwa field arithmetic again, but this time using matrix multiplication. Instead of applying the changes to the rows, they’re applied to the columns. This is designed to create a property called diffusion. The goal is for the variables that appear in the equation to spread out.

In the last step, called AddRoundKey, the round-specific sub-key is XORed with all bits from the previous step. The output of the process is fed into the input of the next round, and the process continues. For most people, AES can be treated like a black box. What matters most is not how it works but that a random secret key is generated when using it, the mode of operation is safe and randomized, and the initialization vector is unpredictable.

Overall, Block ciphers are vital for internet security, underpinning our communications and data protection. With various modes of operation, such as ECB, CBC, and CTR, and the robust AES algorithm, we have powerful tools to secure information. It's crucial to understand these encryption methods, as they are key to maintaining privacy and security in the digital age.

相关阅读

database

The True Cost of Poor Data Quality: Why It Matters and How to Improve It

In today’s fast-paced, data-driven world, businesses have more access to data than ever before....

ipaddresses

How to Determine the Network Address from a Known IP Address

Ever wondered how devices communicate within a network? Or perhaps you've come across terms like "IP...

java

Advanced Java: Simplifying Object Property Copy and Manipulation with BeanUtil

In Java programming, the BeanUtil utility class is a powerful and convenient tool for simplifying the...

原文发布

本文首发于 dev.to,评论与点赞保留在原站。

在 dev.to 继续阅读
上一篇Tips for Implementing Sustainable Cloud DesignIntroduction Hey there! I'm Mark, a software engineer passionate about the intersection...
返回档案
下一篇Mastering Kubernetes: A Guide to Container OrchestrationIn the rapidly evolving landscape of modern software development, Kubernetes (often abbreviated as...
返回档案
频道开放·随时打个招呼 · 2026
--:--:--EST
联系

看到什么有意思的?和我聊聊。

这是一个作品集,不是服务 · 但每一条留言我都会看 — 如果哪里让你有所触动,或者只想打个招呼,欢迎写信过来。

开启对话

订阅

偶尔收到一封简讯

来自 m4rkyu.com 的笔记与日志——简短、标注日期、没有杂音。随时可退订。

作品

线上发布、游戏作品与视觉档案。

  • 项目
  • 游戏
  • 档案
  • 日志

资源

每日好用的工具与个人收藏的链接库。

  • 搜索
  • 最新
  • 工具
  • 链接
  • 笔记
  • 主题
  • RSS
  • JSON Feed
  • 商店

工作室

背景、联系方式以及合作渠道。

  • 关于
  • 联系
  • 更新日志
  • 技术说明
  • 简历筹备中

社交

在常去的平台上找到我。

  • Facebook敬请期待
  • Instagram敬请期待
  • YouTube敬请期待
  • 领英敬请期待
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
始于 2024
ZhenXiao Mark YuZhenXiao Mark Yu
© 2026 ZhenXiao Mark Yu·加拿大 安大略
  • 邮件
  • GitHub
  • dev.to
  • 领英 (敬请期待)
  • 推特 / X (敬请期待)
  • Instagram (敬请期待)
由 Next.js 16 · React 19 · Tailwind 4 构建

由 Next.js 16 · React 19 · Tailwind 4 构建