Cryptography › Module 4 › Lesson 2
Key Management Basics
Generate, store, rotate, and revoke keys without leaving secrets in chat or git
Opening
Brilliant algorithms cannot save a private key pasted into Slack — lifecycle is the control, toy keys only in this course.
Key management is operational security with a calendar: generate with a real CSPRNG, store where access is controlled, use with least privilege, rotate on a schedule and after suspicion, revoke when a name or key is dead. The algorithm lesson is over if the PEM is in a public gist. This topic never asks you to paste a live production key into chat, git, or these notes. Toy keys, lab passphrases you will not reuse, fictional environment variable names — that is the material. You will write a lifecycle map into $HOME/cyberlium-lab. You may generate a toy secret with openssl or Python secrets and throw it away or lock it at mode 600. You will not commit it. You will not put it in a screenshot you upload. Next is the encrypt-and-verify lab: hash a sample file, AES-encrypt with a lab passphrase, decrypt, confirm SHA-256 matches.
1. Generate: OS/crypto RNG, enough length, separate keys per job — not a website of “random” dice photos
Generation means a cryptographic random number generator: /dev/urandom, Windows CNG, Python secrets, openssl rand. Not time.time(), not math.random, not four words from a meme, not a screenshot of a dice site. Length depends on use: AES-256 wants 256 bits of key material; a password that will feed a KDF wants a long high-entropy passphrase stored in a manager, not reused from email. Symmetric keys and asymmetric key pairs are different objects: AES needs a shared secret stored like a password; RSA/ECDSA needs a private key that never leaves a controlled boundary and a public key you can publish. Separate keys per environment and service. The staging AES key is not the production AES key. The CI token is not the laptop admin password. Least privilege is easier when a leak of one toy-sized lab key cannot open the whole company — and in this course, a leak of a lab passphrase must not open your real accounts, which is why you will not reuse it.
2. Store, rotate, revoke: vaults and managers, not README, not group chat, not email-myself-the-PEM
Production stores: hardware security modules (HSM), cloud KMS, secret managers with IAM, OS keychains. Humans: a password manager. Local scripts: environment variables or a chmod 600 gitignored file — never the source tree you push. Public GitHub is not a vault. Slack is not a vault. A world-readable USB stick is not a vault. Encrypted offline backups of private keys beat “email myself the PEM.” Lost keys lock you out; leaked keys lock attackers in. Plan both. Rotation is changing the key before it is famous. Revocation is telling the world a key or certificate is dead (PKI you already met: CAs revoke; for API keys you disable the credential in the control plane). The plan must exist before the breach. If a live key ever lands in git, treat it as burned: rotate, purge history if you have a real incident process, never “it was only five minutes.” This course uses toy keys so you can practice the habit without burning a real tenant.
Lifecycle rooms — generate, store, privilege, rotate/revoke. Live secrets never enter the notes.
Generate
CSPRNG (openssl rand, secrets). Enough bits. New key per service/environment. Not meme phrases.
Store
KMS/HSM/secret manager in production; password manager for humans; env or chmod 600 gitignored file locally.
Least privilege
Each service gets only the keys it needs. One global master key in every container is a blast radius.
Rotate & revoke
Schedule plus emergency. Compromised = burned. Certificates: renew before Not After; revoke on leak.
3. Wrong vs right: live keys in git/chat vs toy lifecycle notes and env-from-outside-the-repo
Worked failure — treating Slack as a KMS. Right is a map plus a toy secret that never becomes a screenshot.
Wrong
Paste a production private key or API token into README, Discord, or these notes. Commit .pem files. Email yourself unencrypted keys. Reuse the upcoming lab passphrase as your bank password. Generate keys from the clock. One master key copied into every container. Print keys on badges. Scan other people’s repos for keys as “practice.”
Right
Write generate → store → use → rotate → revoke. Show os.environ (or equivalent) as the pattern — the value is set outside the repo. Toy/fictional names only. chmod 600 key-mgmt-notes.txt. Next: Lab — Encrypt & Verify a File with a lab passphrase you will not reuse.
4. Practical: pattern in code (no live secret), notes locked, optional toy rand you do not commit
The Python snippet reads APP_MASTER_KEY from the environment. It does not embed a literal. For the lab, you may set a toy value in your shell for ten seconds, run a length check, then unset it — or skip execution and still write the pattern. openssl rand -hex 32 produces toy material; if you run it, do not paste the output into chat or git. The notes describe the lifecycle and the ethics line. They do not contain the hex.
Command guide
key-mgmt-notes.txt plus env pattern — toy only, never a live key in git or chat
DEFENSIVE key lifecycle. TOY secrets only. NEVER paste a live production key into this file, git, or chat.
Command — copy this
mkdir -p "$HOME/cyberlium-lab" NOTES="$HOME/cyberlium-lab/key-mgmt-notes.txt"
Command — copy this
{
echo "=== KEY MANAGEMENT LIFECYCLE ==="
echo "generate: CSPRNG (openssl rand / Python secrets) — not clock, not memes"
echo "store: KMS/HSM/secret manager (prod); password manager (humans); env or chmod 600 gitignored (local)"
echo "use: least privilege — one key per service/environment"
echo "rotate: on schedule and after suspicion"
echo "revoke: disable burned credentials; renew certs before Not After"
echo ""
echo "code pattern: os.environ['APP_MASTER_KEY'] set OUTSIDE the repo — no literals"
echo "ethics: never live keys in git/chat/screenshots; toy keys only; do not reuse lab passphrases on real accounts"
} > "$NOTES"Pattern only — do not put a real secret in the script.
Command — copy this
cat > "$HOME/cyberlium-lab/read_toy_key.py" << 'PY'
import os
key = os.environ.get("APP_MASTER_KEY")
if not key or len(key) < 32:
raise SystemExit("fail closed: set a TOY APP_MASTER_KEY (>=32 chars) outside git, or skip running")
print("toy key length ok — not printing the secret")
PYOptional toy material — do NOT copy the output into NOTES, git, or chat.
Optional command
openssl rand -hex 32 export APP_MASTER_KEY='toy-only-not-a-real-secret-xxxx' # local shell, then unset
Command — copy this
chmod 600 "$NOTES" "$HOME/cyberlium-lab/read_toy_key.py"
Windows without chmod: WSL/Git Bash, or restrict the files in your profile.
NEVER: git add *.pem *.key or a file that contains a live token NEVER: paste production secrets into Slack/Discord/email NEVER: reuse the next lab's passphrase as an email or bank password
Mission: key-mgmt-notes.txt lifecycle map (mode 600), no live secrets
1) Write generate (CSPRNG), store (vault/manager/env), least privilege, rotate, and revoke in your own words. 2) Record the pattern: secrets come from the environment or a chmod 600 gitignored file — never source, never chat. 3) Save as $HOME/cyberlium-lab/key-mgmt-notes.txt and chmod 600. Toy keys only. Do not paste live keys. Do not commit PEMs.
Stuck? Ask Cyberlium AI Mentor
If “the README is the easiest vault” still feels true, ask for a hint — not a place to paste a real PEM. Try: "Hint only: how should I generate and store keys, why fail closed if os.environ is missing, and why must live keys never enter git or this chat?" You still fill key-mgmt-notes.txt. Toy only. No production secrets. No scanning other people’s repos.
You now treat keys as a lifecycle, not as a string that lives next to the code. Fail closed if the environment is empty. Rotate before the breach drill is real. Next — Lab — Encrypt & Verify a File — you will SHA-256 a toy file in cyberlium-lab, encrypt with OpenSSL AES using a lab passphrase you will not reuse, decrypt, and confirm the digest matches. chmod 600 everything that might hold that passphrase. Then the topic final quiz, and a honest tease of Topic 9 Ethical Hacking (CEH v13).
Knowledge Check
APPLY: Where do production API/crypto secrets belong, and what is forbidden in this course’s notes?
Multiple choice
Knowledge Check
APPLY: True or False: Key rotation planning should exist before a breach, and that plan includes pasting the current production private key into Discord so the team can rotate faster.
True or False
Knowledge Check
APPLY: Least privilege for keys means what, and how does that interact with the encrypt lab’s passphrase?
Multiple choice