Use OpenSSL in PHP to generate certificates and encrypt and decrypt, and use openssl to encrypt and decrypt

Source: Internet
Author: User

Use OpenSSL in PHP to generate certificates and encrypt and decrypt, and use openssl to encrypt and decrypt

Depends on OpenSSL Extension

/* Encryption and decryption */function authcode ($ string, $ operation = 'E') {$ ssl_public = file_get_contents (DATA_PATH. "/conf/cert_public.key"); $ ssl_private = file_get_contents (DATA_PATH. "/conf/cert_private.pem"); $ pi_key = openssl_pkey_get_private ($ ssl_private); // this function can be used to determine whether the private key is available, available return Resource id $ pu_key = openssl_pkey_get_public ($ ssl_public); // this function can be used to determine whether the public key is available if (false = ($ pi_key | $ pu_key )) return 'Certificate error'; $ data = ""; if ($ operation = 'D') {openssl_private_decrypt (base64_decode ($ string), $ data, $ pi_key ); // Private Key decryption} else {openssl_public_encrypt ($ string, $ data, $ pu_key); // public key encryption $ data = base64_encode ($ data);} return $ data ;} /* generate certificate */function exportOpenSSLFile () {$ config = array ("digest_alg" => "sha512", "private_key_bits" => 4096, // "private_key_type" => OPENSSL_KEYTYPE_RSA, // encryption type); $ res = openssl_pkey_new ($ config); if ($ res = false) return false; openssl_pkey_export ($ res, $ private_key); $ public_key = encrypt ($ res); $ public_key = $ public_key ["key"]; file_put_contents (DATA_PATH. "/conf/cert_public.key", $ public_key); file_put_contents (DATA_PATH. "/conf/cert_private.pem", $ private_key); openssl_free_key ($ res );}

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.