x2Fusion

PHP GhostCrypt : GhostCrypt.class.php

Dec 22nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1.  <?php
  2.  
  3. /**
  4.  *
  5.  * GhostCrypt 2.5
  6.  *
  7.  * Minimal php-5.3.3
  8.  * Version: 2.5
  9.  *
  10.  */
  11.  
  12. class GhostCrypt
  13. {
  14.     // Data to be encrypted.
  15.     private $data;
  16.    
  17.     // Public Key to be used,
  18.     // if used within class construction
  19.     private $pubKey;
  20.  
  21.    // Class construction.
  22.     public function __construct($inFile = null, $outFileExt = '', $pubKey = '')
  23.     {
  24.         // Check whether the input file is empty or null.
  25.         if(empty($inFile) || $inFile == null)
  26.         {
  27.             die("Err: Your input inFile is Blank or Null!" . PHP_EOL);
  28.         }
  29.         // Public key, unless one is inserted, generate a random public key.
  30.         $this->pubKey = ( empty($pubKey) ) ? openssl_random_pseudo_bytes(32) : $pubKey;
  31.         $this->data = file_get_contents($inFile);
  32.         $this->data = $this->encrypt();
  33.         $inFileInfo = pathinfo($inFile);
  34.         list($outFile,) = explode($inFileInfo['extension'], $inFile);
  35.         $outFile = $outFile . (empty($fileExt) ? 'enc.' . $inFileInfo['extension'] : $fileExt);
  36.         file_put_contents($outFile, $this->decrypt().$this->data);
  37.     }
  38.  
  39.     // Class Encryption
  40.     private function encrypt()
  41.     {
  42.         // Public ? : or Private key,
  43.         // whether GhostHash is implemented within your environment.
  44.         $k = ( method_exists('GhostHash','returnHash') ) ? GhostHash::returnHash($this->pubKey) : $this->pubKey;
  45.         $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
  46.         $encrypted = openssl_encrypt($this->data, 'aes-256-cbc', $k, 0, $iv);
  47.         $encrypted = "#:#" . $encrypted . ':' . base64_encode($iv) . ':' . base64_encode($this->pubKey);
  48.         return $encrypted;
  49.     }
  50.  
  51.    // Class decryption/ Self Decrypt function.
  52.     private function decrypt()
  53.     {
  54.         return '<?php
  55. $data = file_get_contents(__FILE__);
  56. $lines = explode(PHP_EOL, $data);
  57. foreach($lines as $key => $line)
  58. {
  59.    if(substr($line, 0, 3) == "#:#")
  60.    {
  61.        $part = explode(":", substr($line, 3));
  62.        $k = ( method_exists("GhostHash","returnHash") ) ? GhostHash::returnHash(base64_decode($part[2])) : base64_decode($part[2]);
  63.        eval( "?>" . openssl_decrypt($part[0], "aes-256-cbc", $k, 0, base64_decode($part[1])) );
  64.    }
  65. }
  66.  
  67. ';
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment