How to use RSA to implement encryption and decryption code in PHP

Source: Internet
Author: User
Tags fread
1. The first step in encrypting decryption is to generate a public key, a private key pair, and a private key to encrypt the contents through the public key (can be reversed)

Download the open source RSA key generation tool OpenSSL (usually the Linux system comes with the program), unzip to a separate folder, enter the Bin directory, and execute the following command:

The code is as follows:

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 the PKCS8 format, and the third generates the RSA public key Rsa_public_key.pem
From the above, we can generate the corresponding public key through the private key, so we use the private key Private_key.pem on the server side, the public key is issued to the Android and iOS and other front

2, PHP with the generated public key, the private key for encryption and decryption , directly on the code

The code is as follows:

$FP =fopen ("Rsa/rsa_private_key.pem", "R"); Your private key file path $private_key=fread ($fp, 8192); fclose ($fp); $fp 1=fopen ("Rsa/rsa_public_key.pem", "R"); Your public key file path $public_key=fread ($fp 1,8192); fclose ($fp 1);//echo $private _key; $pi _key=openssl_pkey_get_private ($private _key);//This function can be used to determine if the private key is available, and can return the resource ID Resource ID $PU _key=openssl_pkey_get_ Public ($public _key);//This function can be used to determine if the key is available Print_r ($pi _key); echo "\ n"; echo "<br>";p rint_r ($pu _key); echo "\ n"; echo "<br>"; echo "

RSA Configuration FAQs for PHP :

3 DLL file usages in the OpenSSL folder in the code sample for the PHP development language

1, if your system is a Windows system, and the system32 file directory is not Libeay32.dll, ssleay32.dll these two files
Then you need to copy these two files to the system32 file directory.

2. If there is no php_openssl.dll in your PHP installation directory (PHP\EXT)

So please put Php_openssl.dll in this folder.

Like encryption and decryption of the small partners to take a good look at this article, benefited from ...

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.