cwe-321-hardcoded-crypto-key

Replace hard-coded cryptographic keys in Java with KeyGenerator and SecureRandom.

3|1|Updated Mar 6, 2026
One-click install
npx skills add https://github.com/DevelopersCoffee/java-cwe-security-skills --skill cwe-321-hardcoded-crypto-key
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cwe-321-hardcoded-crypto-key
Source: https://github.com/DevelopersCoffee/java-cwe-security-skills/tree/main/cwe-321-hardcoded-crypto-key
Command: npx skills add https://github.com/DevelopersCoffee/java-cwe-security-skills --skill cwe-321-hardcoded-crypto-key

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses the critical security vulnerability of hard-coded cryptographic keys in Java code, preventing unauthorized access to sensitive data.

Core Features & Use Cases

  • Vulnerability Identification: Detects and flags instances of hard-coded encryption keys.
  • Secure Key Management: Provides guidance on generating and managing cryptographic keys securely.
  • Deterministic Remediation: Offers a clear, secure code pattern to replace vulnerable implementations.
  • Use Case: When a SAST tool flags a CWE-321 vulnerability, use this Skill to understand the risk and apply the recommended secure coding practices.

Quick Start

Use the cwe-321-hardcoded-crypto-key skill to remediate hard-coded cryptographic key issues in the provided Java code.

Frequently Asked Questions about cwe-321-hardcoded-crypto-key

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix a hard-coded cryptographic key vulnerability in Java?

To fix a hard-coded cryptographic key vulnerability in Java, replace the insecure string literal with a securely generated key using KeyGenerator and SecureRandom, then externalize key storage. This remediation eliminates unauthorized access risks associated with embedded credentials.

What is CWE-321 and how does it affect Java application security?

CWE-321 is the use of a hard-coded cryptographic key, which creates a critical security vulnerability in Java applications by exposing encryption secrets to unauthorized access. Attackers can extract embedded keys to decrypt sensitive data.

How do I securely generate and manage cryptographic keys in Java?

Securely generate and manage cryptographic keys in Java by using the KeyGenerator class initialized with SecureRandom for entropy. Externalize key storage to a secure, managed solution rather than embedding keys directly in source code.

Can I use this remediation for SAST findings related to hard-coded encryption keys?

Yes, you can use this remediation to address SAST findings related to hard-coded encryption keys. It provides deterministic secure code patterns to replace vulnerable implementations flagged by static application security testing tools.

Why does externalizing cryptographic key storage improve Java security?

Externalizing cryptographic key storage improves Java security by separating sensitive credentials from application source code. This prevents unauthorized users from extracting keys if they gain access to the codebase or compiled binaries.

What are the limitations of replacing hard-coded keys with KeyGenerator?

Replacing hard-coded keys with KeyGenerator requires an external key management infrastructure to persist and retrieve generated keys. You must ensure the externalized storage solution is properly configured and secured to prevent new vulnerabilities.