| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- class RSA
- {
- private $_privFile;
- private $_pubFile;
-
- private $_privKey;
- private $_pubKey;
-
- private $_algo ;
- private $_psw;
- public function __construct($conf)
- {
- $xml = new DOMDocument();
- $xml->load($conf);
- $items = $xml->getElementsByTagName("items");
-
- $this->_privFile = $items->item(0)->getElementsByTagName('pfx')->item(0)->nodeValue;
- $this->_pubFile = $items->item(0)->getElementsByTagName('cer')->item(0)->nodeValue;
- $this->_algo = OPENSSL_ALGO_SHA1;
- $this->_psw = $items->item(0)->getElementsByTagName('psw')->item(0)->nodeValue;
- }
-
- public function __destruct()
- {
- @ fclose($this->_privKey);
- @ fclose($this->_pubKey);
- }
- public function setupPrivKey()
- {
- if(is_resource($this->_privKey)){
- return true;
- }
- $prk = file_get_contents($this->_privFile);
- $this->_privKey = openssl_pkey_get_private($prk);
- return true;
- }
-
- public function setupPubKey()
- {
- if(is_resource($this->_pubKey)){
- return true;
- }
- $puk = file_get_contents($this->_pubFile);
- $this->_pubKey = openssl_pkey_get_public($puk);
- return true;
- }
-
- public function pubEncrypt($data)
- {
- if(!is_string($data)){
- return null;
- }
-
- $this->setupPubKey();
-
- $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
- if($r){
- return base64_encode($encrypted);
- }
- return null;
- }
-
- public function sign($data)
- {
- $digest=$data.$this->_psw;
- $privKey = file_get_contents($this->_privFile);
- openssl_sign($digest, $signature, $privKey, $this->_algo);
- return base64_encode($signature);
- }
-
- public function privDecrypt($encrypted)
- {
- if(!is_string($encrypted)){
- return null;
- }
-
- $this->setupPrivKey();
-
- $encrypted = base64_decode($encrypted);
-
- $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
- if($r){
- return $decrypted;
- }
- return null;
- }
-
- public function verify($data,$signature)
- {
- $digest=$data.$this->_psw;
- $pubKey = file_get_contents($this->_pubFile);
- return openssl_verify($digest, base64_decode($signature), $pubKey, $this->_algo );
- }
-
- public function privEncrypt($data)
- {
- if(!is_string($data)){
- return null;
- }
-
- $this->setupPrivKey();
-
- $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
- if($r){
- return base64_encode($encrypted);
- }
- return null;
- }
-
- public function pubDecrypt($crypted)
- {
- if(!is_string($crypted)){
- return null;
- }
-
- $this->setupPubKey();
-
- $crypted = base64_decode($crypted);
- $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
- if($r){
- return $decrypted;
- }
- return null;
- }
- }
|