1. <?
  2. function generate_random($n){
  3. $bytes = openssl_random_pseudo_bytes($n, $cstrong);
  4. $hex   = bin2hex($bytes);
  5. if($cstrong) return $hex;
  6. else die('Error');
  7. }
  8.  
  9. class E_userside {
  10.     var $password;
  11.     var $signup_salt;
  12.     var $p;
  13.    
  14.     function createUniqueIV(){
  15.     return generate_random(8);
  16.     }
  17.  
  18.     function createUserSalt(){
  19.       return generate_random(300);
  20.     }
  21.  
  22.     function fetchPubKey(){
  23.     $this->p=openssl_pkey_get_public(file_get_contents('publickey'));
  24.     }
  25.  
  26.  
  27.   function encrypt($message){
  28.       $iv=$this->createUniqueIV();
  29.       $dk=generate_random(32);
  30.       $uk_ = $dk ^ $this->getuk();
  31.       openssl_public_encrypt($dk, $dk_,$this->p);
  32.       return base64_encode(serialize(array('cipher'=>openssl_encrypt ($message, 'aes192', $dk, false,$iv),'iv'=>$iv, 'uk_'=>$uk_, 'dk_'=>$dk_)));
  33.      
  34.   }
  35.  
  36.   function decrypt($result){
  37.       $array=unserialize(base64_decode($result));
  38.         $uk=$this->getuk();
  39.         $dk=$array['uk_']^$uk;
  40.         return openssl_decrypt ($array['cipher'], 'aes192', $dk, false,$array['iv']);
  41.     }
  42.        
  43.  
  44. }
  45. ######################ADMIN
  46. class E_adminside {
  47.     var $P;
  48.    
  49.     function createUniqueIV(){
  50.     return generate_random(8);
  51.     }
  52.  
  53.     function fetchPrivateKey(){
  54.     $this->P=openssl_pkey_get_private(file_get_contents('privatekey'));
  55.     }
  56.  
  57.  
  58.   function encryptComment($message,$params){
  59.     openssl_private_decrypt($params['dk_'], $dk,$this->P);
  60.       $iv=$this->createUniqueIV();
  61.       return array('cipher'=>openssl_encrypt ($message, 'aes192', $dk, false,$iv),'iv'=>$iv, 'uk_'=>$params['uk_'], 'dk_'=>$params['dk_']);
  62.      
  63.   }
  64.  
  65.   function decrypt($data){
  66.         $array=unserialize(base64_decode($data));
  67.         openssl_private_decrypt($array['dk_'], $dk,$this->P);
  68.         return openssl_decrypt ($array['cipher'], 'aes192', $dk, false,$array['iv']);
  69.     }
  70.        
  71.  
  72. }
  73. ?>