Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- /*
- |--------------------------------------------------------------------------
- | FUNCTION BUKA FILE EXT *.ini
- |--------------------------------------------------------------------------
- */
- function read_file_ini($path)
- {
- $file = @parse_ini_file($path);
- if(!$file){
- return 'File Not Found';
- exit();
- }
- return $file;
- }
- /*
- |--------------------------------------------------------------------------
- | ENCRYPT SET KE COOKIE
- |--------------------------------------------------------------------------
- */
- function set_to_cookie($name, $cookie_data, $e_key, $a_key)
- {
- $iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
- $ciphertext = mcrypt_encrypt(
- MCRYPT_RIJNDAEL_128,
- $e_key,
- json_encode($cookie_data),
- MCRYPT_MODE_ECB,
- $iv
- );
- $hmac = hash_hmac('sha256', $iv.$ciphertext, $a_key, true);
- return setcookie(
- $name,
- base64_encode(
- $hmac.$iv.$ciphertext
- )
- );
- }
- /*
- |--------------------------------------------------------------------------
- | DECRYPT DARI COOKIE
- |--------------------------------------------------------------------------
- */
- function get_from_cookie($name, $e_key, $a_key)
- {
- if (!isset($_COOKIE[$name]))
- {
- return null;
- }
- $decoded = base64_decode($_COOKIE[$name]);
- $hmac = mb_substr($decoded, 0, 32, '8bit');
- $iv = mb_substr($decoded, 32, 16, '8bit');
- $ciphertext = mb_substr($decoded, 48, null, '8bit');
- $calculated = hash_hmac('sha256', $iv.$ciphertext, $a_key, true);
- if (hash_equals($hmac, $calculated)) {
- return $decrypted = rtrim(
- mcrypt_decrypt(
- MCRYPT_RIJNDAEL_128,
- $e_key,
- $ciphertext,
- MCRYPT_MODE_ECB,
- $iv
- ),
- "\0"
- );
- }
- }
- /*
- |--------------------------------------------------------------------------
- | CARA PAKAI
- |--------------------------------------------------------------------------
- */
- // INISIALISASI KEY
- $sample_key = 'bramantootnamarb';
- // BACA FILENYA
- $data_bram = read_file_ini('php.ini');
- // ENCRYPT LALU SET KE COOKIE DENGAN NAMA BPKP
- set_to_cookie('BPKP', $data_bram, $sample_key, $sample_key);
- // DECRYPT, MISALNYA DENGAN OUTPUT JSON
- $decrypt = get_from_cookie('BPKP', $sample_key, $sample_key);
- header('Content-Type: application/json');
- print_r($decrypt);
- // Source: https://bramanto.web.id/bpkp/bramanto.php
Add Comment
Please, Sign In to add comment