<?php header('Content-Type:text/html;charset=utf-8'); //RSA加密解密实例 $private_key = <<<EOF -----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQC3//sR2tXw0wrC2DySx8vNGlqt3Y7ldU9+LBLI6e1KS5lfc5jl TGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2klBd6h4wrbbHA2XE1sq21ykja/ Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o2n1vP1D+tD3amHsK7QIDAQAB AoGBAKH14bMitESqD4PYwODWmy7rrrvyFPEnJJTECLjvKB7IkrVxVDkp1XiJnGKH 2h5syHQ5qslPSGYJ1M/XkDnGINwaLVHVD3BoKKgKg1bZn7ao5pXT+herqxaVwWs6 ga63yVSIC8jcODxiuvxJnUMQRLaqoF6aUb/2VWc2T5MDmxLhAkEA3pwGpvXgLiWL 3h7QLYZLrLrbFRuRN4CYl4UYaAKokkAvZly04Glle8ycgOc2DzL4eiL4l/+x/gaq deJU/cHLRQJBANOZY0mEoVkwhU4bScSdnfM6usQowYBEwHYYh/OTv1a3SqcCE1f+ qbAclCqeNiHajCcDmgYJ53LfIgyv0wCS54kCQAXaPkaHclRkQlAdqUV5IWYyJ25f oiq+Y8SgCCs73qixrU1YpJy9yKA/meG9smsl4Oh9IOIGI+zUygh9YdSmEq0CQQC2 4G3IP2G3lNDRdZIm5NZ7PfnmyRabxk/UgVUWdk47IwTZHFkdhxKfC8QepUhBsAHL QjifGXY4eJKUBm3FpDGJAkAFwUxYssiJjvrHwnHFbg0rFkvvY63OSmnRxiL4X6EY yI9lblCsyfpl25l7l5zmJrAHn45zAiOoBrWqpM5edu7c -----END RSA PRIVATE KEY----- EOF; $public_key = '-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3//sR2tXw0wrC2DySx8vNGlqt 3Y7ldU9+LBLI6e1KS5lfc5jlTGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2kl Bd6h4wrbbHA2XE1sq21ykja/Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o 2n1vP1D+tD3amHsK7QIDAQAB -----END PUBLIC KEY-----'; //签名 function sign($data, $sign, $private_key){ $d = openssl_sign($data, $sign, $private_key ,'SHA256'); $sign = base64_encode($sign); return $sign; } //验证 function verify($data, $sign, $public_key){+ $sign = base64_decode($sign); $v = openssl_verify($data, $sign, $public_key ,'SHA256'); return $v; } //私钥加密 function private_encrypt($data,$encrypted,$pi_key){ openssl_private_encrypt($data,$encrypted,$pi_key); $encrypted = base64_encode($encrypted); return $encrypted; } //公钥解密 function public_decrypt($data,$decrypted,$pu_key){ openssl_public_decrypt(base64_decode($data),$decrypted,$pu_key); return $decrypted; } //公钥加密 function public_encrypt($data,$encrypted,$pu_key){ openssl_public_encrypt($data,$encrypted,$pu_key); $encrypted = base64_encode($encrypted); return $encrypted; } //私钥解密 function private_decrypt($data,$decrypted,$pi_key){ openssl_private_decrypt(base64_decode($data),$decrypted,$pi_key); return $decrypted; } $sign = ''; $encrypted = ''; $decrypted = ''; $pi_key = openssl_pkey_get_private($private_key); $pu_key = openssl_pkey_get_public($public_key); /*---------签名-------------*/ $data = 'AAAAA'; echo $data.'<br>签名:<br>'; echo sign($data, $sign, $private_key); /*---------签名-------------*/ echo '<hr>'; /*---------验签-------------*/ $data = 'BBBBB'; $sign = 'dMHW4RooOpbUw8DYubIvkQskmGKG7jho0Lhp6FmHe/m28xzgLOLHAw+8zs+GzalYVGQYQ3cUbd4qCbT4uDG2I5cubC4R8fVPUItrt233d3YMVkacnNKWnoR9VeoEaS2HO6VrYBehXNVW6reGoOOJiqYLn/aWtYx84HbMKRKWRtg='; echo $data.'和'.$sign.'<br>验签:<br>'; echo verify($data, $sign, $public_key); /*---------验签-------------*/ echo '<hr><hr>'; /*---------私钥加密-------------*/ $data = 'CCCCCCCC'; echo $data.'<br>私钥加密:<br>'; echo private_encrypt($data, $encrypted, $pi_key); /*---------私钥加密-------------*/ echo '<hr>'; /*---------公钥解密-------------*/ $data = 'BvjbtUzDKzZ2UbtWIFy7KPNJJGpDnS5Qd+w/kpYSFTg8l1kz+DqvCqfkBho2TRj8S5SFfy4DmFxyPnvpIhhUMC6bCMCOD8ULzk20rhqs7UZfX1OoTx9+IE7MO3slM91OqquO2HILHiiihIVBBn/l/0zAzH6GIC44LXJQUKJMDYI='; echo $data.'<br>公钥解密:<br>'; echo public_decrypt($data, $decrypted, $pu_key); /*---------公钥解密-------------*/ echo '<hr><hr>'; /*---------公钥加密-------------*/ $data = 'AAAAAAAAAAAAAAAAAAAA'; echo $data.'<br>公钥加密:<br>'; echo public_encrypt($data, $encrypted, $pu_key); /*---------公钥加密-------------*/ echo '<hr>'; /*---------私钥解密-------------*/ $data = 'JZ2aRDoeiO1xzPFAhjbtJNMRRqZwrjjPK9d0dgRVtlMhWucHu6jrQR9QRETtM6eQgkrdvbxeMtJ87e6YmptYZUZZjALymT0yU7VO5aAVBKXnmyokqqfuJF41PYS6bSih/VFaIoanrTZGUf03suurjkbebx3yKLIBPdEgly6eM/k='; echo $data.'<br>私钥解密:<br>'; echo private_decrypt($data,$decrypted,$pi_key); /*---------私钥解密-------------*/ echo '<hr>';
标签:加密,data,RSA,echo,-------------,解密,PHP,--------- From: https://www.cnblogs.com/zhouein/p/17789172.html