PHP RSA encryption and decryption use of detailed _php example

Source: Internet
Author: User
Tags decrypt fread openssl openssl rsa

1, encryption and decryption of the first step is to generate a public key, private key pair, the private key encrypted content can be decrypted through the public key (in turn can also)

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

Copy Code code 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 the PKCS8 format, and the third generates the RSA public key Rsa_public_key.pem
It is seen from above that the corresponding public key can be generated by the private key, so we use the private key Private_key.pem on the server side, and the public key is distributed to the front end of Android and iOS

2, PHP use the generated public key, the private key to encrypt and decrypt , directly on the code

Copy Code code 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 whether the private key is available, and can be used to return the resource ID 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 "<br>";
Print_r ($pu _key), echo "\ n"; echo "<br>";
echo "$data = ' php ras encryption algorithm ';
$encrypted = "";
$decrypted = "";
echo "Encrypted source data:". $data. " \ n "; echo "<br>";
echo "Private key encrypt:\n"; echo "<br>";
Openssl_private_encrypt ($data, $encrypted, $pi _key);//private key encryption
$encrypted = Base64_encode ($encrypted);//encrypted content usually contains special characters, need to encode conversion, in the network through the URL transmission to pay attention to base64 encoding is URL security
Echo ' private key encrypted: '. $encrypted. ' \ n "; echo "<br>"; echo "<br>";
echo "Public key decrypt:\n"; echo "<br>";
Openssl_public_decrypt (Base64_decode ($encrypted), $decrypted, $pu _key);//private key encrypted content can be decrypted by public key
After the echo ' public key is decrypted: '. $decrypted. ' \ n "; echo "<br>";
echo "echo "Public key encrypt:\n"; echo "<br>";
Openssl_public_encrypt ($data, $encrypted, $pu _key);/Public key encryption
$encrypted = Base64_encode ($encrypted);
echo $encrypted, "\ n"; echo "<br>";
echo "Private key decrypt:\n"; echo "<br>";
Openssl_private_decrypt (Base64_decode ($encrypted), $decrypted, $pi _key);//private key decryption
echo $decrypted, "\ n"; echo "<br>";

RSA Configuration FAQ for PHP :

3 DLL file usages in the OpenSSL folder in the code example of the PHP development language

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

2, if your PHP installation directory (Php\ext) in the absence of Php_openssl.dll

So please put Php_openssl.dll in this folder.

Like encryption and decryption of the small partners must take a good look at this article, benefit ...

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.