Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class PHC
- {
- # encrypt some text
- public function encrypt( $plain ) {
- $i = 8;
- $key = '';
- $chars = array(range('A','Z'),range('a','Z'));
- while($i > 0) {
- $key = array_rand($chars[rand(1,0)]);
- $i--;
- }
- $block = '';
- $i = 0;
- $max = strlen($key) - 1;
- foreach(str_split($plain) as $index => $char) {
- if(rand(0,20) < 19) {
- $block .= "\\x".($index)."\\x".$char.$key[$i];
- } else {
- $block .= "\\x".($index)."\\x".$char.$key[$i].$block;
- }
- $i = ($i + 1 > $max)?0:$i+1;
- }
- return $block;
- }
- # decrypt some text
- public function decrypt( $encrypted ) {
- #$encrypted[strlen($encrypted)-1] = '';
- $enc = $encrypted;
- $blocks = array();
- while(preg_match("#(\\\\x[0-9]+\\\\x(.|\.)*?)\\\\x#", $encrypted, $chunk)) {
- $c = str_replace("\\x", "\\\\x", $chunk[1]);
- $encrypted = preg_replace('#'.str_replace("\\x", "\\\\x", $chunk[1]).'#', '', $encrypted, 1);
- $temp = explode("\\x", $chunk[1]);
- $tmp = str_split($temp[2]);
- $blocks[(int)$temp[1]] = $tmp[0];
- }
- $array = explode("\\x", $enc);
- $last = array(array_pop($array), array_pop($array));
- $blocks[$last[1]] = $last[0];
- ksort($blocks);
- return implode('', $blocks);
- }
- }
- $phc = new PHC;
- $var = $phc->encrypt('Hello world');
- echo "{$var}<hr />";
- echo $phc->decrypt($var);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement