Security

What is Use of Hard-coded Cryptographic Key (CWE-321)?

Learn how hard-coded cryptographic keys work, see real-world code examples and framework-specific fixes for CWE-321. Ensure your application is secure from...

SP
Shreya Pillai July 29, 2026 5 min read Security
AI-friendly summary

What it is: Use of Hard-coded Cryptographic Key (CWE-321) is a security vulnerability where a product uses an unchangeable cryptographic key embedded directly in the code.

Why it matters: This increases the risk that encrypted data can be decrypted by attackers who gain access to the hard-coded keys. It undermines encryption's effectiveness and exposes sensitive information.

How to fix it: Store cryptographic keys securely using methods like environment variables or secret managers instead of hard-coding them in source code.

TL;DR: Use of Hard-coded Cryptographic Key (CWE-321) is a security risk where unchangeable cryptographic keys are embedded directly into the application’s source code, increasing the likelihood that attackers can decrypt sensitive data.

Field Value
CWE ID CWE-321
OWASP Category A04:2025 - Cryptographic Failures
CAPEC None known
Typical Severity High
Affected Technologies any backend language
Detection Difficulty Moderate
Last Updated 2026-07-29

What is Use of Hard-coded Cryptographic Key?

Use of Hard-coded Cryptographic Key (CWE-321) is a type of cryptographic failure vulnerability that occurs when an application uses a fixed, unchangeable cryptographic key embedded directly in the code. As defined by the MITRE Corporation under CWE-321, and classified by the OWASP Foundation under A04:2025 - Cryptographic Failures.

Quick Summary

Use of Hard-coded Cryptographic Key is a high-severity vulnerability where an application’s source code contains unchangeable cryptographic keys. This significantly increases the risk that attackers can decrypt sensitive data if they gain access to these keys, leading to potential data breaches and compliance issues. Jump to: Overview · How It Works · Business Impact · Attack Scenario · Detection · Fixes

Jump to: Quick Summary · Use of Hard-coded Cryptographic Key Overview · How Use of Hard-coded Cryptographic Key Works · Business Impact of Use of Hard-coded Cryptographic Key · Use of Hard-coded Cryptographic Key Attack Scenario · How to Detect Use of Hard-coded Cryptographic Key · How to Fix Use of Hard-coded Cryptographic Key · Framework-Specific Fixes for Use of Hard-coded Cryptographic Key · How to Ask AI to Check Your Code for Use of Hard-coded Cryptographic Key · Use of Hard-coded Cryptographic Key Best Practices Checklist · Use of Hard-coded Cryptographic Key FAQ · Vulnerabilities Related to Use of Hard-coded Cryptographic Key · References · Scan Your Own Site

Use of Hard-coded Cryptographic Key Overview

What

Use of Hard-coded Cryptographic Key is a vulnerability where an application uses unchangeable cryptographic keys embedded directly in the source code.

Why it matters

This increases the risk that encrypted data can be decrypted by attackers who gain access to these hard-coded keys, undermining encryption’s effectiveness and exposing sensitive information.

Where it occurs

In any backend language or framework where cryptographic keys are used but not properly secured.

Who is affected

Developers and organizations using applications with embedded cryptographic keys.

Who is NOT affected

Applications that use secure methods like environment variables or secret managers to store cryptographic keys.

How Use of Hard-coded Cryptographic Key Works

Root Cause

The root cause is the embedding of unchangeable cryptographic keys directly in source code, making them vulnerable to extraction and misuse by attackers.

Attack Flow

  1. Attacker identifies hard-coded keys in source code or configuration files.
  2. Extracts these keys using reverse engineering techniques.
  3. Uses extracted keys to decrypt sensitive data stored in encrypted format.

    Prerequisites to Exploit

    • Access to the application’s source code or configuration files.
    • Ability to extract and use embedded cryptographic keys.

      Vulnerable Code

      encryption_key = "hard-coded-key"
      

      This snippet demonstrates a hard-coded key directly embedded in Python code, making it vulnerable to extraction.

Secure Code

import os

encryption_key = os.environ.get('ENCRYPTION_KEY')

The secure version retrieves the cryptographic key from an environment variable, ensuring it is not stored within the source code and reducing risk of exposure.

Business Impact of Use of Hard-coded Cryptographic Key

  • Confidentiality: Exposure of sensitive data due to decrypted keys.
  • Integrity: Unauthorized modification of encrypted data using extracted keys.
  • Availability: Potential disruption if critical systems rely on compromised encryption mechanisms.

