PHP的Openssl进行RSA加密与解密范例_第1页
PHP的Openssl进行RSA加密与解密范例_第2页
PHP的Openssl进行RSA加密与解密范例_第3页
PHP的Openssl进行RSA加密与解密范例_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/index.php?php/需要配置PHP.INI文件,打开extension=php_openssl.dllincludersaclass.php;/以下是一个简单的测试demo,如果不需要请删除$rsa=newRsa(D:rsa);/放项目的PHP目录/私钥加密,公钥解密echosource:Testing:HelloWorld!;$pre=$rsa-privEncrypt(Testing:HelloWorld!);echoPrivateEncrypted:.$pre.;$pud=$rsa-pubDecrypt($pre);echoPublicDecrypted:.$pud.;/公钥加密

2、,私钥解密echosource:workinginhere!;$pue=$rsa-pubEncrypt(workinginhere!);echoPublicEncrypt:.$pue.;$prd=$rsa-privDecrypt($pue);echoPrivateDecrypt:.$prd;?/rsaclass.php_keyPath=$path;*createthekeypair,savethekeyto$this-_keyPath*/publicfunctioncreateKey()$r=openssl_pkey_new();openssl_pkey_export($r,$privKey)

3、;file_put_contents($this-_keyPath.DIRECTORY_SEPARATOR.key.pem,$privKey);$this-_privKey=openssl_pkey_get_public($privKey);$rp=openssl_pkey_get_details($r);$pubKey=$rpkey;file_put_contents($this-_keyPath.DIRECTORY_SEPARATOR.pub.pem,$pubKey);$this-_pubKey=openssl_pkey_get_public($pubKey);/*setupthepriv

4、atekey*/publicfunctionsetupPrivKey()if(is_resource($this-_privKey)returntrue;$file=$this-_keyPath.DIRECTORY_SEPARATOR.key.pem;$prk=file_get_contents($file);$this-_privKey=openssl_pkey_get_private($prk);returntrue;/*setupthepublickey*/publicfunctionsetupPubKey()if(is_resource($this-_pubKey)returntrue

5、;$file=$this-_keyPath.DIRECTORY_SEPARATOR.pub.pem;$puk=file_get_contents($file);$this-_pubKey=openssl_pkey_get_public($puk);returntrue;*encryptwiththeprivatekey*/publicfunctionprivEncrypt($data)if(!is_string($data)returnnull;$this-setupPrivKey();$r=openssl_private_encrypt($data,$encrypted,$this-_pri

6、vKey);if($r)returnbase64_encode($encrypted);returnnull;/*decryptwiththeprivatekey*/publicfunctionprivDecrypt($encrypted)if(!is_string($encrypted)returnnull;$this-setupPrivKey();$encrypted=base64_decode($encrypted);$r=openssl_private_decrypt($encrypted,$decrypted,$this-_privKey);if($r)return$decrypte

7、d;returnnull;/*encryptwithpublickey*/publicfunctionpubEncrypt($data)if(!is_string($data)returnnull;$this-setupPubKey();$r=openssl_public_encrypt($data,$encrypted,$this-_pubKey);if($r)returnbase64_encode($encrypted);returnnull;/*decryptwiththepublickey*/publicfunctionpubDecrypt($crypted)if(!is_string

8、($crypted)returnnull;$this-setupPubKey();$crypted=base64_decode($crypted);$r=openssl_public_decrypt($crypted,$decrypted,$this-_pubKey);if($r)return$decrypted;returnnull;publicfunction_destruct()fclose($this-_privKey);fclose($this-_pubKey);?加密使用公钥,解密使用私钥签名使用私钥,验证使用公钥私钥可以导出公钥I产牛私钥I,指定位数1024,公钥指数e,通常是3,17,65537$opensslg

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论