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
- Generate random salt (32 bytes) and IV (16 bytes)
- Derive key from password using KDF (scrypt or PBKDF2)
- Split derived key: first 16 bytes for encryption, next 16 for MAC
- Encrypt private key with AES-128-CTR using encryption key and IV
- Compute MAC as
keccak256(macKey || ciphertext) - Assemble keystore JSON structure
Basic Encryption
Encryption Options
KDF Selection
- Scrypt (Default)
- PBKDF2
- Memory-hard (expensive to parallelize)
- GPU/ASIC resistant
- Higher security against brute-force
- 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
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)

