Phppki encryption technology (openssl) _ PHP Tutorial

Source: Internet
Author: User
Tags sha1 hash
Phppki encryption technology (openssl. Copy the code as follows :? Phppki encryption to use pki encryption requires openssl extension php. iniextensionphp_openssl.dll extension * pki mode is * public key encryption, private key decryption; The code is as follows:


// Pki encryption
// Openssl extension must be enabled for pki encryption.
// Php. ini extension = php_openssl.dll extension
/* The pki mode is
* Public key encryption and private key decryption;
* Private key encryption and public key decryption;
*/
// Private key encryption and public key decryption
// Client
// $ Data
$ Data = 'ABCD ';
// Obtain the private key $ priv_key_id
$ Priv_key_id = openssl_get_privatekey (file_get_contents ('99bill-rsa. pem', r ));
// Get the public key $ pub_key_id
$ Pub_key_id = openssl_get_publickey (file_get_contents ('99bill-rsa. cer ', r ));
// $ Data is encrypted by SHA1 hash first, and then encrypted by $ priv_key_id private key to generate a signature $ signature
// $ Signature is the encrypted signature
// Openssl_sign () encryption function. I don't know how to decrypt it ??????????????????????
Openssl_sign ($ data, $ signature, $ priv_key_id, OPENSSL_ALGO_SHA1 );
// There are two encryption functions available, and these two encryption functions have the decryption method.
// Type 1: Private key encryption and public key decryption
// $ Data indicates the data to be encrypted. $ crypted indicates the data generated by encryption, and $ decrypted indicates the data generated by decryption. $ data has the same value as $ decrypted.
// Encrypt with the $ priv_key_id private key to generate $ crypted;
Openssl_private_encrypt ($ data, $ crypted, $ priv_key_id );
Echo $ crypted;
// Use the $ pub_key_id public key to decrypt and generate $ decrypted
Openssl_public_decrypt ($ crypted, $ decrypted, $ pub_key_id );
// Type 2: Public key encryption and private key decryption
// $ Data indicates the data to be encrypted. $ crypted indicates the data generated by encryption, and $ decrypted indicates the data generated by decryption. $ data has the same value as $ decrypted.
// Generate $ crypted by encrypting the $ pub_key_id public key;
Openssl_public_encrypt ($ data, $ crypted, $ pub_key_id );
// Use the $ priv_key_id private key to decrypt and generate $ decrypted
Openssl_private_decrypt ($ crypted, $ decrypted, $ priv_key_id );
// Note: The public key obtained here does not correspond to the private key file.
// Normally, the obtained public key corresponds to the private key file one by one. here I use quick money.
// Quick money generates a file for the private key, and the corresponding public key generated file is on the quick money side
// Quick money generates a file for the public key, and the corresponding private key generated file is on the quick money side
// A public key and a private key are missing.
// I have never found a one-to-one private key and public key to generate a file. if you find one, send it to me. thank you.
// Openssl_verify () method is used to verify whether the signature is correct (the data generated by private key encryption is returned and verified by the corresponding public key.
// $ Signature: the data generated by public key encryption. $ the original data is returned. 1 is returned for success, 0 is returned for failure, and-1 is returned for error.
// $ Pub_key_id public key
Openssl_verify ($ data, $ signature, $ pub_key_id );
// Release the private key or public key from the memory
Openssl_free_key ($ priv_key_id );
Openssl_free_key ($ pub_key_id );


Generate private key and public key
Genrsa-out private-rsa.pem
Rsa-in private-rsa.pem-pubout-out pubic-rsa.cer

The http://www.bkjia.com/PHPjc/327971.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327971.htmlTechArticle code is as follows :? Php // pki encryption // openssl extension must be enabled when pki encryption is used. // php. ini extension = php_openssl.dll extension/* The pki mode is * public key encryption and private key decryption ;...

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.