RSA.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. class RSA
  3. {
  4. private $_privFile;
  5. private $_pubFile;
  6. private $_privKey;
  7. private $_pubKey;
  8. private $_algo ;
  9. private $_psw;
  10. public function __construct($conf)
  11. {
  12. $xml = new DOMDocument();
  13. $xml->load($conf);
  14. $items = $xml->getElementsByTagName("items");
  15. $this->_privFile = $items->item(0)->getElementsByTagName('pfx')->item(0)->nodeValue;
  16. $this->_pubFile = $items->item(0)->getElementsByTagName('cer')->item(0)->nodeValue;
  17. $this->_algo = OPENSSL_ALGO_SHA1;
  18. $this->_psw = $items->item(0)->getElementsByTagName('psw')->item(0)->nodeValue;
  19. }
  20. public function __destruct()
  21. {
  22. @ fclose($this->_privKey);
  23. @ fclose($this->_pubKey);
  24. }
  25. public function setupPrivKey()
  26. {
  27. if(is_resource($this->_privKey)){
  28. return true;
  29. }
  30. $prk = file_get_contents($this->_privFile);
  31. $this->_privKey = openssl_pkey_get_private($prk);
  32. return true;
  33. }
  34. public function setupPubKey()
  35. {
  36. if(is_resource($this->_pubKey)){
  37. return true;
  38. }
  39. $puk = file_get_contents($this->_pubFile);
  40. $this->_pubKey = openssl_pkey_get_public($puk);
  41. return true;
  42. }
  43. public function pubEncrypt($data)
  44. {
  45. if(!is_string($data)){
  46. return null;
  47. }
  48. $this->setupPubKey();
  49. $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
  50. if($r){
  51. return base64_encode($encrypted);
  52. }
  53. return null;
  54. }
  55. public function sign($data)
  56. {
  57. $digest=$data.$this->_psw;
  58. $privKey = file_get_contents($this->_privFile);
  59. openssl_sign($digest, $signature, $privKey, $this->_algo);
  60. return base64_encode($signature);
  61. }
  62. public function privDecrypt($encrypted)
  63. {
  64. if(!is_string($encrypted)){
  65. return null;
  66. }
  67. $this->setupPrivKey();
  68. $encrypted = base64_decode($encrypted);
  69. $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
  70. if($r){
  71. return $decrypted;
  72. }
  73. return null;
  74. }
  75. public function verify($data,$signature)
  76. {
  77. $digest=$data.$this->_psw;
  78. $pubKey = file_get_contents($this->_pubFile);
  79. return openssl_verify($digest, base64_decode($signature), $pubKey, $this->_algo );
  80. }
  81. public function privEncrypt($data)
  82. {
  83. if(!is_string($data)){
  84. return null;
  85. }
  86. $this->setupPrivKey();
  87. $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
  88. if($r){
  89. return base64_encode($encrypted);
  90. }
  91. return null;
  92. }
  93. public function pubDecrypt($crypted)
  94. {
  95. if(!is_string($crypted)){
  96. return null;
  97. }
  98. $this->setupPubKey();
  99. $crypted = base64_decode($crypted);
  100. $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
  101. if($r){
  102. return $decrypted;
  103. }
  104. return null;
  105. }
  106. }