首页 > 开发 > PHP > 正文

php实现RSA加密类实例

2024-05-04 23:33:25
字体:
来源:转载
供稿:网友

这篇文章主要介绍了php实现RSA加密类,实例分析了php自定义RSA类实现加密与解密的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php实现RSA加密类。分享给大家供大家参考。具体分析如下:

通过openssl实现的签名、验签、非对称加解密,需要配合x.509证书(如crt和pem)文件使用。

由于各种原因,该类并不十分完善,欢迎各种测试!

  1. <?php  
  2. /** 
  3. * RSA算法类 
  4. * 签名及密文编码:base64字符串/十六进制字符串/二进制字符串流 
  5. * 填充方式: PKCS1Padding(加解密)/NOPadding(解密) 
  6. * 
  7. * Notice:Only accepts a single block. Block size is equal to the RSA key size!  
  8. * 如密钥长度为1024 bit,则加密时数据需小于128字节,加上PKCS1Padding本身的11字节信息,所以明文需小于117字节 
  9. * 
  10. * @author: linvo 
  11. * @version: 1.0.0 
  12. * @date: 2013/1/23 
  13. */ 
  14. class RSA{  
  15. private $pubKey = null;  
  16. private $priKey = null;  
  17. /** 
  18. * 自定义错误处理 
  19. */ 
  20. private function _error($msg){  
  21. die('RSA Error:' . $msg); //TODO  
  22. }  
  23. /** 
  24. * 构造函数 
  25. * 
  26. * @param string 公钥文件(验签和加密时传入) 
  27. * @param string 私钥文件(签名和解密时传入) 
  28. */ 
  29. public function __construct($public_key_file = ''$private_key_file = ''){ 
  30. if ($public_key_file){  
  31. $this->_getPublicKey($public_key_file);  
  32. }  
  33. if ($private_key_file){  
  34. $this->_getPrivateKey($private_key_file);  
  35. }  
  36. }  
  37. /** 
  38. * 生成签名 
  39. * 
  40. * @param string 签名材料 
  41. * @param string 签名编码(base64/hex/bin) 
  42. * @return 签名值 
  43. */ 
  44. public function sign($data$code = 'base64'){  
  45. $ret = false; 
  46. if (openssl_sign($data$ret$this->priKey)){  
  47. $ret = $this->_encode($ret$code); 
  48. return $ret;  
  49. /** 
  50. * 验证签名 
  51. * 
  52. * @param string 签名材料 
  53. * @param string 签名值 
  54. * @param string 签名编码(base64/hex/bin) 
  55. * @return bool  
  56. */ 
  57. public function verify($data$sign$code = 'base64'){  
  58. $ret = false; 
  59. $sign = $this->_decode($sign$code); 
  60. if ($sign !== false) { 
  61. switch (openssl_verify($data$sign$this->pubKey)){  
  62. case 1: $ret = true; break
  63. case 0:  
  64. case -1:  
  65. default$ret = false;  
  66. }  
  67. }  
  68. return $ret;  
  69. }  
  70. /** 
  71. * 加密 
  72. * 
  73. * @param string 明文 
  74. * @param string 密文编码(base64/hex/bin) 
  75. * @param int 填充方式(貌似php有bug,所以目前仅支持OPENSSL_PKCS1_PADDING) 
  76. * @return string 密文 
  77. */ 
  78. public function encrypt($data$code = 'base64'$padding = OPENSSL_PKCS1_PADDING){ 
  79. $ret = false;  
  80. if (!$this->_checkPadding($padding'en')) $this->_error('padding error'); 
  81. if (openssl_public_encrypt($data$result$this->pubKey, $padding)){ 
  82. $ret = $this->_encode($result$code); 
  83. }  
  84. return $ret;  
  85. }  
  86. /** 
  87. * 解密 
  88. * 
  89. * @param string 密文 
  90. * @param string 密文编码(base64/hex/bin) 
  91. * @param int 填充方式(OPENSSL_PKCS1_PADDING / OPENSSL_NO_PADDING) 
  92. * @param bool 是否翻转明文(When passing Microsoft CryptoAPI-generated RSA cyphertext, revert the bytes in the block) 
  93. * @return string 明文 
  94. */ 
  95. public function decrypt($data$code = 'base64'$padding = OPENSSL_PKCS1_PADDING, $rev = false){ 
  96. $ret = false; 
  97. $data = $this->_decode($data$code); 
  98. if (!$this->_checkPadding($padding'de')) $this->_error('padding error'); 
  99. if ($data !== false){  
  100. if (openssl_private_decrypt($data$result$this->priKey, $padding)){ 
  101. $ret = $rev ? rtrim(strrev($result), "/0") : ''.$result
  102. }  
  103. }  
  104. return $ret;  
  105. }  
  106. // 私有方法  
  107. /** 
  108. * 检测填充类型 
  109. * 加密只支持PKCS1_PADDING 
  110. * 解密支持PKCS1_PADDING和NO_PADDING 
  111.  
  112. * @param int 填充模式 
  113. * @param string 加密en/解密de 
  114. * @return bool 
  115. */ 
  116. private function _checkPadding($padding$type){ 
  117. if ($type == 'en'){  
  118. switch ($padding){  
  119. case OPENSSL_PKCS1_PADDING:  
  120. $ret = true;  
  121. break;  
  122. default:  
  123. $ret = false;  
  124. }  
  125. else {  
  126. switch ($padding){  
  127. case OPENSSL_PKCS1_PADDING:  
  128. case OPENSSL_NO_PADDING:  
  129. $ret = true;  
  130. break;  
  131. default:  
  132. $ret = false;  
  133. }  
  134. }  
  135. return $ret;  
  136. }  
  137. private function _encode($data$code){  
  138. switch (strtolower($code)){  
  139. case 'base64':  
  140. $data = base64_encode(''.$data); 
  141. break;  
  142. case 'hex':  
  143. $data = bin2hex($data);  
  144. break;  
  145. case 'bin':  
  146. default:  
  147. }  
  148. return $data;  
  149. }  
  150. private function _decode($data$code){  
  151. switch (strtolower($code)){  
  152. case 'base64':  
  153. $data = base64_decode($data);  
  154. break;  
  155. case 'hex':  
  156. $data = $this->_hex2bin($data);  
  157. break;  
  158. case 'bin':  
  159. default:  
  160. }  
  161. return $data;  
  162. }  
  163. private function _getPublicKey($file){  
  164. $key_content = $this->_readFile($file); 
  165. if ($key_content){  
  166. $this->pubKey = openssl_get_publickey($key_content); 
  167. }  
  168. }  
  169. private function _getPrivateKey($file){  
  170. $key_content = $this->_readFile($file);  
  171. if ($key_content){  
  172. $this->priKey = openssl_get_privatekey($key_content); 
  173. private function _readFile($file){ 
  174. $ret = false; 
  175. if (!file_exists($file)){ 
  176. $this->_error("The file {$file} is not exists"); 
  177. else { 
  178. $ret = file_get_contents($file); 
  179. return $ret
  180. }  
  181. private function _hex2bin($hex = false){  
  182. $ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i'$hex) ? pack("H*"$hex) : false; 
  183. return $ret;  
  184. }  


测试demo:

 

 
  1. <?php  
  2. header('Content-Type:text/html;Charset=utf-8;');  
  3. include "rsa.php";  
  4. echo '<pre>';  
  5. $a = isset($_GET['a']) ? $_GET['a'] : '测试123';  
  6. //////////////////////////////////////  
  7. $pubfile = 'E:/ssl/cert/pwd.crt';  
  8. $prifile = 'E:/ssl/cert/pwd.pem';  
  9. $m = new RSA($pubfile$prifile);  
  10. $x = $m->sign($a);  
  11. $y = $m->verify($a$x);  
  12. var_dump($x$y);  
  13. $x = $m->encrypt($a);  
  14. $y = $m->decrypt($x);  
  15. var_dump($x$y); 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表