Skip to main content

Overview

Keystore encryption transforms a raw private key into a password-protected JSON structure. The process uses key derivation functions (KDF) and symmetric encryption to ensure the private key cannot be recovered without the correct password.

Encryption Process

Algorithm Flow

Step by Step

  1. Generate random salt (32 bytes) and IV (16 bytes)
  2. Derive key from password using KDF (scrypt or PBKDF2)
  3. Split derived key: first 16 bytes for encryption, next 16 for MAC
  4. Encrypt private key with AES-128-CTR using encryption key and IV
  5. Compute MAC as keccak256(macKey || ciphertext)
  6. Assemble keystore JSON structure

Basic Encryption

Output:

Encryption Options

KDF Selection

Pros:
  • Memory-hard (expensive to parallelize)
  • GPU/ASIC resistant
  • Higher security against brute-force
Cons:
  • Slower (~2-5 seconds default)
  • Higher memory usage

Scrypt Parameters

Memory formula: 128 * N * r * p bytes Default: 128 * 262144 * 8 * 1 = 256 MB

PBKDF2 Parameters

Custom Salt and IV

Only provide custom salt/IV for testing or specific compliance requirements. Random generation (default) is recommended for security.

Custom UUID

Performance Tuning

Fast Encryption (Testing/Development)

Balanced (Mobile/Web)

Maximum Security (Cold Storage)

Error Handling

Advanced Usage

Deterministic Encryption (Testing)

Batch Encryption

Progress Indication

Since encryption can take several seconds with default parameters:

Encryption Components Explained

Salt

  • Purpose: Ensures different derived keys for same password
  • Size: 32 bytes (256 bits)
  • Generation: crypto.getRandomValues()
  • Storage: Stored in kdfparams.salt (hex-encoded)

IV (Initialization Vector)

  • Purpose: Ensures different ciphertexts for same key
  • Size: 16 bytes (128 bits)
  • Generation: crypto.getRandomValues()
  • Storage: Stored in cipherparams.iv (hex-encoded)

Derived Key

  • Purpose: Convert password to fixed-length encryption key
  • Size: 32 bytes (256 bits)
  • Split: First 16 bytes = encryption key, last 16 bytes = MAC key

MAC (Message Authentication Code)

  • Purpose: Verify password correctness and data integrity
  • Algorithm: Keccak256
  • Input: macKey || ciphertext
  • Size: 32 bytes (256 bits)

References