PHP RSA encryption and decryption using a detailed _php instance

Source: Internet
Author: User
Tags decrypt openssl rsa
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:

Copy the 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
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

Copy CodeThe 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 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 "
";
Print_r ($pu _key); echo "\ n"; echo "
";
echo "";
$data = ' php ras encryption algorithm ';
$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 contents of the encryption usually contain special characters, need to encode the conversion, the transmission between the network through the URL to pay attention to base64 encoding is URL-safe
After the echo ' private key is encrypted: '. $encrypted. ' \ n "; echo "
"; Echo"
";
echo "Public key decrypt:\n"; echo "
";
Openssl_public_decrypt (Base64_decode ($encrypted), $decrypted, $pu _key);//private key encrypted content can be decrypted by 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 "
";

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 must take a good look at this article, benefited from ...

  • 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.