• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List

E:/E/GEAMP/www/openbiz/openbiz/bin/service/cryptService.php

00001 <?php
00018 class cryptService
00019 {
00020     protected $m_DefaultKey ;
00021     public $m_Algorithm;
00022     public $m_OperationMode;
00023 
00030     function __construct(&$xmlArr)
00031     {
00032         $this->readMetadata($xmlArr);
00033     }
00034 
00041     protected function readMetadata(&$xmlArr)
00042     {
00043         $this->m_DefaultKey        = strtolower($xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["DEFAULTKEY"]);
00044        $this->m_Algorithm          = strtolower($xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["ALGORITHM"]);
00045         $this->m_OperationMode     = strtolower($xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["OPERATIONMODE"]);
00046     }
00047 
00048     public function encrypt($data, $key=null)
00049     {
00050        if(!function_exists("mcrypt_module_open"))
00051        {
00052               return $data;
00053        }
00054        if($key==null){
00055               $key = $this->m_DefaultKey;
00056        }
00057        if($data==null)
00058               return;
00059         $td = mcrypt_module_open($this->m_Algorithm, '', $this->m_OperationMode, '');
00060         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
00061        // $iv ="jixian";
00062         $ks = mcrypt_enc_get_key_size($td);
00063         $keystr = substr(md5($key), 0, $ks);
00064         mcrypt_generic_init($td, $keystr, $iv);
00065         $encrypted = mcrypt_generic($td, $data);
00066         mcrypt_module_close($td);
00067         $hexdata = bin2hex($encrypted);       
00068         return $hexdata;
00069     }
00070 
00071     public function decrypt($data, $key=null)
00072     {
00073        if(!function_exists("mcrypt_module_open"))
00074        {
00075               return $data;
00076        }
00077               if($key==null){
00078               $key = $this->m_DefaultKey;
00079        }      
00080        if($data==null)
00081               return;
00082         $td = mcrypt_module_open($this->m_Algorithm, '', $this->m_OperationMode, '');
00083         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);        
00084         $ks = mcrypt_enc_get_key_size($td);
00085         $keystr = substr(md5($key), 0, $ks);
00086         mcrypt_generic_init($td, $keystr, $iv);
00087         $encrypted = pack( "H*", $data);
00088         $decrypted = mdecrypt_generic($td, $encrypted);
00089         mcrypt_generic_deinit($td);
00090         mcrypt_module_close($td);        
00091         return trim($decrypted);
00092     }    
00093 
00094 }
00095 
00096 ?>

Generated on Thu Apr 19 2012 17:09:13 for openbiz by  doxygen 1.7.2