国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php實現(xiàn)RSA加密類實例

2024-05-04 23:33:25
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php實現(xiàn)RSA加密類,實例分析了php自定義RSA類實現(xiàn)加密與解密的技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php實現(xiàn)RSA加密類。分享給大家供大家參考。具體分析如下:

通過openssl實現(xiàn)的簽名、驗簽、非對稱加解密,需要配合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,則加密時數(shù)據(jù)需小于128字節(jié),加上PKCS1Padding本身的11字節(jié)信息,所以明文需小于117字節(jié) 
  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. * 構造函數(shù) 
  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程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 英超| 普陀区| 舞阳县| 犍为县| 东兴市| 佛学| 榆社县| 卓资县| 正蓝旗| 华容县| 泾源县| 南皮县| 湘潭县| 连州市| 桐庐县| 渭南市| 贵南县| 永丰县| 浑源县| 五家渠市| 东城区| 龙川县| 鄂托克旗| 河北省| 上高县| 洛川县| 休宁县| 宝兴县| 页游| 枝江市| 嘉兴市| 恩施市| 塔河县| 西乌珠穆沁旗| 海晏县| 莎车县| 金川县| 临武县| 唐山市| 堆龙德庆县| 通榆县|