PHP code example of kodhe / encrypt

1. Go to this page and download the library: Download kodhe/encrypt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kodhe / encrypt example snippets


$config['encryption_key'] = 'your-secret-key-min-32-chars-long';

$this->load->library('encrypt');

// Enkripsi
$encrypted = $this->encrypt->encode('data rahasia');

// Dekripsi
$decrypted = $this->encrypt->decode($encrypted);

// Dengan key custom
$encrypted = $this->encrypt->encode('data', 'custom-key');

use Kodhe\Encrypt\Encrypt;

$encrypt = new Encrypt([
    'encryption_key' => 'your-secret-key-min-32-chars-long',
    'algorithm' => 'aes-256-cbc', // optional
]);

// Enkripsi
$token = $encrypt->encode('user_id_123');

// Dekripsi
$user_id = $encrypt->decode($token);

// Batch operations (lebih cepat untuk banyak data)
$encrypted_array = $encrypt->encode_many(['data1', 'data2', 'data3']);
$decrypted_array = $encrypt->decode_many($encrypted_array);

$data = $encrypt->decode($encrypted);

if ($data === false) {
    // Data corrupted, kunci salah, atau di-tamper!
    log_message('error', 'Dekripsi gagal - data mungkin di-tamper');
}

// Otomatis detect format legacy dan decode
$old_data = $this->encrypt->decode($legacy_encrypted);

// Migrasi manual ke format baru
$new_encrypted = $this->encrypt->migrate($legacy_encrypted);

// Cek apakah data masih format legacy
if ($this->encrypt->is_legacy_format($encrypted)) {
    // Perlu migrasi
    $this->encrypt->migrate($encrypted);
}

// Migrasi semua data terenkripsi di 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]);
    }
}