bramanto

Bramanto

Aug 27th, 2020 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6.  
  7. /*
  8. |--------------------------------------------------------------------------
  9. | FUNCTION BUKA FILE EXT *.ini
  10. |--------------------------------------------------------------------------
  11. */
  12. function read_file_ini($path)
  13. {
  14.     $file = @parse_ini_file($path);
  15.  
  16.     if(!$file){
  17.         return 'File Not Found';
  18.         exit();
  19.     }
  20.    
  21.     return $file;
  22. }
  23.  
  24. /*
  25. |--------------------------------------------------------------------------
  26. | ENCRYPT SET KE COOKIE
  27. |--------------------------------------------------------------------------
  28. */
  29. function set_to_cookie($name, $cookie_data, $e_key, $a_key)
  30. {
  31.     $iv         = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
  32.     $ciphertext = mcrypt_encrypt(
  33.                     MCRYPT_RIJNDAEL_128,
  34.                     $e_key,
  35.                     json_encode($cookie_data),
  36.                     MCRYPT_MODE_ECB,
  37.                     $iv
  38.                 );
  39.    
  40.     $hmac = hash_hmac('sha256', $iv.$ciphertext, $a_key, true);
  41.    
  42.     return setcookie(
  43.         $name,
  44.         base64_encode(
  45.             $hmac.$iv.$ciphertext
  46.         )
  47.     );
  48. }
  49.  
  50. /*
  51. |--------------------------------------------------------------------------
  52. | DECRYPT DARI COOKIE
  53. |--------------------------------------------------------------------------
  54. */
  55. function get_from_cookie($name, $e_key, $a_key)
  56. {
  57.     if (!isset($_COOKIE[$name]))
  58.     {
  59.         return null;
  60.     }
  61.    
  62.     $decoded    = base64_decode($_COOKIE[$name]);
  63.     $hmac       = mb_substr($decoded, 0, 32, '8bit');
  64.     $iv         = mb_substr($decoded, 32, 16, '8bit');
  65.     $ciphertext = mb_substr($decoded, 48, null, '8bit');
  66.    
  67.     $calculated = hash_hmac('sha256', $iv.$ciphertext, $a_key, true);
  68.    
  69.     if (hash_equals($hmac, $calculated)) {
  70.         return $decrypted = rtrim(
  71.                 mcrypt_decrypt(
  72.                     MCRYPT_RIJNDAEL_128,
  73.                     $e_key,
  74.                     $ciphertext,
  75.                     MCRYPT_MODE_ECB,
  76.                     $iv
  77.                 ),
  78.                 "\0"
  79.             );
  80.        
  81.     }
  82. }
  83.  
  84. /*
  85. |--------------------------------------------------------------------------
  86. | CARA PAKAI
  87. |--------------------------------------------------------------------------
  88. */
  89.  
  90. // INISIALISASI KEY
  91. $sample_key = 'bramantootnamarb';
  92.  
  93. // BACA FILENYA
  94. $data_bram = read_file_ini('php.ini');
  95.  
  96. // ENCRYPT LALU SET KE COOKIE DENGAN NAMA BPKP
  97. set_to_cookie('BPKP', $data_bram, $sample_key, $sample_key);
  98.  
  99. // DECRYPT, MISALNYA DENGAN OUTPUT JSON
  100. $decrypt = get_from_cookie('BPKP', $sample_key, $sample_key);
  101.  
  102. header('Content-Type: application/json');
  103. print_r($decrypt);
  104.  
  105. // Source: https://bramanto.web.id/bpkp/bramanto.php
  106.  
  107.  
Add Comment
Please, Sign In to add comment