Java Hardcoded Gcm Nonce
Description
Detects hardcoded GCM initialization vectors/nonces in Java cryptographic implementations. Using static nonces in GCM mode breaks the security model and can lead to cryptographic attacks that compromise data confidentiality and authenticity. GCM requires unique nonces for each encryption operation with the same key to maintain security.
Detection Strategy
• Scans Java source code that imports the javax.crypto.spec package
• Identifies constructor calls to GCMParameterSpec class
• Examines the nonce parameter (typically the second argument) passed to the GCMParameterSpec constructor
• Reports vulnerability when the nonce parameter is determined to be hardcoded or static (not dynamically generated)
• Flags cases where the nonce value can be traced back to a constant definition rather than secure random generation
Vulnerable code example
import javax.crypto.spec.GCMParameterSpec;
public class VulnerableGcmNonce {
private static final String HARDCODED_NONCE = "000000000000";
public void encryptWithHardcodedNonce() {
byte[] nonce = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Hardcoded nonce array
...✅ Secure code example
import javax.crypto.spec.GCMParameterSpec;
import java.security.SecureRandom;
public class SecureGcmNonce {
public void encryptWithRandomNonce() {
// SAFE: Generate fresh random nonce for each encryption
byte[] nonce = new byte[12];...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Continuous Hacking Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' hacking team, fill out this contact form.