| name | cwe-329-missing-random-iv |
| description | Use this skill when you need to remediate CWE-329 (Missing Random IV in CBC Mode) vulnerabilities in Java code. Triggers on SAST findings, security reviews, or when fixing missing random iv in cbc mode issues. |
| version | 1.0.0 |
| license | MIT |
| tags | ["security","java","cwe-329","remediation","sast"] |
CWE-329 Missing Random IV in CBC Mode
Description
Missing Random IV in CBC Mode
Reference:
https://cwe.mitre.org/data/definitions/329.html
OWASP Category: A02:2021 – Cryptographic Failures
Vulnerable Pattern
❌ Example 1: Vulnerable Pattern
private static final byte[] STATIC_IV = "1234567890123456".getBytes();
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(STATIC_IV));
Why it's vulnerable: This pattern is vulnerable to Missing Random IV in CBC Mode
Deterministic Fix
✅ Secure Implementation: Secure Implementation
public byte[] encrypt(byte[] plaintext, SecretKey key) throws Exception {
byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(128, iv));
byte[] ciphertext = cipher.doFinal(plaintext);
byte[] result = new byte[iv.length + ciphertext.length];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(ciphertext, 0, result, iv.length, ciphertext.length);
return result;
}
Why it's secure: Implements proper protection against Missing Random IV in CBC Mode
Detection Pattern
Look for these patterns in your codebase:
grep -rn "IvParameterSpec.*static\|final.*IV" --include="*.java"
Remediation Steps
-
Generate new random IV for each encryption operation
-
Use SecureRandom for IV generation
-
Prefer AES-GCM over AES-CBC for authenticated encryption
-
Store IV with ciphertext (it's not secret)
Key Imports
import java.security.SecureRandom;
import javax.crypto.spec.GCMParameterSpec;
Verification
After remediation:
-
Run SAST scanner to confirm vulnerability is resolved
-
Review all instances of the vulnerable pattern
-
Add unit tests that verify the secure implementation
-
Check for similar patterns in related code
Trigger Examples
Fix CWE-329 vulnerability
Resolve Missing Random IV in CBC Mode issue
Secure this Java code against missing random iv in cbc mode
SAST reports CWE-329
Common Vulnerable Locations
| Controller | *Controller.java | User input handling |
| Service | *Service.java | Business logic |
| Repository | *Repository.java | Data access |
References
Source: Generated by Java CWE Security Skills Generator
Last Updated: 2026-03-07