Overview
TheEncryptionService provides symmetric encryption using AES-256-CBC to protect sensitive data like API keys, access tokens, and passwords stored in the database.
All sensitive credentials in the
bot_credentials and google_oauth_credentials tables are encrypted at rest using this service.Configuration
The encryption service requires a cipher key to be set in your environment configuration:.env
Initialization
Encrypting Data
Encrypt sensitive strings before storing them in the database:Encryption Process
The encryption method:- Generates a random initialization vector (IV)
- Encrypts the plaintext using AES-256-CBC
- Prepends the IV to the ciphertext
- Base64-encodes the result for safe storage
Each encryption operation uses a unique IV, ensuring that encrypting the same value twice produces different ciphertext.
Decrypting Data
Decrypt encrypted values when reading from the database:Error Handling
Decryption can fail if the data is corrupted or the wrong key is used:Checking Encryption Status
Verify if a value is encrypted before attempting decryption:The
isEncrypted() method checks for valid base64 encoding and sufficient length. It cannot definitively verify that data was encrypted by this service.Real-World Examples
API Reference
__construct(string $cipherKey = null)
Initializes the encryption service with a cipher key.
Location: src/Services/EncryptionService.php:10
Parameters:
$cipherKey(string, optional): 32-character encryption key. If not provided, reads fromAPP_CIPHER_KEYenvironment variable.
RuntimeException if cipher key is not configured
encrypt(string $value): string
Encrypts a plaintext string using AES-256-CBC.
Location: src/Services/EncryptionService.php:19
Parameters:
$value(string): Plaintext to encrypt
RuntimeException if encryption fails
decrypt(string $value): string
Decrypts an encrypted string.
Location: src/Services/EncryptionService.php:33
Parameters:
$value(string): Base64-encoded ciphertext (fromencrypt()method)
RuntimeExceptionif data format is invalidRuntimeExceptionif decryption fails (wrong key or corrupted data)
isEncrypted(string $value): bool
Checks if a string appears to be encrypted data.
Location: src/Services/EncryptionService.php:54
Parameters:
$value(string): String to check
true if value appears to be encrypted, false otherwise
Security Best Practices
Key Rotation
Periodically rotate your cipher key. Re-encrypt all data with the new key and securely destroy the old key.
Secure Key Storage
Store the cipher key in a secure location separate from the application code and database backups.
Environment Isolation
Use different cipher keys for development, staging, and production environments.
Backup Strategy
Back up the cipher key securely. Without it, encrypted data in backups cannot be recovered.
Encryption Algorithm Details
| Property | Value |
|---|---|
| Algorithm | AES-256-CBC |
| Key Size | 256 bits (32 bytes) |
| IV Size | 128 bits (16 bytes) |
| Mode | Cipher Block Chaining (CBC) |
| Padding | PKCS#7 |
| Encoding | Base64 (for storage) |
Related Services
Database
Store encrypted credentials in the database
Configuration
Manage encryption keys and settings