php RSA 加解密实例
Posted lishalom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php RSA 加解密实例相关的知识,希望对你有一定的参考价值。
1 <?php 2 header("Content-Type: text/html;charset=utf-8"); 3 /* 4 5 生成公钥、私钥对,私钥加密的内容能通过公钥解密(反过来亦可以) 6 7 下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin目录,执行以下命令: 8 9 openssl genrsa -out rsa_private_key.pem 1024 #生成原始 RSA私钥文件 rsa_private_key.pem 10 openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out private_key.pem #将原始 RSA私钥转换为 pkcs8格式 11 openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem #通过私钥生成对应 RSA公钥 rsa_public_key.pem 12 13 */ 14 $private_key = ‘-----BEGIN RSA PRIVATE KEY----- 15 MIICXQIBAAKBgQC3//sR2tXw0wrC2DySx8vNGlqt3Y7ldU9+LBLI6e1KS5lfc5jl 16 TGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2klBd6h4wrbbHA2XE1sq21ykja/ 17 Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o2n1vP1D+tD3amHsK7QIDAQAB 18 AoGBAKH14bMitESqD4PYwODWmy7rrrvyFPEnJJTECLjvKB7IkrVxVDkp1XiJnGKH 19 2h5syHQ5qslPSGYJ1M/XkDnGINwaLVHVD3BoKKgKg1bZn7ao5pXT+herqxaVwWs6 20 ga63yVSIC8jcODxiuvxJnUMQRLaqoF6aUb/2VWc2T5MDmxLhAkEA3pwGpvXgLiWL 21 3h7QLYZLrLrbFRuRN4CYl4UYaAKokkAvZly04Glle8ycgOc2DzL4eiL4l/+x/gaq 22 deJU/cHLRQJBANOZY0mEoVkwhU4bScSdnfM6usQowYBEwHYYh/OTv1a3SqcCE1f+ 23 qbAclCqeNiHajCcDmgYJ53LfIgyv0wCS54kCQAXaPkaHclRkQlAdqUV5IWYyJ25f 24 oiq+Y8SgCCs73qixrU1YpJy9yKA/meG9smsl4Oh9IOIGI+zUygh9YdSmEq0CQQC2 25 4G3IP2G3lNDRdZIm5NZ7PfnmyRabxk/UgVUWdk47IwTZHFkdhxKfC8QepUhBsAHL 26 QjifGXY4eJKUBm3FpDGJAkAFwUxYssiJjvrHwnHFbg0rFkvvY63OSmnRxiL4X6EY 27 yI9lblCsyfpl25l7l5zmJrAHn45zAiOoBrWqpM5edu7c 28 -----END RSA PRIVATE KEY-----‘; 29 $public_key = ‘-----BEGIN PUBLIC KEY----- 30 MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3//sR2tXw0wrC2DySx8vNGlqt 31 3Y7ldU9+LBLI6e1KS5lfc5jlTGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2kl 32 Bd6h4wrbbHA2XE1sq21ykja/Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o 33 2n1vP1D+tD3amHsK7QIDAQAB 34 -----END PUBLIC KEY-----‘; 35 file_put_contents(‘public_key.txt‘, $public_key); 36 file_put_contents(‘private_key.txt‘, $private_key); 37 38 /** 39 * rsa 非对称加解密 40 */ 41 class rsa { 42 private $public_key = ‘‘; //公密钥 43 private $private_key = ‘‘; //私密钥 44 private $public_key_resource = ‘‘; //公密钥资源 45 private $private_key_resource = ‘‘; //私密钥资源 46 /** 47 * 架构函数 48 * @param [string] $public_key_file [公密钥文件地址] 49 * @param [string] $private_key_file [私密钥文件地址] 50 */ 51 public function __construct($public_key_file,$private_key_file) { 52 try { 53 if(!file_exists($public_key_file) || !file_exists($private_key_file)) { 54 throw new Exception(‘key file no exists‘); 55 } 56 if (false == ($this->public_key = file_get_contents($public_key_file)) || false == ($this->private_key = file_get_contents($private_key_file))) { 57 throw new Exception(‘read key file fail‘); 58 } 59 if(false == ($this->public_key_resource = $this->is_bad_public_key($this->public_key)) || false == ($this->private_key_resource = $this->is_bad_private_key($this->private_key))) { 60 throw new Exception(‘public key or private key no usable‘); 61 } 62 63 } catch (Exception $e) { 64 die($e->getMessage()); 65 } 66 } 67 private function is_bad_public_key($public_key) { 68 return openssl_pkey_get_public($public_key); 69 } 70 private function is_bad_private_key($private_key) { 71 return openssl_pkey_get_private($private_key); 72 } 73 /** 74 * 生成一对公私密钥 成功返回 公私密钥数组 失败 返回 false 75 */ 76 public function create_key() { 77 $res = openssl_pkey_new(); 78 if($res == false) return false; 79 openssl_pkey_export($res, $private_key); 80 $public_key = openssl_pkey_get_details($res); 81 return array(‘public_key‘=>$public_key["key"],‘private_key‘=>$private_key); 82 } 83 /** 84 * 用私密钥加密 85 */ 86 public function private_encrypt($input) { 87 openssl_private_encrypt($input,$output,$this->private_key_resource); 88 return base64_encode($output); 89 } 90 /** 91 * 解密 私密钥加密后的密文 92 */ 93 public function public_decrypt($input) { 94 openssl_public_decrypt(base64_decode($input),$output,$this->public_key_resource); 95 return $output; 96 } 97 /** 98 * 用公密钥加密 99 */ 100 public function public_encrypt($input) { 101 openssl_public_encrypt($input,$output,$this->public_key_resource); 102 return base64_encode($output); 103 } 104 /** 105 * 解密 公密钥加密后的密文 106 */ 107 public function private_decrypt($input) { 108 openssl_private_decrypt(base64_decode($input),$output,$this->private_key_resource); 109 return $output; 110 } 111 } 112 113 $rsa = new rsa(‘public_key.txt‘,‘private_key.txt‘); 114 115 $str = ‘待加密字符‘; 116 $str = $rsa->public_encrypt($str); //用公密钥加密 117 echo $str,‘</br>‘; 118 $str = $rsa->private_decrypt($str); //用私密钥解密 119 echo $str,‘</br>‘; 120 //============================================================= 121 $str = $rsa->private_encrypt($str); //用丝密钥加密 122 echo $str,‘</br>‘; 123 $str = $rsa->public_decrypt($str); //用公密钥解密 124 echo $str,‘</br>‘;
以上是关于php RSA 加解密实例的主要内容,如果未能解决你的问题,请参考以下文章