cdw1p

BniEnc.pnp

Jul 20th, 2022 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. <?php
  2.  
  3. class BniEnc
  4. {
  5.     const TIME_DIFF_LIMIT = 480;
  6.  
  7.     public static function encrypt(array $json_data, $cid, $secret) {
  8.         return self::doubleEncrypt(strrev(time()) . '.' . json_encode($json_data), $cid, $secret);
  9.     }
  10.  
  11.     public static function decrypt($hased_string, $cid, $secret) {
  12.         $parsed_string = self::doubleDecrypt($hased_string, $cid, $secret);
  13.         list($timestamp, $data) = array_pad(explode('.', $parsed_string, 2), 2, null);
  14.         if (self::tsDiff(strrev($timestamp)) === true) {
  15.             return json_decode($data, true);
  16.         }
  17.         return null;
  18.     }
  19.  
  20.     private static function tsDiff($ts) {
  21.         return abs($ts - time()) <= self::TIME_DIFF_LIMIT;
  22.     }
  23.  
  24.     private static function doubleEncrypt($string, $cid, $secret) {
  25.         $result = '';
  26.         $result = self::enc($string, $cid);
  27.         $result = self::enc($result, $secret);
  28.         return strtr(rtrim(base64_encode($result), '='), '+/', '-_');
  29.     }
  30.  
  31.     private static function enc($string, $key) {
  32.         $result = '';
  33.         $strls = strlen($string);
  34.         $strlk = strlen($key);
  35.         for($i = 0; $i < $strls; $i++) {
  36.             $char = substr($string, $i, 1);
  37.             $keychar = substr($key, ($i % $strlk) - 1, 1);
  38.             $char = chr((ord($char) + ord($keychar)) % 128);
  39.             $result .= $char;
  40.         }
  41.         return $result;
  42.     }
  43.  
  44.     private static function doubleDecrypt($string, $cid, $secret) {
  45.         $result = base64_decode(strtr(str_pad($string, ceil(strlen($string) / 4) * 4, '=', STR_PAD_RIGHT), '-_', '+/'));
  46.         $result = self::dec($result, $cid);
  47.         $result = self::dec($result, $secret);
  48.         return $result;
  49.     }
  50.  
  51.     private static function dec($string, $key) {
  52.         $result = '';
  53.         $strls = strlen($string);
  54.         $strlk = strlen($key);
  55.         for($i = 0; $i < $strls; $i++) {
  56.             $char = substr($string, $i, 1);
  57.             $keychar = substr($key, ($i % $strlk) - 1, 1);
  58.             $char = chr(((ord($char) - ord($keychar)) + 256) % 128);
  59.             $result .= $char;
  60.         }
  61.         return $result;
  62.     }
  63.  
  64. }
Add Comment
Please, Sign In to add comment