1<?PHP2 Header("Content-type:text/html;charset=utf-8");3 /*4 5 generates a public key, private key pair, and private key encrypted content can be decrypted by the public key (and vice versa)6 7 Download the open source RSA key generation tool OpenSSL (usually the Linux system comes with the program), unzip to a separate folder, enter the Bin directory, and execute the following command:8 9 OpenSSL genrsa-out rsa_private_key.pem 1024x768 #生成原始 RSA private key file Rsa_private_key.pemTen OpenSSL pkcs8-topk8-inform pem-in rsa_private_key.pem-outform pem-nocrypt-out PRIVATE_KEY.PEM #将原始 RSA private key converted to PKCS 8 Format One OpenSSL rsa-in rsa_private_key.pem-pubout-out RSA_PUBLIC_KEY.PEM #通过私钥生成对应 RSA public key Rsa_public_key.pem A - */ - $private _key=-----BEGIN RSA PRIVATE KEY----- the MIICXQIBAAKBGQC3//SR2TXW0WRC2DYSX8VNGLQT3Y7LDU9+LBLI6E1KS5LFC5JL - tgf7kbtskchbm3ouehwqp1zj85ije59af5gib2klbd6h4wrbbha2xe1sq21ykja/ - Gqx7/iria3zqfxgv/qekygox+xalvoolzqdwh76o2n1vp1d+td3amhsk7qidaqab - Aogbakh14bmitesqd4pywodwmy7rrrvyfpenjjtecljvkb7ikrvxvdkp1xijngkh + 2H5SYHQ5QSLPSGYJ1M/XKDNGINWALVHVD3BOKKGKG1BZN7AO5PXT+HERQXAVWWS6 - GA63YVSIC8JCODXIUVXJNUMQRLAQOF6AUB/2VWC2T5MDMXLHAKEA3PWGPVXGLIWL + 3h7qlyzlrlrbfrurn4cyl4uyaakokkavzly04glle8ycgoc2dzl4eil4l/+x/gaq A deju/chlrqjbanozy0meovkwhu4bscsdnfm6usqowybewhyyh/otv1a3sqcce1f+ at qbaclcqenihajccdmgyj53lfigyv0wcs54kcqaxapkahclrkqladquv5iwyyj25f - Oiq+y8sgccs73qixru1ypjy9yka/meg9smsl4oh9ioigi+zuygh9ydsmeq0cqqc2 - 4g3ip2g3lndrdzim5nz7pfnmyrabxk/ugvuwdk47iwtzhfkdhxkfc8qepuhbsahl - Qjifgxy4ejkubm3fpdgjakafwuxyssijjvrhwnhfbg0rfkvvy63osmnrxil4x6ey - yi9lblcsyfpl25l7l5zmjrahn45zaioobrwqpm5edu7c ------END RSA PRIVATE KEY-----'; in $public _key=-----BEGIN Public KEY----- - MIGFMA0GCSQGSIB3DQEBAQUAA4GNADCBIQKBGQC3//SR2TXW0WRC2DYSX8VNGLQT to 3y7ldu9+lbli6e1ks5lfc5jltgf7kbtskchbm3ouehwqp1zj85ije59af5gib2kl + bd6h4wrbbha2xe1sq21ykja/gqx7/iria3zqfxgv/qekygox+xalvoolzqdwh76o - 2n1vp1d+td3amhsk7qidaqab the-----END Public KEY-----'; * file_put_contents(' Public_key.txt ',$public _key); $ file_put_contents(' Private_key.txt ',$private _key);Panax Notoginseng - /** the * RSA asymmetric plus decryption + */ A classRSA { the Private $public _key= ";//Public Key + Private $private _key= ";//private Key - Private $public _key_resource= ";//Public Key Resource $ Private $private _key_resource= ";//private Key Resource $ /** - * Schema function - * @param [string] $public _key_file [public key file address] the * @param [string] $private _key_file [private key file address] - */Wuyi Public function__construct ($public _key_file,$private _key_file) { the Try { - if(!file_exists($public _key_file) || !file_exists($private _key_file)) { Wu Throw New Exception(' key file no exists '); - } About if(false== ($this->public_key =file_get_contents($public _key_file)) ||false== ($this->private_key =file_get_contents($private _key_file))) { $ Throw New Exception(' read key file Fail '); - } - 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))) { - Throw New Exception(' Public key or private key no usable '); A } + the}Catch(Exception $e) { - die($e-getMessage ()); $ } the } the Private functionIs_bad_public_key ($public _key) { the returnOpenssl_pkey_get_public ($public _key); the } - Private functionIs_bad_private_key ($private _key) { in returnOpenssl_pkey_get_private ($private _key); the } the /** About * Generate a pair of public and private keys to successfully return a public-private key array failed to return false the */ the Public functionCreate_key () { the $res=openssl_pkey_new (); + if($res==false)return false; -Openssl_pkey_export ($res,$private _key); the $public _key= Openssl_pkey_get_details ($res);Bayi return Array(' Public_key ' =$public _key["Key"], ' private_key ' =$private _key); the } the /** - * Encrypt with private key - */ the Public functionPrivate_encrypt ($input) { theOpenssl_private_encrypt ($input,$output,$this-private_key_resource); the return Base64_encode($output); the } - /** the * Encrypt ciphertext after decrypting private key the */ the Public functionPublic_decrypt ($input) {94Openssl_public_decrypt (Base64_decode($input),$output,$this-public_key_resource); the return $output; the } the /**98 * Encrypted with public key About */ - Public functionPublic_encrypt ($input) {101Openssl_public_encrypt ($input,$output,$this-public_key_resource);102 return Base64_encode($output);103 }104 /** the * Decrypt the ciphertext after the public key is encrypted106 */107 Public functionPrivate_decrypt ($input) {108Openssl_private_decrypt (Base64_decode($input),$output,$this-private_key_resource);109 return $output; the }111 } the 113 $rsa=NewRSA (' Public_key.txt ', ' private_key.txt '); the the $str= ' Encrypt character '; the $str=$rsa->public_encrypt ($str);//encrypt with public key117 Echo $str, ' </br> ';118 $str=$rsa->private_decrypt ($str);//decrypt with private key119 Echo $str, ' </br> '; - //=============================================================121 $str=$rsa->private_encrypt ($str);//encrypt with a silk key122 Echo $str, ' </br> ';123 $str=$rsa->public_decrypt ($str);//decrypt with a public key124 Echo $str, ' </br> ';
PHP RSA Plus decryption instance