RSA Encryption Decryption

Source: Internet
Author: User
Tags openssl rsa

RSA Encryption Decryption:

Openssl_pkey_get_private series functions, under Windows need to load Extension=php_openssl.dll;linux under extension=openssl.so.

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:

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

Openssl_pkey_get_private

Openssl_pkey_get_public

Openssl_private_encrypt

Openssl_public_decrypt

Openssl_public_encrypt

Openssl_private_decrypt

<?PHP//private key encryption, public key decryption$privateFile= "Rsa_private_key.pem";$privateKey=file_get_contents($privateFile);$pi _key= Openssl_pkey_get_private ($privateKey);//Print_r ($pi _key); echo "\ n";$data= "AASSSSASSSDDD";//Raw DataEcho"Source:".$data." \ n ";$encrypted= ""; $decrypted= ""; Openssl_private_encrypt ($data,$encrypted,$pi _key);//private key EncryptionEcho $encrypted."|||| \ n ";$encrypted=Base64_encode($encrypted);Echo $encrypted, "\ n"; $publicFile= "Rsa_public_key.pem";$publicKey=file_get_contents($publicFile);$pu _key= Openssl_pkey_get_public ($publicKey); Openssl_public_decrypt (Base64_decode($encrypted),$decrypted,$pu _key);//private key encrypted content can be decrypted by public keyEcho $decrypted, "\ n";//public key encryption, private key decryption$data= "ABCDEFG"; Openssl_public_encrypt ($data,$encrypted,$pu _key); Openssl_private_decrypt ($encrypted,$decrypted,$pi _key);Echo $decrypted;

RSA Encryption Decryption

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.