Secret Key Generator

Generate cryptographically secure random secret keys for encryption and authentication.

Key Length Guide

128-bit: AES-128, suitable for most applications

192-bit: AES-192, enhanced security

256-bit: AES-256, maximum AES security, recommended

384-bit: Extended keys for specialized uses

512-bit: Maximum entropy for key derivation

What Is a Secret Key?

A secret key is a random string of bits used as input to a cryptographic algorithm to encrypt data, authenticate messages, or sign tokens. Unlike passwords chosen by humans, cryptographic secret keys are generated entirely from a source of randomness and are never meant to be memorized — they are stored in environment variables, key management systems, or secure vaults.

The strength of any symmetric encryption scheme — AES, ChaCha20, HMAC — depends entirely on two things: the algorithm's mathematical design and the quality of the key. A 256-bit AES key derived from a weak pseudorandom number generator can collapse the security of an otherwise sound algorithm. This is why professional secret key generators rely on cryptographically secure pseudorandom number generators (CSPRNGs), which produce output that is computationally indistinguishable from true randomness.

Modern web browsers expose a CSPRNG through the crypto.getRandomValues() API, which draws entropy from the operating system's kernel entropy pool (e.g., /dev/urandom on Linux or CryptGenRandom on Windows). This secret key generator uses exactly that API, meaning every key you produce carries genuine cryptographic-grade randomness and is safe for use in production systems.

Common use cases include: generating AES encryption keys for database field encryption, creating signing keys for JSON Web Tokens (JWTs), seeding HMAC algorithms for API request authentication, producing session secrets for web frameworks like Express or Django, and deriving keys for end-to-end encrypted messaging protocols.

Key Length, Bits, and Entropy

Entropy measures the unpredictability of a secret key. A key containing N bytes holds exactly N × 8 bits of entropy when each byte is drawn uniformly at random. The generator stores this directly on each key object as key.bits = keyLength * 8.

The relationship between byte count and security level is well-established by standards bodies like NIST. A 128-bit key (16 bytes) provides adequate security against brute-force attack for the foreseeable future; a 256-bit key (32 bytes) is recommended for long-lived data and post-quantum margin; a 512-bit key (64 bytes) delivers extreme entropy headroom for key derivation functions.

The output character length depends on the encoding format chosen, not just the byte count. The following table summarizes the output size for each supported format at common key lengths:

Key Length Bits Hex Chars Base64 Chars Base32 Chars
16 bytes128322425
24 bytes192483238
32 bytes256644451
48 bytes384966476
64 bytes51212888102

Choosing a longer key never weakens security — it only increases entropy. The trade-off is purely practical: very long keys stored in environment variables or config files take more space, and some protocols define a maximum key length.

Output Character Length by Format

bits = N × 8 | hex_len = N × 2 | base64_len = ⌈N / 3⌉ × 4 | base32_len = ⌊(N × 8) / 5⌋ | binary_len = 9N − 1

Where:

  • N= Key length in bytes (selected preset: 16, 24, 32, 48, or 64)
  • bits= Total entropy in bits; equal to N × 8 (stored as key.bits in the generator)
  • hex_len= Output characters in Hex format: 2 lowercase hex digits per byte
  • base64_len= Output characters in Base64 format: 4 chars per 3 bytes, padded to a multiple of 4
  • base32_len= Output characters in Base32 format: 5 bits mapped to one of 32 chars (A–Z, 2–7)
  • binary_len= Output characters in Binary format: 8-char groups separated by spaces (9N − 1 total)

Output Formats: Hex, Base64, Base32, Binary

The generator supports four encoding formats, each suited to different environments and protocols. The raw entropy is identical for every format at the same byte length — only the text representation changes.

Hexadecimal (Hex)

Hex is the most common format for cryptographic keys. Each byte is represented as two lowercase hexadecimal digits (0–9, a–f). A 32-byte key produces a 64-character string. Hex keys are easy to paste into config files, contain only URL-safe characters, and are widely accepted by databases, environment variable parsers, and cryptographic libraries. The generator maps each byte b as b.toString(16).padStart(2, '0').

Base64

Base64 encodes 3 bytes into 4 printable ASCII characters, producing a denser representation than hex. The generator uses the browser's native btoa() function on the raw byte string, yielding standard Base64 with +, /, and = padding. Base64 keys are common in JWT secrets, HTTP Authorization headers, and PEM-formatted certificates. A 32-byte key encodes to 44 Base64 characters (including one padding =).

Base32

Base32 maps every 5 bits to one of 32 characters drawn from the alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 (RFC 4648). This encoding is case-insensitive and avoids visually ambiguous characters, making it ideal for human-transcribed codes such as TOTP authenticator seeds (Google Authenticator, Authy). The generator reads all bits of the byte array and chunks them into groups of 5, yielding ⌊(N × 8) / 5⌋ characters.

Binary

Binary displays each byte as its 8-bit binary representation, separated by spaces. This format is used primarily for educational purposes — visualizing the raw bit pattern — and for low-level protocol debugging where individual bit positions matter. A 16-byte key produces 16 space-separated 8-bit groups (143 characters total).

Choosing the Right Key Length for Your Use Case

Selecting an appropriate key length depends on the algorithm, the sensitivity of the data, and the expected lifetime of the key. Here is a practical guide aligned with current NIST and industry recommendations.

128-bit (16 bytes) — AES-128, HMAC-SHA256 seeds

AES-128 is approved by NIST for protecting SECRET-level classified information and provides roughly 2128 possible keys — far beyond any brute-force attack with current or near-future hardware. Use 128-bit keys for short-lived session tokens, API rate-limiting secrets, and CSRF tokens where key rotation happens frequently.

256-bit (32 bytes) — AES-256, JWT signing, database encryption

The 256-bit length is the most widely recommended default. It is required for TOP SECRET data under NSA Suite B, and is the standard for JWT HS256/HS384 signing secrets, Django SECRET_KEY, Node.js express-session secrets, and field-level database encryption keys. This generator defaults to 32 bytes for this reason.

384-bit and 512-bit — Key derivation inputs, master secrets

Extended key lengths are useful as inputs to key derivation functions (KDFs) like HKDF or PBKDF2, where the master secret seeds multiple derived sub-keys. A 512-bit master secret ensures that even after deriving dozens of sub-keys the entropy pool is not exhausted. They are also appropriate for HMAC-SHA512 where the block size matches 512 bits.

In all cases, key length alone does not guarantee security — proper key storage, rotation policies, and access control are equally important. Never commit secret keys to source control; always load them from environment variables or a dedicated secrets manager such as AWS Secrets Manager or HashiCorp Vault.

Security Best Practices for Secret Keys

Generating a strong random key is only the first step. How you store, transmit, rotate, and retire that key determines the overall security posture of your application. Follow these established practices to protect generated keys throughout their lifecycle.

  • Use environment variables: Store secret keys in environment variables (.env files loaded at runtime), never hard-coded in source files. Add .env to .gitignore immediately.
  • Prefer secrets managers: For production workloads, use a dedicated secrets management service. These tools provide audit logs, automatic rotation, and fine-grained access policies.
  • Rotate keys regularly: Even a strong key becomes a liability if it is exposed through a breach. Implement a key rotation schedule and ensure your application can accept new keys without downtime.
  • Limit key scope: Generate a separate key for each service, environment (dev/staging/prod), and purpose (signing vs. encryption). Key reuse amplifies the blast radius of a single compromise.
  • Never log keys: Structured logging pipelines often capture request context. Sanitize any variable that might contain a secret before logging, and audit log aggregators for accidental key leakage.
  • Verify randomness source: This generator uses crypto.getRandomValues(), which is a CSPRNG. Avoid third-party generators that rely on Math.random() — that function is not cryptographically secure and must never be used for security-sensitive values.

For applications governed by compliance frameworks such as PCI-DSS, HIPAA, or SOC 2, consult your security officer about key management requirements. Many frameworks mandate hardware security modules (HSMs) for storing encryption keys at rest.

Worked Examples

256-bit AES Key in Hex (Default)

Problem:

Generate a 256-bit secret key in Hex format for use as an AES-256 encryption key or a JWT signing secret.

Solution Steps:

  1. 1Select key length: 256-bit preset → N = 32 bytes.
  2. 2Select format: HEX.
  3. 3The generator calls crypto.getRandomValues(new Uint8Array(32)), filling 32 slots with cryptographically random bytes.
  4. 4Each byte b is encoded as b.toString(16).padStart(2, '0') — two lowercase hex characters.
  5. 5The 32 two-character strings are joined: output length = 32 × 2 = 64 hex characters.
  6. 6Entropy stored: key.bits = 32 × 8 = 256 bits.

Result:

A 64-character hex string such as a3f8c2d1e9b04756…fd23 — 256 bits of cryptographic entropy, suitable for AES-256, HMAC-SHA256, and JWT HS256 secrets.

128-bit TOTP Seed in Base32

Problem:

Generate a 128-bit authenticator seed in Base32 format compatible with Google Authenticator and RFC 6238 TOTP.

Solution Steps:

  1. 1Select key length: 128-bit preset → N = 16 bytes.
  2. 2Select format: BASE32.
  3. 3The generator calls crypto.getRandomValues(new Uint8Array(16)) for 16 random bytes.
  4. 4All bytes are converted to a binary string: 16 × 8 = 128 bits total.
  5. 5The binary string is chunked into groups of 5 bits; each group indexes into 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.
  6. 6Output character count = ⌊128 / 5⌋ = 25 characters.

Result:

A 25-character uppercase Base32 string such as JBSWY3DPEHPK3PXP25TYA — ready to enter into any RFC 6238 TOTP authenticator app as a secret seed.

512-bit Master Secret in Base64

Problem:

Generate a 512-bit master secret in Base64 for use as a KDF input or HMAC-SHA512 key.

Solution Steps:

  1. 1Select key length: 512-bit preset → N = 64 bytes.
  2. 2Select format: BASE64.
  3. 3The generator calls crypto.getRandomValues(new Uint8Array(64)) to fill 64 bytes with random data.
  4. 4btoa(String.fromCharCode(...bytes)) is called, encoding the raw byte sequence as standard Base64.
  5. 5Base64 output length = ⌈64 / 3⌉ × 4 = 22 × 4 = 88 characters (including padding '=' characters).
  6. 6Entropy: key.bits = 64 × 8 = 512 bits.

Result:

An 88-character Base64 string carrying 512 bits of entropy — ideal as a master key for HKDF key derivation or as an HMAC-SHA512 secret that maximally fills the 512-bit block size.

Generating 5 Keys at Once for Key Rotation

Problem:

Pre-generate 5 independent 256-bit hex keys to support a rolling key rotation scheme with overlap.

Solution Steps:

  1. 1Select key length: 256-bit preset → N = 32 bytes.
  2. 2Select format: HEX.
  3. 3Set the quantity slider to 5.
  4. 4Clicking Generate calls crypto.getRandomValues independently for each of the 5 iterations — producing 5 statistically independent random byte arrays.
  5. 5Each array is hex-encoded: 5 × 64-character strings, each with 256 bits of entropy.
  6. 6Store key[0] as the current active key, key[1] as the pending next key, and keys[2–4] as reserve rotation keys.

Result:

5 independent 64-character hex keys. Because each uses a fresh call to the CSPRNG, knowledge of one key provides zero information about the others.

