Advertisement
opsftw

PHC:Polymorphic Encryption

Apr 7th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. <?php
  2. class PHC
  3. {
  4.     # encrypt some text
  5.     public function encrypt( $plain ) {
  6.         $i = 8;
  7.         $key = '';
  8.         $chars = array(range('A','Z'),range('a','Z'));
  9.         while($i > 0) {
  10.             $key = array_rand($chars[rand(1,0)]);
  11.             $i--;
  12.         }
  13.         $block = '';
  14.         $i = 0;
  15.         $max = strlen($key) - 1;
  16.         foreach(str_split($plain) as $index => $char) {
  17.             if(rand(0,20) < 19) {
  18.                 $block .= "\\x".($index)."\\x".$char.$key[$i];
  19.             } else {
  20.                 $block .= "\\x".($index)."\\x".$char.$key[$i].$block;
  21.             }
  22.             $i = ($i + 1 > $max)?0:$i+1;
  23.         }
  24.         return $block;
  25.     }
  26.    
  27.     # decrypt some text
  28.     public function decrypt( $encrypted ) {
  29.         #$encrypted[strlen($encrypted)-1] = '';
  30.         $enc = $encrypted;
  31.         $blocks = array();
  32.         while(preg_match("#(\\\\x[0-9]+\\\\x(.|\.)*?)\\\\x#", $encrypted, $chunk)) {
  33.             $c = str_replace("\\x", "\\\\x", $chunk[1]);
  34.             $encrypted = preg_replace('#'.str_replace("\\x", "\\\\x", $chunk[1]).'#', '', $encrypted, 1);
  35.             $temp = explode("\\x", $chunk[1]);
  36.             $tmp = str_split($temp[2]);
  37.             $blocks[(int)$temp[1]] = $tmp[0];
  38.         }
  39.         $array = explode("\\x", $enc);
  40.         $last = array(array_pop($array), array_pop($array));
  41.         $blocks[$last[1]] = $last[0];
  42.         ksort($blocks);
  43.         return implode('', $blocks);
  44.     }
  45. }
  46. $phc = new PHC;
  47. $var = $phc->encrypt('Hello world');
  48. echo "{$var}<hr />";
  49. echo $phc->decrypt($var);
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement