SEA.decrypt()
Decrypt data that was encrypted with SEA.encrypt(). Requires the same key that was used for encryption.
Syntax
Parameters
- encrypted (string|object): Encrypted data from
SEA.encrypt()
- key (string|object): Decryption key or key pair with
epriv property
- callback (function, optional): Called with decrypted result
- options (object, optional):
encode: Encoding of encrypted data (default: ‘base64’)
name: Decryption algorithm (default: ‘AES-GCM’)
Returns
A Promise that resolves to the decrypted data in its original format.
Basic Usage
Decrypt with Password
Decrypt with Key Pair
With Callback
How It Works
Reference: ~/workspace/source/sea/decrypt.js:8-40
- Parse Encrypted Data: Extracts
ct, iv, and s from encrypted string/object
- Decode: Converts base64 strings to binary buffers
- Derive Key: Reconstructs AES key from input key + salt
- Decrypt: Uses AES-GCM to decrypt ciphertext
- Parse Result: Converts decrypted data back to original format
Decryption Process
Decryption Key Types
Using a Password
Using Key Pair
Using epriv Directly
Using Shared Secret
Data Type Preservation
Decryption restores the original data type:
Strings
Objects
Arrays
Numbers
Error Handling
Wrong Key
With Callbacks
Corrupted Data
Decryption failures often return undefined rather than throwing errors. Always check the result before using it.
Options
Custom Encoding
If data was encrypted with custom encoding:
Fallback Encoding
Reference: ~/workspace/source/sea/decrypt.js:24-28
SEA automatically falls back to ‘utf8’ encoding for legacy data:
Use Cases
Decrypt Private User Data
End-to-End Encrypted Messaging
Decrypt Backup
Conditional Decryption
Security Considerations
Authentication Tag
AES-GCM includes authentication:
- Verifies data integrity
- Detects tampering
- Decryption fails if ciphertext modified
Reference: ~/workspace/source/sea/decrypt.js:22
Key Security
Keep decryption keys secure:
- Never log keys to console
- Don’t send keys over unencrypted channels
- Store keys encrypted when possible
- Use the same key that was used for encryption
Timing Attacks
Web Crypto API implementations use constant-time operations to prevent timing attacks.
- Speed: Very fast (hardware-accelerated)
- Async: Always asynchronous
- Memory: Efficient (streaming decryption where supported)
Common Patterns
Decrypt Helper Function
Batch Decryption
Safe Decryption
Integration with GUN
Auto-decrypt User Data
Middleware Pattern
Troubleshooting
Decryption Returns Undefined
Causes:
- Wrong decryption key
- Corrupted encrypted data
- Data was not encrypted
- Different encoding used
Solution:
“Could not decrypt” Error
Cause: Encoding mismatch or corrupted data
Solution: