This article mainly introduces the PHP generated encryption public key encryption private key instances of the relevant information, the need for friends can refer to the following
PHP generates cryptographic public key cryptographic private key instance in detail
Generate public key private key win must be OPENSSL.COF support Liunx generally comes with the installation
$config = Array (//"digest_alg" = "sha512", "private_key_bits" = 512,//bytes 512 1024 2048 4096 etc. "Private_key_type" = Openssl_keytype_rsa,//encryption type); 1. Create a public and private key return resource $res = Openssl_pkey_new ($config); Obtain the private key from the obtained resource and assign the private key to the $privkeyopenssl_pkey_export ($res, $privKey); Obtain the private key from the obtained resource and assign the private key to $pubkey$pubkey = Openssl_pkey_get_details ($res); $pubKey = $pubKey ["Key"]; Var_dump (Array (' privkey ' = $privKey, ' pubkey ' = $pubKey)); Die 2. Encrypt and decrypt data to be encrypted $data = ' plaintext data goes here '; Encrypt the data string to encrypt the $data the public key Openssl_public_encrypt ($data, $encrypted, $pubKey) required for encrypted data encryption; echo Base64_encode ($encrypted); Decryption of the encrypted data to decrypt the decrypted data to obtain the decrypted data needed to decrypt the private key $decrypted = Base64_decode ($encrypted); Openssl_private_decrypt ($encrypted, $ decrypted, $privKey); Echo $decrypted; 3. One cryptographic decryption class, RSA {private $public _key = ";//Public key private $private _key =";//private key private $public _KEY_RESOURC e = "; 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] */publi C function construct ($public _key, $private _key) {$this->public_key = $public _key; $this->private_key = $private _key; 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 P Rivate key no usable '); }} Private Function Is_bad_public_key ($public _key) {return openssl_pkey_get_public ($public _key); } Private Function Is_bad_private_key ($private _key) {return openssl_pkey_get_private ($private _key); /** * Generate a pair of public and private keys to successfully return a public-private key array failed to return FALSE * * * Create_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); }/** * Encrypted with private key */Public Function Private_encrypt ($input) {openssl_private_encrypt ($input, $output, $this-> ;p Rivate_key_resource); Return Base64_encode ($output); /** * Decrypt secret key after encryption */Public Function Public_decrypt ($input) {Openssl_public_decrypt (Base64_decode ($input ), $output, $this->public_key_resource); return $output; /** * * * * * * * Public key Encryption * * PUBLIC_ENCRYPT ($input) {openssl_public_encrypt ($input, $output, $this->p Ublic_key_resource); Return Base64_encode ($output); }/** * Decrypt the public key after encrypting the ciphertext * * * PRIVATE_DECRYPT ($input) {Openssl_private_decrypt (Base64_decode ($INP UT), $output, $this->private_key_resource); return $output; }} $private _key = '-----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-----'; $public _key = '-----BEGIN public KEY-----migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqc3// SR2TXW0WRC2DYSX8VNGLQT 3y7ldu9+lbli6e1ks5lfc5jltgf7kbtskchbm3ouehwqp1zj85ije59af5gib2kl Bd6h4wrbbHA2XE1sq21ykja/ gqx7/iria3zqfxgv/qekygox+xalvoolzqdwh76o 2n1vp1d+td3amhsk7qidaqab-----END Public KEY-----'; $rsa = new RSA ($public _key, $private _key); $STR = ' encrypted decryption '; $str = $rsa->public_encrypt ($STR); Use the public key to encrypt the echo $str, ' </br> '; $str = $rsa->private_decrypt ($STR); Decrypt echo $str with private key, ' </br> '; ============================================================= $str = $rsa->private_encrypt ($STR); Encrypt echo $str with private key, ' </br> '; $str = $rsa->public_decrypt ($STR); Use the public key to decrypt the echo $str, ' </br> ';