Nulis Coding - Contoh Penerapan Enkripsi AES 256 bit di Pemrograman PHP
Seperti yang telah dijelaskan pada artikel sebelumnya yang berjudul "Mengenal Enkripsi Algoritma Advanced Encryption Standard (AES) 128, 192, 256 bit" kali ini akan kita lanjutkan mengenai proses penerapan dan implementasi algoritma kriptografi AES 256 bit pada PHP.
source: Google Images
Kita ada dua versi untuk pemanfaatan implementasinya yaitu dengan prosedural dan framework Codeigniter, langsung saja berikut untuk detail coding nya:
Versi Native/Prosedural PHP:
<?php $plaintext = 'My secret message 1234'; $password = 'KunciRahasia'; $method = 'aes-256-cbc'; // Must be exact 32 chars (256 bit) $password = substr(hash('sha256', $password, true), 0, 32); echo "Password:" . $password . "\n"; // IV must be exact 16 chars (128 bit) $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0); // av3DYGLkwBsErphcyYp+imUW4QKs19hUnFyyYcXwURU= $encrypted = base64_encode(openssl_encrypt($plaintext, $method, $password, OPENSSL_RAW_DATA, $iv)); // My secret message 1234 $decrypted = openssl_decrypt(base64_decode($encrypted), $method, $password, OPENSSL_RAW_DATA, $iv); echo 'plaintext=' . $plaintext . "\n"; echo 'cipher=' . $method . "\n"; echo 'encrypted to: ' . $encrypted . "\n"; echo 'decrypted to: ' . $decrypted . "\n\n";
Model untuk Codeigniter:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class ModelAes extends CI_Model { // VARIABEL & KEY private $password = 'KunciRahasia'; private $method = 'aes-256-cbc'; // AES 256 CBC (chipher block chaining) public function __construct(){ parent::__construct(); } // FUNGSI ENCRYPT AES public function encrypt_data($data){ // Password Harus tepat 32 karakter (256 bit) // 256-bit encryption key $password = substr(hash('sha256', $this->password, true), 0, 32); // IV (initialitation vector) harus tepat 16 blok karakter (128 bit) // IV is still 16 bytes or 128 bits for AES 128, 192 and 256. $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0); // PROSES ENKRIPSI $encrypted = base64_encode(openssl_encrypt($data, $this->method, $password, OPENSSL_RAW_DATA, $iv)); return $encrypted; } // FUNGSI DESCRYPT AES public function decrypt_data($data){ // Password Harus tepat 32 karakter (256 bit) // 256-bit encryption key $password = substr(hash('sha256', $this->password, true), 0, 32); // IV (initialitation vector) harus tepat 16 blok karakter (128 bit) // IV is still 16 bytes or 128 bits for AES 128, 192 and 256. $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0); // PROSES DESKRIPSI $decrypted = openssl_decrypt(base64_decode($data), $this->method, $password, OPENSSL_RAW_DATA, $iv); return $decrypted; } }
Bisa dilihat "KunciRahasia" merupakan kunci yang dijadikan proses enkripsi maupun deskripsi, bisa diganti sesuai keinginan.
Nah itu tadi contoh dari penggunaan dan penerapan enkripsi AES 256 bit di bahasa Pemrograman PHP.
Happy Coding guys!
Referensi:
https://gist.github.com/odan/138dbd41a0c5ef43cbf529b03d814d7c
0 Comments
Terimakasih atas kunjungan anda.
Jangan Lupa membagikan artikel ini kepada teman anda agar lebih bermanfaat.