Usage of phprsa encryption and decryption

Source: Internet
Author: User
This article describes in detail how to use phprsa for encryption and decryption. For more information, see section 1. the first step of encryption and decryption is to generate public and private key pairs, private key-encrypted content can be decrypted through the public key (or vice versa)

Download the open-source RSA key generation tool openssl (usually the program is provided in Linux), decompress it to an independent folder, enter the bin directory, and execute the following command:

openssl genrsa -out rsa_private_key.pem 1024openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out private_key.pemopenssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

The first command generates the original RSA private key file rsa_private_key.pem. the second command converts the original RSA private key to pkcs8. The third command generates the RSA public key rsa_public_key.pem.
We can see from the above that the corresponding public key can be generated through the private key, so we use the private key private_key.pem on the server side, and the public key is issued to the android and ios frontend.

2. use the generated public key and private key in php for encryption and decryption, and directly upload the code

$ Fp = fopen ("rsa/rsa_private_key.pem", "r"); // path of your private key file $ private_key = fread ($ fp, 8192 ); fclose ($ fp); $ fp1 = fopen ("rsa/rsa_public_key.pem", "r"); // path of your public key file $ public_key = fread ($ fp1, 8192 ); fclose ($ fp1); // echo $ private_key; $ pi_key = openssl_pkey_get_private ($ private_key); // This function can be used to determine whether the private key is available, you can return the Resource id $ pu_key = openssl_pkey_get_public ($ public_key); // This function can be used to determine whether the public key is available print_r ($ pi_key); echo "\ n"; echo"
"; Print_r ($ pu_key); echo" \ n "; echo"
"; Echo" "; $ data = 'php ras encryption algorithmic '; $ encrypted =" "; $ decrypted =" "; echo" encrypted source data :". $ data. "\ n"; echo"
"; Echo" private key encrypt: \ n "; echo"
"; Openssl_private_encrypt ($ data, $ encrypted, $ pi_key); // private key encryption $ encrypted = base64_encode ($ encrypted); // The encrypted content usually contains special characters, if you need to encode and convert the data, you must note whether base64 encoding is url-safe echo 'encrypted private key :'. $ encrypted. "\ n"; echo"
"; Echo"
"; Echo" public key decrypt: \ n "; echo"
"; Openssl_public_decrypt (base64_decode ($ encrypted), $ decrypted, $ pu_key); // The private key can be decrypted using the public key. echo 'public key :'. $ decrypted. "\ n"; echo"
"; Echo" "; echo" public key encrypt: \ n "; echo"
"; Openssl_public_encrypt ($ data, $ encrypted, $ pu_key); // public key encryption $ encrypted = base64_encode ($ encrypted); echo $ encrypted," \ n "; echo"
"; Echo" private key decrypt: \ n "; echo"
"; Openssl_private_decrypt (base64_decode ($ encrypted), $ decrypted, $ pi_key); // private key decryption echo $ decrypted," \ n "; echo"
";

Php rsa configuration FAQ:

● Usage of three DLL files in the openssl folder in the PHP development language code example

1. if your system is windows and the system32 file directory does not contain libeay32.dll or ssleay32.dll
Copy these two files to the system32 directory.

2. if php_openssl.dll is not found in the php installation directory (php \ ext ),

Put php_openssl.dll in this folder.

Little friends who like encryption and decryption must take a good look at this article, which has benefited a lot.


For more details about the use of php rsa encryption and decryption, refer to the PHP Chinese network!

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.