AES で暗号化する方法
AES 共通鍵暗号で平文を暗号化します。鍵サイズ、モード、IV の生成を解説します。
问题
同じ鍵を持つ人だけが復号できるように、AES でデータを暗号化する必要があります。
前提条件
- 共通鍵暗号の理解
- 鍵と IV に関する知識
ステップ
-
1. Generate or obtain a key
AES supports 128, 192, or 256-bit keys. Generate a cryptographically random key of the chosen size. Never derive a key from a password by simple hashing; use a KDF like PBKDF2 or scrypt instead.
入力256-bit key输出32 random bytes -
2. Generate an IV or nonce
For modes like CBC and CTR, generate a random 16-byte IV for each encryption. The IV does not need to be secret but must be unique and unpredictable. Reusing an IV with the same key is catastrophic for security.
入力random IV输出16 random bytes -
3. Encrypt and store the IV with the ciphertext
Encrypt the plaintext using the key, IV, and chosen mode. Store the IV alongside the ciphertext (it is needed for decryption). Use an authenticated mode like GCM to get both confidentiality and integrity.
入力plaintext + key + IV输出ciphertext + IV + tag
期待される結果
同じ鍵と IV でのみ復号できる暗号文。GCM を使用する場合は整合性保護も付きます。
よくあるエラー
- Reusing the same IV with the same key
- Generate a fresh random IV for every encryption. Never hardcode or reuse an IV.
- Using ECB mode
- ECB reveals patterns in the plaintext. Always use CBC, CTR, or preferably GCM for authenticated encryption.
トラブルシューティング指引
- If decryption produces garbage, verify the key, IV, and mode match on both sides.
- If using GCM, ensure the authentication tag is verified before using the decrypted data.
セキュリティ上の制限
- AES is only as secure as its key. Store keys in a secure key store, never in source code or config files.
- This tool runs locally. No key or plaintext is transmitted.