Security anti-pattern for weak encryption (CWE-326, CWE-327). Use when generating or reviewing code that encrypts data, handles encryption keys, or uses cryptographic modes. Detects DES, ECB mode, static IVs, and custom crypto implementations.
Applications use outdated algorithms (DES, RC4), insecure modes (ECB), or mismanage IVs/nonces (static, reused), enabling easy decryption. AI models suggest these weak practices from older tutorials, leading to data breaches and compliance failures.
The Anti-Pattern
The anti-pattern involves using cryptographic techniques that are no longer considered secure for protecting sensitive data.
1. Outdated or Broken Algorithms
Using algorithms like DES, 3DES, or RC4 is a critical flaw. These algorithms have known vulnerabilities and are easily broken with modern computing power.
BAD Code Example
# VULNERABLE: Using the outdated DES algorithm.
from Crypto.Cipher import DES
from Crypto import Random
key = Random.get_random_bytes(8) # DES uses an 8-byte (64-bit) key, but only 56 bits are effective.
def encrypt_data_des(plaintext):
cipher = DES.new(key, DES.MODE_ECB) # ECB mode is also insecure.
# Pad the plaintext to be a multiple of 8 bytes (DES block size).
padded_plaintext = plaintext + (8 - len(plaintext) % 8) * chr(8 - len(plaintext) % 8)
ciphertext = cipher.encrypt(padded_plaintext.encode('utf-8'))
return ciphertext
# DES can be brute-forced in under 24 hours with commodity hardware.
2. Insecure Modes of Operation (e.g., ECB)
Even if using a strong algorithm like AES, using it in Electronic Codebook (ECB) mode is highly insecure. ECB encrypts identical blocks of plaintext into identical blocks of ciphertext, revealing patterns in the data.
BAD Code Example
# VULNERABLE: Using AES in ECB mode.
from Crypto.Cipher import AES
from Crypto import Random
key = Random.get_random_bytes(16) # AES-128 key.
def encrypt_data_ecb(plaintext):
cipher = AES.new(key, AES.MODE_ECB)
# Pad the plaintext to be a multiple of 16 bytes (AES block size).
padded_plaintext = plaintext + (16 - len(plaintext) % 16) * chr(16 - len(plaintext) % 16)
ciphertext = cipher.encrypt(padded_plaintext.encode('utf-8'))
return ciphertext
# If you encrypt an image with many identical color blocks using AES-ECB,
# the encrypted image will still show the original image's outline and patterns.
# This leaks significant information about the plaintext.
GOOD Code Example
# SECURE: Use a modern, authenticated encryption mode like AES-256-GCM.
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.exceptions import InvalidTag
import os
# Generate a strong, random key. AES-256 uses a 32-byte key.
key = AESGCM.generate_key(bit_length=256)
def encrypt_data_gcm(plaintext):
aesgcm = AESGCM(key)
# GCM requires a unique, unpredictable nonce (Initialization Vector).
# It must never be reused with the same key. A 12-byte nonce is standard.
nonce = os.urandom(12)
# AES-GCM performs both encryption and provides an authentication tag (integrity check).
ciphertext = aesgcm.encrypt(nonce, plaintext.encode('utf-8'), None)
# Store and transmit the nonce along with the ciphertext.
return nonce + ciphertext
def decrypt_data_gcm(encrypted_data_with_nonce):
aesgcm = AESGCM(key)
nonce = encrypted_data_with_nonce[:12]
ciphertext = encrypted_data_with_nonce[12:]
try:
# The decrypt method will also verify the authentication tag.
# If the data is tampered with, it will raise an `InvalidTag` exception.
plaintext = aesgcm.decrypt(nonce, ciphertext, None).decode('utf-8')
return plaintext
except InvalidTag:
raise ValueError("Decryption failed: data may have been tampered with or corrupted.")
# AES-256-GCM provides strong confidentiality, integrity, and authenticity.
# Each encryption is unique due to the nonce, preventing pattern leakage.
Detection
Code review for algorithm choice: Search your codebase for calls to cryptographic functions using DES, 3DES, RC4, MD5 (for encryption), or SHA-1 (for encryption/signatures).
Check modes of operation: Look for ECB mode being used with block ciphers like AES.
Inspect IV/Nonce generation: Verify how initialization vectors (IVs) or nonces are generated. Are they random and unique for each encryption? Avoid static, predictable, or reused IVs.
Custom crypto implementations: Be extremely wary of any "homegrown" encryption algorithms. These are almost always insecure.
Prevention
Use strong, modern algorithms: For symmetric encryption, always use AES-256. For authenticated encryption, prefer AES-256-GCM or ChaCha20-Poly1305.
Avoid insecure modes of operation: Never use ECB mode. If using CBC mode, always pair it with a strong MAC (Message Authentication Code) in an Encrypt-then-MAC scheme. Better yet, use AEAD modes like GCM.
Generate random, unique IVs/Nonces: Generate a unique, unpredictable IV (CBC) or nonce (GCM) for every encryption using a cryptographically secure random number generator. Never reuse a nonce with the same key in GCM (enables catastrophic key recovery).
Use established cryptographic libraries: Never "roll your own" encryption. Use well-vetted, standard libraries (e.g., cryptography in Python, javax.crypto in Java).
Ensure key strength: Use sufficiently long keys (e.g., 256 bits for AES).
1---2name: weak-encryption-anti-pattern3description: Security anti-pattern for weak encryption (CWE-326, CWE-327). Use when generating or reviewing code that encrypts data, handles encryption keys, or uses cryptographic modes. Detects DES, ECB mode, static IVs, and custom crypto implementations.4---56# Weak Encryption Anti-Pattern78**Severity:** High910## Summary1112Applications use outdated algorithms (DES, RC4), insecure modes (ECB), or mismanage IVs/nonces (static, reused), enabling easy decryption. AI models suggest these weak practices from older tutorials, leading to data breaches and compliance failures.1314## The Anti-Pattern1516The anti-pattern involves using cryptographic techniques that are no longer considered secure for protecting sensitive data.1718### 1. Outdated or Broken Algorithms1920Using algorithms like DES, 3DES, or RC4 is a critical flaw. These algorithms have known vulnerabilities and are easily broken with modern computing power.2122#### BAD Code Example2324```python25# VULNERABLE: Using the outdated DES algorithm.26from Crypto.Cipher import DES27from Crypto import Random2829key = Random.get_random_bytes(8) # DES uses an 8-byte (64-bit) key, but only 56 bits are effective.3031def encrypt_data_des(plaintext):32 cipher = DES.new(key, DES.MODE_ECB) # ECB mode is also insecure.33 # Pad the plaintext to be a multiple of 8 bytes (DES block size).34 padded_plaintext = plaintext + (8 - len(plaintext) % 8) * chr(8 - len(plaintext) % 8)35 ciphertext = cipher.encrypt(padded_plaintext.encode('utf-8'))36 return ciphertext3738# DES can be brute-forced in under 24 hours with commodity hardware.39```4041### 2. Insecure Modes of Operation (e.g., ECB)4243Even if using a strong algorithm like AES, using it in Electronic Codebook (ECB) mode is highly insecure. ECB encrypts identical blocks of plaintext into identical blocks of ciphertext, revealing patterns in the data.4445#### BAD Code Example4647```python48# VULNERABLE: Using AES in ECB mode.49from Crypto.Cipher import AES50from Crypto import Random5152key = Random.get_random_bytes(16) # AES-128 key.5354def encrypt_data_ecb(plaintext):55 cipher = AES.new(key, AES.MODE_ECB)56 # Pad the plaintext to be a multiple of 16 bytes (AES block size).57 padded_plaintext = plaintext + (16 - len(plaintext) % 16) * chr(16 - len(plaintext) % 16)58 ciphertext = cipher.encrypt(padded_plaintext.encode('utf-8'))59 return ciphertext6061# If you encrypt an image with many identical color blocks using AES-ECB,62# the encrypted image will still show the original image's outline and patterns.63# This leaks significant information about the plaintext.64```6566### GOOD Code Example6768```python69# SECURE: Use a modern, authenticated encryption mode like AES-256-GCM.70from cryptography.hazmat.primitives.ciphers.aead import AESGCM71from cryptography.exceptions import InvalidTag72import os7374# Generate a strong, random key. AES-256 uses a 32-byte key.75key = AESGCM.generate_key(bit_length=256)7677def encrypt_data_gcm(plaintext):78 aesgcm = AESGCM(key)79 # GCM requires a unique, unpredictable nonce (Initialization Vector).80 # It must never be reused with the same key. A 12-byte nonce is standard.81 nonce = os.urandom(12)8283 # AES-GCM performs both encryption and provides an authentication tag (integrity check).84 ciphertext = aesgcm.encrypt(nonce, plaintext.encode('utf-8'), None)8586 # Store and transmit the nonce along with the ciphertext.87 return nonce + ciphertext8889def decrypt_data_gcm(encrypted_data_with_nonce):90 aesgcm = AESGCM(key)91 nonce = encrypted_data_with_nonce[:12]92 ciphertext = encrypted_data_with_nonce[12:]9394 try:95 # The decrypt method will also verify the authentication tag.96 # If the data is tampered with, it will raise an `InvalidTag` exception.97 plaintext = aesgcm.decrypt(nonce, ciphertext, None).decode('utf-8')98 return plaintext99 except InvalidTag:100 raise ValueError("Decryption failed: data may have been tampered with or corrupted.")101102# AES-256-GCM provides strong confidentiality, integrity, and authenticity.103# Each encryption is unique due to the nonce, preventing pattern leakage.104```105106## Detection107108- **Code review for algorithm choice:** Search your codebase for calls to cryptographic functions using `DES`, `3DES`, `RC4`, `MD5` (for encryption), or `SHA-1` (for encryption/signatures).109- **Check modes of operation:** Look for `ECB` mode being used with block ciphers like AES.110- **Inspect IV/Nonce generation:** Verify how initialization vectors (IVs) or nonces are generated. Are they random and unique for each encryption? Avoid static, predictable, or reused IVs.111- **Custom crypto implementations:** Be extremely wary of any "homegrown" encryption algorithms. These are almost always insecure.112113## Prevention114115- [ ] **Use strong, modern algorithms:** For symmetric encryption, always use **AES-256**. For authenticated encryption, prefer **AES-256-GCM** or **ChaCha20-Poly1305**.116- [ ] **Avoid insecure modes of operation:** Never use ECB mode. If using CBC mode, always pair it with a strong MAC (Message Authentication Code) in an Encrypt-then-MAC scheme. Better yet, use AEAD modes like GCM.117- [ ] **Generate random, unique IVs/Nonces:** Generate a unique, unpredictable IV (CBC) or nonce (GCM) for every encryption using a cryptographically secure random number generator. Never reuse a nonce with the same key in GCM (enables catastrophic key recovery).118- [ ] **Use established cryptographic libraries:** Never "roll your own" encryption. Use well-vetted, standard libraries (e.g., `cryptography` in Python, `javax.crypto` in Java).119- [ ] **Ensure key strength:** Use sufficiently long keys (e.g., 256 bits for AES).120121## Related Security Patterns & Anti-Patterns122123- [Hardcoded Secrets Anti-Pattern](../hardcoded-secrets/): Encryption keys are critical secrets that must be managed securely.124- [Insufficient Randomness Anti-Pattern](../insufficient-randomness/): The randomness used for IVs/nonces must be cryptographically secure.125- [Padding Oracle Anti-Pattern](../padding-oracle/): A specific attack against block ciphers used in CBC mode without proper authentication.126127## References128129- [OWASP Top 10 A04:2025 - Cryptographic Failures](https://owasp.org/Top10/2025/A04_2025-Cryptographic_Failures/)130- [OWASP GenAI LLM10:2025 - Unbounded Consumption](https://genai.owasp.org/llmrisk/llm10-unbounded-consumption/)131- [OWASP API Security API8:2023 - Security Misconfiguration](https://owasp.org/API-Security/editions/2023/en/0xa8-security-misconfiguration/)132- [CWE-326: Inadequate Encryption Strength](https://cwe.mitre.org/data/definitions/326.html)133- [CWE-327: Use of a Broken or Risky Cryptographic Algorithm](https://cwe.mitre.org/data/definitions/327.html)134- [CAPEC-97: Cryptanalysis](https://capec.mitre.org/data/definitions/97.html)135- [BlueKrypt - Cryptographic Key Length Recommendation](https://www.keylength.com/)136- Source: [sec-context](https://github.com/Arcanum-Sec/sec-context)
Run npx skillmds@latest add igbuend/weak-encryption-anti-pattern in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Security anti-pattern for weak encryption (CWE-326, CWE-327). Use when generating or reviewing code that encrypts data, handles encryption keys, or uses cryptographic modes. Detects DES, ECB mode, static IVs, and custom crypto implementations. It is listed under Security on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
igbuend (@igbuend) published this skill. Their other Agent Skills are listed on their SkillMD profile.