Use of phprsa encryption and decryption, and use of rsa encryption and decryption _ PHP Tutorial

Source: Internet
Author: User
Tags openssl rsa
The use of phprsa encryption and decryption is detailed, and the use of rsa encryption and decryption is detailed. Explanation of use of phprsa encryption and decryption, and explanation of use of rsa encryption and decryption 1. the first step of encryption and decryption is to generate a public key and private key pair, the content encrypted by the private key can be decrypted through the public key (in turn, it can also be used in php rsa encryption and decryption instructions, and rsa encryption and decryption instructions

1,The first step of encryption and decryption is to generate a public key and private key pair. the content encrypted by the private key can be decrypted by 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:

The code is as follows:


Openssl genrsa-out rsa_private_key.pem 1024
Openssl pkcs8-topk8-inform PEM-in rsa_private_key.pem-outform PEM-nocrypt-out private_key.pem
Openssl 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, Directly add the code

The code is as follows:


$ 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. Resource id can be returned.
$ 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 content, pay attention to whether base64 encoding is url-safe during url transmission between networks.
Echo 'private key encrypted: '. $ encrypted. "\ n"; echo"
"; Echo"
";
Echo "public key decrypt: \ n"; echo"
";
Openssl_public_decrypt (base64_decode ($ encrypted), $ decrypted, $ pu_key); // The private key-encrypted content can be decrypted using the public key.
Echo 'public key decrypted: '. $ 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.

If you like encryption and decryption, you must take a good look at this article...

Http://www.bkjia.com/PHPjc/942984.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/942984.htmlTechArticlephp rsa encryption and decryption use details, rsa encryption and decryption use details 1. the first step of encryption and decryption is to generate a public key, private key pair, the content encrypted by the private key can be decrypted through the public key (in turn, it can also be...

Related Article

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.