kodhe / encrypt
Modernized Encrypt Library for CodeIgniter 3 with OpenSSL support
Requires
- php: ^7.0|^8.0
- ext-hash: *
- ext-openssl: *
Requires (Dev)
- phpunit/phpunit: ^9.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-10-28 10:42:32 UTC
README
A modern encryption library for CodeIgniter 3 with OpenSSL support, replacing the deprecated mcrypt library.
โ ๏ธ DEPRECATED NOTICE
This library is DEPRECATED and is kept only for backward compatibility. For new projects, use the official OpenSSL-based CodeIgniter 3 Encryption library.
๐ Key Features
- โ OpenSSL Support: AES-256-CBC + HMAC-SHA256 for maximum security
- โ Backward Compatible: API identical to the legacy CI3 Encrypt library
- โ Legacy Data Support: Can read old (mcrypt) encrypted data
- โ Tamper Proof: Automatically detects tampered data
- โ PSR-4 Compliant: Modular, modern structure
- โ Performance Optimized: Key-derivation caching, batch operations
- โ
Migration Path: The
migrate()method moves old data to the new format
๐ฆ Installation
composer require kodhe/encrypt
Or add it to composer.json:
{
"require": {
"kodhe/encrypt": "^1.0"
}
}
๐ง Configuration
Add this in application/config/config.php:
$config['encryption_key'] = 'your-secret-key-min-32-chars-long';
IMPORTANT: Use a key of at least 32 characters for optimal security!
๐ก Usage
Traditional CI3 Style
$this->load->library('encrypt'); // Encryption $encrypted = $this->encrypt->encode('secret data'); // Decryption $decrypted = $this->encrypt->decode($encrypted); // With a custom key $encrypted = $this->encrypt->encode('data', 'custom-key');
Modern PSR-4 Style
use Kodhe\Encrypt\Encrypt; $encrypt = new Encrypt([ 'encryption_key' => 'your-secret-key-min-32-chars-long', 'algorithm' => 'aes-256-cbc', // optional ]); // Encryption $token = $encrypt->encode('user_id_123'); // Decryption $user_id = $encrypt->decode($token); // Batch operations (faster for large amounts of data) $encrypted_array = $encrypt->encode_many(['data1', 'data2', 'data3']); $decrypted_array = $encrypt->decode_many($encrypted_array);
๐ Security
New Data Format (OpenSSL)
Base64(IV[16 bytes] + HMAC-SHA256[32 bytes] + Ciphertext)
- IV (Initialization Vector): Random for every encryption
- HMAC: Prevents tampering/data manipulation
- AES-256-CBC: Industry standard for symmetric encryption
Tamper Detection
$data = $encrypt->decode($encrypted); if ($data === false) { // Data corrupted, wrong key, or tampered! log_message('error', 'Decryption failed - data may have been tampered with'); }
๐ Migrating from Legacy (mcrypt)
This library automatically detects and reads legacy CI3 data:
// Automatically detects the legacy format and decodes it $old_data = $this->encrypt->decode($legacy_encrypted); // Manual migration to the new format $new_encrypted = $this->encrypt->migrate($legacy_encrypted); // Check whether the data is still in the legacy format if ($this->encrypt->is_legacy_format($encrypted)) { // Perlu migrasi $this->encrypt->migrate($encrypted); }
Database Migration Script
// Migrate all encrypted data in the database $users = $this->db->get('users')->result(); foreach ($users as $user) { $migrated = $this->encrypt->migrate($user->encrypted_data); if ($migrated !== false) { $this->db->where('id', $user->id) ->update('users', ['encrypted_data' => $migrated]); } }
๐ API Reference
| Method | Description | Returns |
|---|---|---|
encode($string, $key = '') |
Encrypts a string | string (base64) |
decode($string, $key = '') |
Decrypts a string | string|false |
set_cipher($cipher) |
Sets the cipher algorithm | self |
set_mode($mode) |
Sets the operating mode | self |
sha1($str) |
Generates a SHA1 hash | string |
set_key($key) |
Sets the encryption key | self |
is_legacy_format($string) |
Checks for the legacy format | bool |
migrate($old_encoded) |
Migrates to the new format | string|false |
encode_many($strings, $key) |
Batch encryption | array |
decode_many($encoded, $key) |
Batch decryption | array |
๐งช Testing
composer test
๐ Comparison with Original CI3
| Feature | CI3 Encrypt (mcrypt) | Kodhe Encrypt (OpenSSL) |
|---|---|---|
| Engine | Mcrypt โ (removed PHP 7.2+) | OpenSSL โ |
| Integrity | โ None | โ HMAC-SHA256 |
| Tamper Proof | โ Silent corruption | โ Detect & reject |
| IV generation | Weak random | โ
random_bytes() |
| Key derivation | Simple substr | โ HKDF/PBKDF2 |
| Legacy support | N/A | โ Auto-detect |
| Batch ops | โ | โ
encode_many() |
โ ๏ธ Security Best Practices
- Use Long Keys: At least 32 characters
- Store Keys Securely: Don't commit them to Git; use environment variables
- Rotate Keys Periodically: Change the encryption key regularly
- HTTPS Always: Transmit encrypted data only over HTTPS
- Migrate Old Data: Move legacy data to the new format as soon as possible
๐ Changelog
v2.0.0 (2024)
- โ Full migration from mcrypt to OpenSSL
- โ Added HMAC for data integrity
- โ Support for auto-detecting the legacy format
- โ Batch operations for performance
- โ PSR-4 autoloading
v1.0.0 (Legacy)
- Original CI3 Encrypt library compatibility
๐ License
MIT License - see LICENSE file for details.
๐ค Contributing
Contributions welcome! Please read our contributing guidelines before submitting PRs.
๐ Support
- Issues: https://github.com/yourname/ci3-encrypt/issues
- Documentation: https://github.com/yourname/ci3-encrypt/wiki
Note: This library targets CodeIgniter 3. For CodeIgniter 4, use the built-in Encryption service.