This time to bring you the RSA cipher encryption and decryption details, RSA cipher encryption and decryption of the notes are what, the following is the actual case, take a look.
As shown below:
<?phpnamespace helpers;class opensslrsa{//echo $private _key private key; public $private _key = '-----BEGIN RSA private Key----- miicxqibaakbgqc+l7enzbhxkoqjufhpfklat40batvzhuahw/ g05xshptgqm9rv8wg0eabfbdo9pub8dixdpqlyifaqktgpspjj1ow7wkxmybqzn5iw/gn+tyfwp+ mb2w6iblpwbdval2njlmu8j3lcpjw1dh4zp1oitkxzmyuuuyyfpuoto9rj2widaqabaogaufcbmjqbt7jtxgfmrgkzqldc2mjg7rks3tsmmhpm8ujtwvqjr9m terl7iqxncu4wrrnc0jcds1sca9n/wdt4fckcala+bg7mwquppg5qhxelffr88ibrnp8y8lmz7ppnqx9c4jivhmzjrznh3luqg6awjsig2w3+ ew1/ubb30aecqqdshtvyc5mpdgio4g8q1ztszszl9ecp+ijluan51vc3nj1expjbtdsz0jvkrddhkcd3rezvyzmqn/lipyq85e/ bakeazdmn6tf3y1h3louumcy6+61chtfrl/yjw13cgapmaqhhevyanhr7njoxp06eimzn7khff/ eyxd1emf1sya8uaqjban1ibfuplrgxaz20lnw9r+rnutxizjlublcxtjv6g0bylykkzguqy7/zhbpsfl4gncubbkhh/ obebaa6kh9vfmccqgfg0wxmoim4ewy7sg+6oue+ncl5hyklsz7boybgohlpqvpjg6j4jq1g0hnscu9xhdg0f8vl/ RXCFLH41AKFOAECQQCR8NDB3BGHQYJFARKKMWQ3QRXHALFBKEXMRPQ8MDZSMLSBZFONUCUFO110LSGJDRLGR8SMTU2HX9GXFIQXVFWF-----END RSA PRIVATE KEY-----'; Public key $public _key = '-----beGIN Public KEY-----migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqc+l7enzbhxkoqjufhpfklat40batvzhuahw/ g05xshptgqm9rv8wg0eabfbdo9pub8dixdpqlyifaqktgpspjj1ow7wkxmybqzn5iw/gn+tyfwp+ Mb2w6iblpwbdval2njlmu8j3lcpjw1dh4zp1oitkxzmyuuuyyfpuoto9rj2widaqab-----END Public KEY-----';
Public $pi _key; Public $pu _key; Determines whether the public function construct () {$this->pi_key = openssl_pkey_get_private ($this->private_key) is available for the publicly and private keys;// This function can be used to determine if the private key is available, return the resource ID Resource ID $this->pu_key = openssl_pkey_get_public ($this->public_key);// This function can be used to determine if the public key is available//Print_r ($this->pi_key); echo "\ n"; Print_r ($this->pu_key); echo "\ n"; }//private key encryption/*public function Privateencrypt ($data) {openssl_private_encrypt ($data, $encrypted, $this->pi_key); $encrypted = $this->urlsafe_b64encode ($encrypted);//The encrypted content usually contains special characters that need to be encoded under conversion, to be aware that Base64 encoding is URL-safe when transferring over the network through a URL return $encrypted; }*/Public Function Privateencrypt ($data) {//Openssl_private_encrypt ($data, $encrypted, $this->pi_key); $crypto = "; foreach (Str_split ($data, 117) as $chunk) {Openssl_private_encrypt ($chunk, $encryptData, $this->pi_key); $crypto. = $encryptData; } $encrypted = $this->urlsafe_b64encode ($crypto);//The encrypted content usually contains special characters that need to be encoded for conversion, to be aware that Base64 encoding is URL-safe when transferring over the network through a URL return $encrypted; }
Add the password when the special symbol is replaced with the URL can be with the content function Urlsafe_b64encode ($string) {$data = Base64_encode ($string); $data = str_replace (Array (' + ', '/', ' = '), array ('-', ' _ ', '), $data); return $data; }//To replace the converted symbol with the special symbol function Urlsafe_b64decode ($string) {$data = Str_replace (Array ('-', ' _ '), Array (' + ', '/'), $string ); $mod 4 = strlen ($data)% 4; if ($mod 4) {$data. = substr (' = = = ', $mod 4); } return Base64_decode ($data); The contents of the private key encryption can be decrypted by public function Publicdecrypt ($encrypted) {//$encrypted = $this->urlsafe_b64decode ($ encrypted); $crypto = "; foreach (Str_split ($this->urlsafe_b64decode ($encrypted), $) as $chunk) {Openssl_public_decrypt ($chunk, $ Decryptdata, $this->pu_key); $crypto. = $decryptData; }//openssl_public_decrypt ($encrypted, $decrypted, $this->pu_key);//The contents of the private key encryption can be decrypted by the public key to return $crypto; }//Public key Encryption (Publicencrypt) ($data) {//openssl_public_encrypt ($data, $encrypted, $this->pu_key);//public key encryption $ Crypto = "; foreach (Str_split ($data, 117) as $chunk) {OPenssl_public_encrypt ($chunk, $encryptData, $this->pu_key); $crypto. = $encryptData; } $encrypted = $this->urlsafe_b64encode ($crypto); return $encrypted; }//private key decryption public function Privatedecrypt ($encrypted) {$crypto = '; foreach (Str_split ($this->urlsafe_b64decode ($encrypted), $) as $chunk) {Openssl_private_decrypt ($chunk, $ Decryptdata, $this->pi_key); $crypto. = $decryptData; }//$encrypted = $this->urlsafe_b64decode ($encrypted); Openssl_private_decrypt ($encrypted, $decrypted, $this->pi_key); return $crypto; }}
Don't need me to write more, there are problems can contact me directly.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
How the PHP generator uses
phpstudy2018 Access to directory service permissions
thinkphp Implementation Payment (JSAPI payment) process Tutorial _php instance