PHP RSA asymmetric plus decryption class

Source: Internet
Author: User
Tags begin rsa private key decrypt openssl rsa

<?PHPHeader("Content-type:text/html;charset=utf-8");/*generate a public key, private key pair, the contents of the private key encryption can be decrypted through the public key (and vice versa) download the open source RSA key generation tool OpenSSL (usually the Linux system comes with the program), unzip to a separate folder, into the bin directory, execute the following command: OpenSSL Genrsa-out RSA_PRIVATE_KEY.PEM 1024x768 #生成原始 RSA private key file Rsa_private_key.pemopenssl pkcs8-topk8-inform pem-in Rsa_private_key . pem-outform pem-nocrypt-out PRIVATE_KEY.PEM #将原始 RSA private key conversion to PKCS8 format OpenSSL rsa-in rsa_private_key.pem-pubout-out Rsa_ PUBLIC_KEY.PEM #通过私钥生成对应 RSA public key Rsa_public_key.pem*/$private _key=-----BEGIN RSA PRIVATE KEY-----miicxqibaakbgqc3//sr2txw0wrc2dysx8vnglqt3y7ldu9+ lbli6e1ks5lfc5jltgf7kbtskchbm3ouehwqp1zj85ije59af5gib2klbd6h4wrbbha2xe1sq21ykja/gqx7/iria3zqfxgv/qekygox+ Xalvoolzqdwh76o2n1vp1d+ td3amhsk7qidaqabaogbakh14bmitesqd4pywodwmy7rrrvyfpenjjtecljvkb7ikrvxvdkp1xijngkh2h5syhq5qslpsgyj1m/ xkdnginwalvhvd3bokkgkg1bzn7ao5pxt+herqxavwws6ga63yvsic8jcodxiuvxjnumqrlaqof6aub/ 2vwc2t5mdmxlhakea3pwgpvxgliwl3h7qlyzlrlrbfrurn4cyl4uyaakokkavzly04glle8ycgoc2dzl4eil4l/+x/gaqdeju/ chlrqjbanozy0meovkwhu4bscsdnfm6usqowybewhyyh/otv1a3sqcce1f+ qbaclcqenihajccdmgyj53lfigyv0wcs54kcqaxapkahclrkqladquv5iwyyj25foiq+y8sgccs73qixru1ypjy9yka/meg9smsl4oh9ioigi+ zuygh9ydsmeq0cqqc24g3ip2g3lndrdzim5nz7pfnmyrabxk/ Ugvuwdk47iwtzhfkdhxkfc8qepuhbsahlqjifgxy4ejkubm3fpdgjakafwuxyssijjvrhwnhfbg0rfkvvy63osmnrxil4x6eyyi9lblcsyfpl25l7l5zmjrah n45zaioobrwqpm5edu7c-----END RSA PRIVATE KEY-----‘;$public _key=-----BEGIN Public KEY-----migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqc3//sr2txw0wrc2dysx8vnglqt3y7ldu9+ lbli6e1ks5lfc5jltgf7kbtskchbm3ouehwqp1zj85ije59af5gib2klbd6h4wrbbha2xe1sq21ykja/gqx7/iria3zqfxgv/qekygox+ Xalvoolzqdwh76o2n1vp1d+td3amhsk7qidaqab-----END Public KEY-----‘;file_put_contents(' Public_key.txt ',$public _key);file_put_contents(' Private_key.txt ',$private _key);/** * RSA asymmetric plus decryption*/classRSA {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] * @param [string] $private _key_file [private key file address] /c5>*/     Public function__construct ($public _key_file,$private _key_file) {        Try {            if(!file_exists($public _key_file) || !file_exists($private _key_file)) {                Throw New Exception(' key file no exists '); }            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 '); }        } Catch(Exception $e) {             die($e-getMessage ()); }        }    Private functionIs_bad_public_key ($public _key) {        returnOpenssl_pkey_get_public ($public _key); }    Private functionIs_bad_private_key ($private _key) {        returnOpenssl_pkey_get_private ($private _key); }    /** * Generate a pair of public and private keys successfully return a public-private key array failed to return false*/     Public functionCreate_key () {$res=openssl_pkey_new (); if($res==false)return false; Openssl_pkey_export ($res,$private _key); $public _key= Openssl_pkey_get_details ($res); return Array(' Public_key ' =$public _key["Key"], ' private_key ' =$private _key); }    /** * Encrypt with private key*/     Public functionPrivate_encrypt ($input) {Openssl_private_encrypt ($input,$output,$this-Private_key_resource); return Base64_encode($output); }    /** * Encrypt ciphertext after secret key is decrypted*/     Public functionPublic_decrypt ($input) {Openssl_public_decrypt (Base64_decode($input),$output,$this-Public_key_resource); return $output; }    /** * Encrypted with public key*/     Public functionPublic_encrypt ($input) {Openssl_public_encrypt ($input,$output,$this-Public_key_resource); return Base64_encode($output); }    /** * Decrypt the ciphertext after the public key is encrypted*/     Public functionPrivate_decrypt ($input) {Openssl_private_decrypt (Base64_decode($input),$output,$this-Private_key_resource); return $output; }}$rsa=NewRSA (' Public_key.txt ', ' private_key.txt ');$str= ' Guo Haiming ';$str=$rsa->public_encrypt ($str);//encrypt with public keyEcho $str, ' </br> ';$str=$rsa->private_decrypt ($str);//decrypt with private keyEcho $str, ' </br> ';//=============================================================$str=$rsa->private_encrypt ($str);//encrypt with a silk keyEcho $str, ' </br> ';$str=$rsa->public_decrypt ($str);//decrypt with a public keyEcho $str, ' </br> ';

PHP RSA asymmetric plus decryption class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.