More about PHP using the OpenSSL extension for public key cryptography

Source: Internet
Author: User
Tags decrypt openssl rsa
generating public and private keys using the OpenSSL command

Generate private Key # OpenSSL genrsa-out Rsa_private_key.pem 1024//generate public Key # OpenSSL rsa-in rsa_private_key.pem-pubout-out rsa_public_ Key.pem


Here is the sample code:

<?php//OpenSSL extended Detection var_dump (extension_loaded (' OpenSSL ')); $prikey = Openssl_pkey_get_private (file_get_contents ( ' Rsa_private_key.pem ')); Private key $pubkey = Openssl_pkey_get_public (file_get_contents (' Rsa_public_key.pem ')); Public key//plaintext data $data = ' test-string! '; * * Issues that may arise: Don ' t know how to get public key from the This private key * cause: PHP's OpenSSL extension and Apache inconsistency cause, of course, running the program at the command line will not appear This issue *///public key cryptography $encrypt_data = "; Openssl_public_encrypt ($data, $encrypt _data, $pubkey); $encrypt _data = Base64_encode ($ Encrypt_data); Echo $encrypt _data;echo ' <br/> ';//---------------------------------------------------------- --//private key Decryption $encrypt_data = Base64_decode ($encrypt _data); Openssl_private_decrypt ($encrypt _data, $decrypt _data, $prikey ); Var_dump ($decrypt _data);



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.