RSA encryption and decryption by PHP

Source: Internet
Author: User
Tags openssl download openssl rsa
Recently in the beginning to write a service-side security interface specification, need to use RSA encryption decryption. So try sledgehammer a little bit and make a record.

Environment: Win7 64-bit

PHP 5.6.12

Prototyping tools are required:

OpenSSL download Address: http://slproweb.com/products/Win32OpenSSL.html

I. Installation of OpenSSL

Where to install freely

Second, enter into the bin directory of OPENSLL for private key and public key generation

Generate private key OpenSSL genrsa-out RSA_PRIVATE_KEY.PEM 1024//Generate public key OpenSSL rsa-in rsa_private_key.pem-pubout-out Rsa_public_key. Pem

Copy the private key and public key of the production to your PHP project

Iii. opening the OpenSSL extension for PHP

Open the Extension=php_openssl.dll in the php.ini (remove;)

Iv. PHP encryption and decryption exercises

  * @time 2015-10-13 */namespace app\models;class rsacrypt {Const Private_key_file_path = ' App\certificate\rsa_priva    Te_key.pem ';    Const Public_key_file_path = ' APP\CERTIFICATE\RSA_PUBLIC_KEY.PEM ';        /** * RSA Encryption * @param string $orignData * @return String */public static function encode ($orignData) {        The path to the key file $privateKeyFilePath = self::P rivate_key_file_path;        extension_loaded (' OpenSSL ') or Die (' PHP requires OpenSSL extension support ');        (File_exists ($privateKeyFilePath)) or Die (' The file path of the key is incorrect '); Generates a resource type key, and if the contents of the key file are corrupted, the Openssl_pkey_get_private function returns false $privateKey = Openssl_pkey_get_private (file_get_        Contents ($privateKeyFilePath));        ($privateKey) or Die (' key not available ');        Encrypt subsequent data for transmission over the network $encryptData = '; Use the private key to encrypt the////////////////////////if (Openssl_private_encrypt ($orignData, $encryptData        , $privateKey)) {return $encryptData;    } else {die (' encryption failed ');    }}/** * RSA decryption * @param string $encryptData * @return String */public static function Decod        E ($encryptData) {//the path to the public key file $publicKeyFilePath = self::P ublic_key_file_path;        extension_loaded (' OpenSSL ') or Die (' PHP requires OpenSSL extension support ');        (File_exists ($publicKeyFilePath)) or Die (' The file path of the public key is incorrect '); Generates a public key of type resource, if the public key file contents are corrupted, the Openssl_pkey_get_public function returns false $publicKey = Openssl_pkey_get_public (file_get_        Contents ($publicKeyFilePath));        ($publicKey) or Die (' public key not available ');        The data after decryption $decryptData = '; Decrypts the////////////////////////if (Openssl_public_decrypt ($encryptData, $) with the public key        Decryptdata, $publicKey)) {return $decryptData;        } else {die (' decryption failed '); }    }}


Appendix:

One, a bug was encountered when using the generate private key under win:

Error:

Warning:can ' t open config file:/usr/local/ssl/openssl.cnfloading ' screen ' into random state-donegenerating RSA private Key, 1024x768 bit long modulus.........++++++.........................................++++++unable to write ' random State ' E is 65537 (0x10001)

Workaround:

Do the following in CMD

Set Openssl_conf=c:\openssl-win32\bin\openssl.cfg

Or

Set Openssl_conf=[path-to-openssl-install-dir]\bin\openssl.cfg

Ps:[path-to-openssl-install-dir] for your OpenSSL path


Second, reference materials:

http://php.net/manual/en/book.openssl.php

Http://www.jb51.net/article/64963.htm

Http://stackoverflow.com/questions/16658038/cant-open-config-file-usr-local-ssl-openssl-cnf-on-windows

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