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

同步 · dev.to / @markyu

AES-CBC vs AES-GCM: The Crypto Choice I’d Make Today

A practical block cipher guide for developers explaining AES modes, CBC pitfalls, GCM authentication, IV rules, and what to avoid in production.

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

本页目录

  • The Quick Map
  • Never Use ECB
  • CBC Needs More Than Encryption
  • Why I Prefer GCM
  • Practical Rules
  • Where Block Ciphers Still Matter
  • Final Thought

Most crypto bugs I have seen were not caused by someone inventing a new cipher.

They were caused by using a good cipher in the wrong mode, reusing an IV, skipping authentication, or copying a Stack Overflow snippet without understanding the contract.

If you are building a normal application in 2026, my default advice is simple:

Prefer authenticated encryption like AES-GCM. Avoid raw AES-CBC unless you know exactly how you will authenticate the ciphertext.

The Quick Map

ModeWhat it gives youMain risk
AES-ECBencryption onlyleaks patterns, avoid it
AES-CBCencryption onlyneeds padding and MAC
AES-CTRstream-like encryptionnonce reuse is dangerous
AES-GCMencryption + authenticationnonce must be unique

The important word is authentication.

Encryption hides content. Authentication tells you whether the ciphertext was modified.

Never Use ECB

ECB encrypts equal plaintext blocks into equal ciphertext blocks.

That means patterns can leak.

same plaintext block  -> same ciphertext block
same plaintext block  -> same ciphertext block

This is why ECB is the classic "do not use this" example.

CBC Needs More Than Encryption

CBC uses an initialization vector:

plaintext + key + IV -> ciphertext

The IV must be unpredictable and unique for the message.

The bigger issue: CBC does not authenticate data by itself.

That means this is incomplete:

ciphertext = AES-CBC-Encrypt(key, iv, plaintext)

You also need a MAC, usually encrypt-then-MAC:

ciphertext = AES-CBC-Encrypt(encKey, iv, plaintext)
tag = HMAC(macKey, iv + ciphertext)

If you skip the MAC, attackers may be able to tamper with ciphertext and learn things from error behavior.

Why I Prefer GCM

AES-GCM gives encryption and authentication together:

plaintext + key + nonce + aad -> ciphertext + tag

You still need to manage nonces correctly, but the API nudges you toward the right model.

Node.js example:

import crypto from "node:crypto";

const key = crypto.randomBytes(32); // AES-256
const iv = crypto.randomBytes(12);  // recommended GCM nonce size

const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
const ciphertext = Buffer.concat([
  cipher.update("secret message", "utf8"),
  cipher.final(),
]);
const tag = cipher.getAuthTag();

console.log({ iv, ciphertext, tag });

Decrypt:

const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
decipher.setAuthTag(tag);

const plaintext = Buffer.concat([
  decipher.update(ciphertext),
  decipher.final(),
]);

console.log(plaintext.toString("utf8"));

If the ciphertext or tag is modified, decryption fails.

That is exactly what you want.

Practical Rules

  • Do not use ECB.
  • Do not hardcode keys.
  • Do not reuse GCM nonces with the same key.
  • Do not store encryption keys beside encrypted data.
  • Do not invent your own padding or MAC scheme.
  • Use high-level libraries when possible.

Where Block Ciphers Still Matter

You may hit this when building:

  • encrypted backups
  • secure tokens
  • internal secrets tooling
  • file encryption
  • compliance-heavy backend systems

For normal password storage, do not use AES. Use a password hashing algorithm such as Argon2, bcrypt, or scrypt.

That distinction is important:

password storage -> hash
data secrecy     -> encryption

Final Thought

Crypto code should be boring. If your encryption design feels clever, slow down.

For most application work, AES-GCM or a trusted high-level library is the safer path than hand-rolling AES-CBC with padding and MAC logic.

Have you ever had to review encryption code in an application backend?

相关阅读

Next.js Images Without CLS: My LQIP Blur-Up Setup

A practical Next.js image optimization guide for zero CLS layouts, blur placeholders, dimensions, remote images, and production image hygiene.

nextjs

Bad Data Quality Costs More Than a Slow Query

A practical data quality guide for engineers: validation, ownership, schema drift, observability, and fixing bad data before dashboards lie.

data

Network Address Calculation: The Subnet Math That Matters

A practical subnetting guide showing how to calculate a network address from an IP address and mask using binary math and simple examples.

networking

原文发布

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

在 dev.to 继续阅读
上一篇Sustainable Cloud Design Starts With Boring Cost SignalsA practical cloud sustainability guide for developers: right-sizing, autoscaling, regions, storage lifecycle, carbon-aware thinking, and cost visibility.
返回全部文章
下一篇Kubernetes Is Useful, but Only After These Basics HurtA practical Kubernetes guide for developers: pods, deployments, services, config, scaling, and when not to introduce Kubernetes too early.
返回档案
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
始于 2024
ZhenXiao Mark YuZhenXiao Mark Yu
联系

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

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

开启对话
频道开放

随时打个招呼 · 2026

--:--:--EST加拿大 安大略
  • 邮件
  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat

订阅

偶尔收到一封简讯

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

作品

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

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

资源

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

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

工作室

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

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

社交

在常去的平台上找到我。

  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
  • 邮件
© 2026 ZhenXiao Mark Yumarkyu0615@gmail.com
  • 邮件
  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
隐私条款由 Next.js 16 · React 19 · Tailwind 4 构建