When a database leaks, an attacker gets an SQL dump with thousands of rows — INN, passports, medical records. If data is stored in plaintext, it's a disaster: fines under 152-FZ up to 500,000 rubles, or under GDPR up to 4% of global annual turnover. The cost of a data breach averages $3.86 million globally (IBM Cost of a Data Breach Report 2022). Application-level encryption is the only way to devalue data for an attacker. Even if they get the database, without the keys the ciphertext is useless. We implement encryption in projects with high security requirements: fintech, medtech, gov. In 5 years on the market, we have completed over 30 projects for personal data protection. Our engineers hold CISSP certifications, and we guarantee compliance with 152-FZ and GDPR. We will audit your system in 2-3 days.
Let me give a real-world example from our practice: encryption in a medical CRM with 50,000 patients.
Why Encrypt Data at the Application Level?
Database-level encryption (TDE) does not protect against DBAs or SQL injections. The application itself manages keys: data is encrypted before writing and decrypted only by authorized queries. This provides granular access control and isolates data from the infrastructure.
Data Encryption vs Hashing
Data to Encrypt
- INN, SNILS, passport series/number
- Medical data, diagnoses
- Financial data, card numbers (PCI DSS requires a separate approach)
- Biometrics
Data to Hash
- Passwords → bcrypt, Argon2id
- Secret tokens, API keys
For searching encrypted fields, we use deterministic encryption or tokenization — more on that later.
Choosing an Algorithm: AES-256-GCM vs ChaCha20-Poly1305
| Algorithm | Type | Authentication | Speed | Hardware Acceleration |
|---|---|---|---|---|
| AES-256-GCM | Symmetric | Yes (GCM) | High | Yes (AES-NI) |
| ChaCha20-Poly1305 | Symmetric | Yes (Poly1305) | High | No (but fast in software) |
| RSA-OAEP | Asymmetric | Yes | Low | Yes (partial) |
AES-256-GCM is the standard for encrypting data at rest. With AES-NI, it is 3x faster than ChaCha20-Poly1305. If the server lacks AES-NI support, ChaCha20-Poly1305 is a reliable alternative. RSA-OAEP is used for encrypting keys or when decryption cannot be done on the server. More about AES-GCM.
Based on OWASP and NIST recommendations.
Practical Implementation of Encryption in Laravel
Laravel encryption tools provide transparent data protection. We use custom casts to encrypt sensitive fields automatically.
Example Cast for Encrypting Fields
// app/Casts/EncryptedCast.php class EncryptedCast implements CastsAttributes { public function get($model, string $key, $value, array $attributes): ?string { if (is_null($value)) return null; try { return Crypt::decryptString($value); } catch (DecryptException) { return null; } } public function set($model, string $key, $value, array $attributes): ?string { if (is_null($value)) return null; return Crypt::encryptString($value); } } // In the model class Patient extends Model { protected $casts = [ 'passport_number' => EncryptedCast::class, 'medical_notes' => EncryptedCast::class, 'snils' => EncryptedCast::class, ]; } // Usage — transparent to the code $patient->passport_number = '4510 123456'; // automatically encrypted $decrypted = $patient->passport_number; // automatically decrypted Advantages of Envelope Encryption
Storing encryption keys in the database alongside encrypted data is pointless. The best practice is envelope encryption: data is encrypted with a Data Encryption Key (DEK), the DEK is encrypted with a Key Encryption Key (KEK), and the KEK is stored in KMS/Vault. This allows re-encrypting data without access to the KEK and simplifies rotation.
HashiCorp Vault integration:
$vault = new Vault([ 'address' => 'https://vault.internal:8200', 'token' => env('VAULT_TOKEN'), ]); $keyData = $vault->read('secret/data/app-encryption-key'); $encryptionKey = $keyData['data']['key']; AWS KMS:
use Aws\Kms\KmsClient; $kms = new KmsClient(['region' => 'eu-west-1']); $result = $kms->encrypt([ 'KeyId' => 'arn:aws:kms:eu-west-1:123456:key/abc-123', 'Plaintext' => $sensitiveData, ]); $encryptedData = base64_encode($result['CiphertextBlob']); Searching Over Encrypted Data
Standard AES-GCM produces different ciphertext for the same value. Searching an encrypted field is impossible. Solutions:
Option 1: Hash for search + encryption for storage:
class PersonalDataRepository { public function findByPassport(string $passport): ?Patient { $hash = hash_hmac('sha256', $passport, config('app.search_key')); return Patient::where('passport_hash', $hash)->first(); } public function store(string $passport): void { Patient::create([ 'passport_data' => Crypt::encryptString($passport), 'passport_hash' => hash_hmac('sha256', $passport, config('app.search_key')), ]); } } Option 2: PostgreSQL pgcrypto:
INSERT INTO patients (passport) VALUES (pgp_sym_encrypt('4510 123456', current_setting('app.encryption_key'))); SELECT pgp_sym_decrypt(passport::bytea, current_setting('app.encryption_key')) FROM patients WHERE id = 1; Comparison of Data Protection Methods
| Method | Reversible | Search | Speed | Key Security |
|---|---|---|---|---|
| AES-256-GCM | Yes | No | High | Depends on storage |
| Deterministic encryption | Yes | Yes | Medium | High with HMAC |
| Tokenization | No (replacement) | Yes | High | Very high |
| Hashing (bcrypt) | No | No | Low | High |
Case Study: Encryption in a Medical CRM (From Our Practice)
Client — a network of clinics with 50,000 patients. Requirement: encrypt passport data, SNILS, diagnoses. We chose AES-256-GCM + envelope encryption with HashiCorp Vault. Implemented Laravel Casts for transparent encryption. Added HMAC hash for searching by policy number. Result: response time unchanged, security audit passed, 152-FZ compliance certificate obtained. The solution scales to any number of records.
What's Included in the Work
- Designing the encryption architecture considering business logic
- Selecting algorithms and key management scheme (envelope encryption)
- Integration with HashiCorp Vault or AWS KMS
- Implementing transparent encryption at the ORM level (Laravel, Doctrine, etc.)
- Configuring deterministic encryption for search
- Key rotation with zero downtime
- Access auditing and operation logging
- Documentation for the team and developer training
- Transferring access and test scenarios
Pricing: from $3,000 for small projects, $10,000+ for enterprise with custom integration.
Implementation Process and Timeline
- Designing encryption schema and key management (1-2 days).
- Implementing encryption at the model level (2-3 days).
- Integration with external key stores (5-7 days).
- Deterministic encryption for search (+3 days).
- Implementing key rotation with zero downtime (+2 days).
- Access auditing and operation logging (+1-2 days).
- Documentation and team training (+1 day).
Final timeline — from 2 weeks to 2 months, depending on data volume and business logic complexity. This investment typically pays for itself by avoiding just one major data breach fine – savings of up to $500,000 under 152-FZ or millions under GDPR.
We offer turnkey implementation starting from $3,000. Contact us for a free evaluation of your project.







