PHP RSA Plus decryption example (GO)

Source: Internet
Author: User

1. Generating keys and public keys
Need to prepare the OpenSSL environment before starting
Linux needs to install the OpenSSL Toolkit, portal http://www.openssl.org/source/
Windows need to install OpenSSL program, portal http://slproweb.com/products/Win32OpenSSL.html
If you do not want to install it, you can test it with the key and public key provided in this example.

Key generation
OpenSSL Genrsa is used to generate RSA private key files, which can specify the length of the private key and password protection, please refer to the documentation for specific parameters.

OpenSSL Genrsa-out1024x768

-OUT Specifies the file name of the generated key, which can be arbitrarily defined, but preferably for the meaning of the name, the key file generated here is Rsa_private_key.pem
A 1024-length key is generated here, and the longer the key length is, the more secure the encryption, but the longer the decryption takes.
The contents of the generated key Rsa_private_key.pem file are:

-----BEGIN RSA PRIVATE KEY-----Miicxaibaakbgqcxlkt87xxt+TJZQGGWY5XBQD7HMRVNGWXDMEC8N02VLKJYDWB8FU4OOOKGTGPSNHY2O4L1S0AAXYPLPQBCOOU4AA4NAIQVSRBFOTZHIGOMK3UNFXCDXH9F9RJDID3UTP RH+9sijhjusrc8lwlsogvhio2/xj4/yZAZVYQEnWcGfQIDAQABAoGANWU3/xt5h5au4gxs8x+7hnkugbx3jvg/F5qafnk4sxffjsuuevasor567chrtrqaz1sz+Ceilfnwjhii48wda4qkzojp8leihendcwlutm7zqbftli9kee2fdi2narnfu//R7WRK5NNSCZST0DV/KKSUBP//4TSREPQWID6CKKECQQDXY7S+TDRRVKVTkd4/3ae5h3exokcybwwkhvbazgjfizysfndhx04sv4zjf0bt1gu48ko/ACFSZEKAVXBW6UJTAKEA0I7QCXAQSYW1T9XZRFKHW8VXGI2NDAQ7RBKG8OXNZSA6TYJMTY79YQF+q1kq52f7iaby5jtlibbot5xhc3+X0qjatcap6d8mdibaock4jon8z5ip4lp2bfj0t4miz0stcydlxttmnd6ml3kfhpaytdze0sizy/Kl4d6q9njof4mgnqjaeiqh+etib/bvmcd9o4+ybra5ckmhf/ltczdepikxoygjzk90r26yjqk+Hsevap0ohj7suzhiscv2o7ut+anuiqjbakudl8zyopz/75dcvvtmfkhhmq/ar+UHQU2AEQCZOGU35/injbvkozhlgxit78jnr7kcoyfb5g4obttzo0zyyaq=-----END RSA PRIVATE KEY-----

Public Key Generation
RSA commands are used to process RSA keys, format conversions, and print information

OpenSSL RSA- in Rsa_private_key.pem-pubout- out Rsa_public_key.pem

which
-in FileName: The RSA key file entered, where the key generated above is RSA_PRIVATE_KEY.PEM.
-pubout: After setting this option, save the public key value to the output file.
-out FileName: Output file, which we define as Rsa_public_key.pem
The contents of the generated public key Rsa_public_key.pem file are:

-----BEGIN Public KEY-----migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdt8lasjepyr1vq/  Q87INJCV7GEQXI2RGAB7LVFS02FXLNWGTQQ1CF+NH6MP88UFXXDVI2OL1RCH48DZPG7MGS4C0YTEDOPIETCZ8FW+ aipytaq1axts51lnwxeyiak28l8mezoqjmn+nb3vfmtaktukrzzxidt3r83+lx4xxxqxwidaqab-----END Public KEY-----

2.PHP decryption with RSA encryption
Before you start, you need to prepare the PHP OpenSSL environment, open the PHP OpenSSL extension, window will php configuration file php.ini in the
; Extension=php_openssl.dll
Switch
Extension=php_openssl.dll

The code is as follows:

<?PHP/** * User: [email protected]* date:14-11-29* time: Morning 10:27*//** * Path to the key file*/$privateKeyFilePath='Rsa_private_key.pem';/** * Path to public key file*/$publicKeyFilePath='Rsa_public_key.pem'; extension_loaded ('OpenSSL') or Die ('PHP requires OpenSSL extension support');(file_exists ($privateKeyFilePath)&&file_exists ($publicKeyFilePath)) or Die ('the file path for the key or public key is incorrect');/** * Generates a key of type resource, if the key file contents are corrupted, the Openssl_pkey_get_private function returns false*/$privateKey=openssl_pkey_get_private (file_get_contents ($privateKeyFilePath));/** * Generates a public key of type resource, Openssl_pkey_get_public function returns False if public key file contents are corrupted*/$publicKey=openssl_pkey_get_public (file_get_contents ($publicKeyFilePath));($privateKey&& $publicKey) or Die ('The key or public key is not available');/** * Original data*/$originalData='My account number is: Shiki, password is: Matata';/** * Encrypt later data for transmission on the network*/$encryptData="'; Echo'The original data is:', $originalData, Php_eol;///////////////////////////////Encrypt with private key////////////////////////if(Openssl_private_encrypt ($originalData, $encryptData, $privateKey)) {/** * After encryption can be base64_encode after easy to transfer in the URL or print otherwise garbled*/Echo'the encryption succeeds, after the encrypted data (after Base64_encode) is:', Base64_encode ($encryptData), Php_eol;} Else{die ('Encryption failed');}///////////////////////////////decryption with public key/////////////////////////** * After decrypting the data*/$decryptData="';if(Openssl_public_decrypt ($encryptData, $decryptData, $publicKey)) {echo'decryption is successful and the decrypted data is:', $decryptData, Php_eol;} Else{die ('Decryption succeeded');}

Printing results:

The original data is: My account is: Shiki, the password is: Matata encryption is successful, after the encrypted data (Base64_encode) is: x6q/u/huw4o7z0tetawurt3ahvgt9/m+ yiqabdds9cchrekb0yu9uav5za/vzawlodvhiyl9bac3wheriexz+gybujkki69rfkwhl5n3llfcetdnsxiq4aa+ ysfmcicxxwmep2vuwzbcxjirvligge1shjjw/txvmocolafrvnq= decryption success, after the decrypted data: My account is: Shiki, password is: Matata

Public key cryptography Openssl_public_encrypt and private key decryption openssl_private_decrypt are similar to the above procedure, you can refer to the document to complete the test.

PHP RSA Plus decryption example (GO)

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.