Tips & Best Practices

  • Always use crypto.getRandomValues() or an equivalent OS-level CSPRNG — never Math.random() — for any security-sensitive key generation.
  • 256-bit (32 bytes) in Hex is the safest default: it covers AES-256, HMAC-SHA256, and JWT HS256 without over-engineering.
  • For TOTP two-factor authentication seeds, always choose Base32 format — authenticator apps expect RFC 4648 Base32 encoding.
  • Generate one key per purpose: separate signing keys from encryption keys, and separate production keys from staging keys.
  • Rotate secret keys on a schedule and after any suspected exposure; use the quantity slider to pre-generate rotation batches.
  • After copying a generated key, immediately paste it into your secrets manager or .env file — never leave it in the clipboard longer than necessary.
  • For JWT secrets, prefer 256-bit or longer keys to match the hash output size of HS256/HS512 and avoid length-extension attack surface.
  • Store the bit length alongside any archived key — knowing whether a key is 128-bit or 256-bit is important context for auditing old encrypted data.

Frequently Asked Questions

Yes, as long as the generator uses the Web Crypto API's <code>crypto.getRandomValues()</code>. This function draws entropy from the operating system's CSPRNG, which is the same source used by server-side tools like OpenSSL. This generator uses exactly that API. The keys are generated entirely client-side — they are never sent to any server — so there is no network interception risk.
A password is typically a human-chosen string of moderate length meant to be memorized. A secret key is a machine-generated sequence of random bytes with no semantic meaning, designed to maximize entropy for a given bit length. Because secret keys are never typed by humans, they can be much longer and more random than passwords, and they should never be used as passwords or stored in password managers.
Choose Hex for most server-side secrets (database encryption keys, session secrets, HMAC keys) because it is universally supported, URL-safe, and easy to read in logs. Choose Base64 when the target system expects Base64 encoding, such as JWT libraries or PEM-formatted keys. Choose Base32 specifically for TOTP two-factor authentication seeds, since authenticator apps (Google Authenticator, Authy) require RFC 4648 Base32 encoding. Use Binary only for educational or debugging purposes.
For general application secrets (JWT signing, session keys, API secrets), 256-bit (32 bytes) is the recommended default — it aligns with AES-256 and HMAC-SHA256 requirements. Use 128-bit (16 bytes) for shorter-lived tokens where key rotation is frequent. Use 384-bit or 512-bit keys only for specific use cases such as inputs to key derivation functions or HMAC-SHA512 secrets where the larger block size benefits from a full-length key.
Yes. AES accepts 128-bit (16-byte), 192-bit (24-byte), or 256-bit (32-byte) keys. The generator's presets map exactly to these sizes. If you choose Hex format, most cryptographic libraries (Node.js <code>crypto</code>, Python <code>cryptography</code>, Java JCE) accept hex-encoded keys and convert them internally. If your library requires raw bytes, decode the hex string before passing it to the cipher initialization function.
Each click triggers a fresh call to <code>crypto.getRandomValues()</code>, which seeds a new sequence from the operating system's entropy pool. The CSPRNG is designed so that successive outputs are statistically independent — knowing all previously generated keys provides no information about future ones. This is a fundamental property of a cryptographically secure random number generator, and it means you can safely generate many keys without worrying about repetition.
Never store secret keys directly in source code — doing so exposes them to anyone with read access to the repository, including all commit history. Always load keys from environment variables at runtime. For local development, use a <code>.env</code> file excluded from version control via <code>.gitignore</code>. For production, use a dedicated secrets manager (AWS Secrets Manager, GCP Secret Manager, HashiCorp Vault) that provides access control, audit logging, and automatic rotation.

Sources & References

Last updated: 2026-06-05

💡

Help us improve!

How would you rate the Secret Key Generator?

<>

Editorial Note

MyCalcBuddy Editorial Team

This page is maintained as an educational calculator reference.

Source

Formula Source: Standard Mathematical References

by Various

UpdatedLast reviewed: May 2026
CheckedFormula checks are based on standard references and internal QA review.