Business consequences include financial losses from data breaches, regulatory fines for non-compliance, and reputational damage due to loss of customer trust.

Use of Hard-coded Cryptographic Key Attack Scenario

  1. Attacker discovers the application’s source code or configuration files.
  2. Extracts hard-coded cryptographic keys using reverse engineering tools.
  3. Uses extracted keys to decrypt sensitive data stored in encrypted format.
  4. Gains unauthorized access and exfiltrates critical information.

How to Detect Use of Hard-coded Cryptographic Key

Manual Testing

  • Review source code for direct embedding of cryptographic keys.
  • Check configuration files for hard-coded key values.
  • Verify that keys are not stored in version control systems or shared repositories.

    Automated Scanners (SAST / DAST)

    Static analysis tools can identify hard-coded keys within the source code, while dynamic testing may be needed to confirm actual exposure and exploitability.

    PenScan Detection

    PenScan’s ZAP, Nuclei, Wapiti, Nikto, SSLyze, Dalfox, and Nmap engines detect hard-coded cryptographic keys in various contexts.

    False Positive Guidance

    A real finding will involve an unchangeable key embedded directly in code or configuration files. A false positive may occur if a seemingly risky pattern is actually safe due to context not visible to the scanner.

How to Fix Use of Hard-coded Cryptographic Key

  • Store cryptographic keys securely using environment variables, secret managers, or hardware security modules.
  • Implement secure mechanisms for key management and rotation.
  • Ensure that sensitive information is never hard-coded in source code or configuration files.

Framework-Specific Fixes for Use of Hard-coded Cryptographic Key

Python/Django

import os

encryption_key = os.environ.get('ENCRYPTION_KEY')

Use environment variables to securely store cryptographic keys instead of embedding them directly in the codebase.

Java

String encryptionKey = System.getenv("ENCRYPTION_KEY");

Retrieve cryptographic keys from system environment variables or secure key management systems.

How to Ask AI to Check Your Code for Use of Hard-coded Cryptographic Key

Review the following Python code block for potential CWE-321 Use of Hard-coded Cryptographic Key vulnerabilities and rewrite it using environment variable retrieval:

encryption_key = "hard-coded-key"
Copy-paste prompt

Review the following Python code block for potential CWE-321 Use of Hard-coded Cryptographic Key vulnerabilities and rewrite it using environment variable retrieval: [paste code here]

Use of Hard-coded Cryptographic Key Best Practices Checklist

✅ Ensure cryptographic keys are never hard-coded in source code or configuration files. ✅ Store sensitive information securely using environment variables, secret managers, or hardware security modules. ✅ Implement secure mechanisms for key management and rotation.

Use of Hard-coded Cryptographic Key FAQ

How does a hard-coded cryptographic key work?

A hard-coded cryptographic key is a fixed value embedded in the source code, which cannot be changed or updated. This makes it vulnerable to attackers who can reverse-engineer the code and extract the key.

Why is Use of Hard-coded Cryptographic Key considered high severity?

The use of hard-coded keys significantly increases the risk that encrypted data may be recovered by malicious users, leading to unauthorized access and potential data breaches.

Can you provide an example of vulnerable code for CWE-321?

An example is directly embedding a cryptographic key in configuration files or source code without any means to change it later.

How can developers prevent Use of Hard-coded Cryptographic Key?

Developers should avoid hard-coding keys and instead use secure methods like environment variables, secret managers, or hardware security modules to store sensitive information.

What are the business impacts of CWE-321?

Business impacts include financial losses from data breaches, regulatory fines for non-compliance, and reputational damage due to loss of customer trust.

How does PenScan detect Use of Hard-coded Cryptographic Key?

PenScan uses static analysis tools like ZAP and Nuclei to identify hard-coded keys in source code and configuration files.

What is the primary mitigation for Use of Hard-coded Cryptographic Key?

The primary mitigation involves using secure mechanisms such as environment variables or secret managers to store cryptographic keys.

| CWE | Name | Relationship | |—|—|—| | CWE-798 | Use of Hard-coded Credentials (ChildOf) | |

References

Scan Your Own Site

Manual code review catches what you know to look for. An automated scan catches what you didn’t. Scan your own website using PenScan to find Use of Hard-coded Cryptographic Key and other risks before an attacker does.