Jika Ada mengaktifkan $config['sess_encrypt_cookie'] = TRUE di config.php , saat mencoba buka cookies Anda dengan print_r($_COOKIE) , maka hasil cetak yang keluar adalah data cookies Anda tapi sudah terenkripsi sehingga sulit dibaca. Tapi jika menonaktifkan $config['sess_encrypt_cookie'] = FALSE saat dicetak akan muncul data cookies Anda dalam format JSON yang bisa dipahami. Proses tersebut disebut enkripsi, salah satu faktor mengapa proses itu ada adalah karena faktor keamanan. Penjelasan lengkap bisa dibaca di CodeIgniter User Guide.
Berikut contoh kode enskripsi text file. (keterangan ada di sumber kode) :
Proses enkripsi (encode data ke text file) :
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public function encrypt() { $data['book_title'] = 'Senyumku adalah Tangisku' ; $data['book_desc'] = '<em>Deskripsi Tulisan</em>' ; $data['book_date'] = '1980-07-09 13:13:09' ; $data['book_author'] = 'Fredy Siswanto' ; $this->load->helper('file') ; $this->load->library('encrypt') ; // ubah ke format json terlebih dahulu $data_to_json = json_encode($data) ; // ini kode kunci. $secure_key = 'kode kunci Anda!' ; // encrypt data $json_to_encrypt = $this->encrypt->encode($data_to_json,$secure_key) ; /* ALTERNATIVE $secure_key bisa dihilangkan sehingga bisa ditulis : $json_to_encrypt = $this->encrypt->encode($text) ============================================= asal di bagian application/config/config.php bagian $config['encryption_key'] diisi sesuai dengan key Anda. ======================================================= */ // nama file yang dienkripsi $filename = 'book_freddyS-1.txt' ; // lokasi folder tempat menyimpan $dir = APPPATH . 'cache/' ; // ~ application/cache/ // simpan ke $filename ; if ( ! write_file( $dir . $filename , $json_to_encrypt ) ) { echo 'Unable to write the file'; } else { echo 'File written!'; } } |
Hasil tampilan file di folder cache :

...
Saat file book_freddyS-1.txt dibuka :

...
Untuk menampilkan data tersebut ke sistem tentu saja bukan hasil enkripsi tersebut ditampilkan, tapi data sebenarnya. Berikut contoh kode untuk mengembalikan hasil enkripsi tersebut ke data asal :
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | public function decrypt() { // nama file yang dienkripsi $filename = 'book_freddyS-1.txt' ; // lokasi folder tempat menyimpan $dir = APPPATH . 'cache/' ; // ~ application/cache/ if ( file_exists( $dir. $filename ) ) { $this->load->helper('file') ; $this->load->library('encrypt') ; // simpan data di text file dalam satu variable $text = file_get_contents( $dir . $filename ) ; // ini kode kunci. $secure_key = 'kode kunci Anda!' ; // decode text $text_to_json = $this->encrypt->decode($text,$secure_key) ; /* ALTERNATIVE $secure_key bisa dihilangkan sehingga bisa ditulis : $text_to_json = $this->encrypt->decode($text) ============================================= asal di bagian application/config/config.php bagian $config['encryption_key'] diisi sesuai dengan key Anda. ======================================================= */ // tahap ini data sudah didapatkan tapi dalam format json // decode data json $json_to_data = json_decode($text_to_json,TRUE) ; // tahap ini selesai // data telah tersimpan dalam bentuk array print_r($json_to_data) ; } else { echo 'Unable to read the file'; } } |
Saat dijalankan :

...
CMIIW.


Thanks for the interesting information. Subscribe to rss
[...] .:: Kohaci Site ::. » Blog Archive » Enkripsi Teks di CodeIgniter. Share this:TwitterFacebookLike this:LikeBe the first to like this post. [...]