jubaopay.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. require_once(APP_ROOT_PATH.'system/payment/Jubaopay/RSA.php');
  3. class jubaopay
  4. {
  5. private $_rsa;
  6. private $message;
  7. private $signature;
  8. private $digest;
  9. private $encrypts;
  10. public function __construct($conf)
  11. {
  12. $this->_rsa = new RSA($conf);
  13. $this->message="";
  14. $this->signature="";
  15. $this->digest="";
  16. $this->encrypts=array();
  17. }
  18. public function __destruct()
  19. {
  20. }
  21. public function __set($name,$value)
  22. {
  23. $this->$name = $value ;
  24. }
  25. public function __get($name)
  26. {
  27. return $this->$name;
  28. }
  29. public function generateRandomString( $length = 16 ) {
  30. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
  31. $password = "";
  32. for ( $i = 0; $i < $length; $i++ )
  33. $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
  34. return $password;
  35. }
  36. public function setEncrypt($key,$value){
  37. $this->encrypts[$key]=$value;
  38. }
  39. public function getEncrypt($key){
  40. return $this->encrypts[$key];
  41. }
  42. public function interpret()
  43. {
  44. $this->digest="";
  45. $plainString="";
  46. $count=0;
  47. foreach( $this->encrypts as $key => $value ) {
  48. $count=$count+1;
  49. $this->digest.=$value;
  50. $plainString.=urlencode($key)."&".urlencode($value);
  51. if ($count < count($this->encrypts))
  52. $plainString.="&";
  53. }
  54. $key = $this->generateRandomString();
  55. $iv = $this->generateRandomString();
  56. $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plainString, MCRYPT_MODE_CBC, $iv));
  57. $this->message=$this->_rsa->pubEncrypt($key).$this->_rsa->pubEncrypt($iv).$encrypted;
  58. $this->signature=$this->_rsa->sign($this->digest);
  59. }
  60. public function decrypt($message)
  61. {
  62. $this->message=$message;
  63. $key=$this->_rsa->privDecrypt(substr($message,0,172));
  64. $iv=$this->_rsa->privDecrypt(substr($message,172,172));
  65. $decrypted = base64_decode(substr($message,172+172));
  66. $plainString=rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted, MCRYPT_MODE_CBC, $iv),"\0");
  67. $this->digest="";
  68. $this->encrypts=array();
  69. $items=explode('&', $plainString);
  70. for ($i=0; $i<count($items)/2; $i++){
  71. $field = urldecode($items[2*$i]);
  72. $value = urldecode($items[2*$i+1]);
  73. $this->encrypts[$field] = $value;
  74. $this->digest.=$value;
  75. }
  76. }
  77. public function verify($signature)
  78. {
  79. $this->signature=$signature;
  80. return $this->_rsa->verify($this->digest,$this->signature);
  81. }
  82. public function encryptComfirm()
  83. {
  84. $this->digest="";
  85. $plainString="";
  86. $count=0;
  87. foreach( $this->encrypts as $key => $value ) {
  88. $count=$count+1;
  89. $this->digest.=$value;
  90. $plainString.=urlencode($key)."&".urlencode($value);
  91. if ($count < count($this->encrypts))
  92. $plainString.="&";
  93. }
  94. $key = $this->generateRandomString();
  95. $iv = $this->generateRandomString();
  96. $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plainString, MCRYPT_MODE_CBC, $iv));
  97. $this->message=$this->_rsa->pubEncrypt($key).$this->_rsa->pubEncrypt($iv).$encrypted;
  98. }
  99. public function signComfirm()
  100. {
  101. $this->signature=$this->_rsa->sign($this->digest);
  102. return $this->signature;
  103. }
  104. }