PHP performs RSA encryption and decryption recently handwritten a server-side security interface specification, which requires RSA encryption and decryption. So let's take a test and make a record.
Environment: Win7 64-bit
PHP 5.6.12
Prototype tools are required:
OpenSSL: http://slproweb.com/products/Win32OpenSSL.html
1. install OpenSSL
Free installation
2. enter the bin directory of OpenSLL to generate the private key and public key.
// Generate the private key openssl genrsa-out rsa_private_key.pem 1024 // generate the public key openssl rsa-in rsa_private_key.pem-pubout-out rsa_public_key.pem
Copy the generated private key and public key to your PHP project.
3. enable OpenSSL extension for PHP
Enable extension = php_openssl.dll in php. ini (remove ;)
4. PHP encryption and decryption exercises
* @ Time 2015-10-13 */namespace App \ Models; class RsaCrypt {const PRIVATE_KEY_FILE_PATH = 'app \ Certificate \ alipay'; const PUBLIC_KEY_FILE_PATH = 'app \ Certificate \ Certificate '; /*** Rsa encryption ** @ param string $ orignData * @ return string */public static function encode ($ orignData) {// path of the key file $ privateKeyFilePath = self :: PRIVATE_KEY_FILE_PATH; extension_loaded ('openssl ') or die ('php requires openssl extension support'); (file_exists ($ privateKeyFilePath) or die ('incorrect key file path '); // Generate a Resource Key. if the key file content is damaged, the openssl_pkey_get_private function returns false $ privateKey = openssl_pkey_get_private (file_get_contents ($ privateKeyFilePath); ($ privateKey) or die ('key unavailable '); // encrypted data for transmission over the network $ encryptData = ''; /// // use the private key to encrypt the image ///// /// // if (openssl_private_encrypt ($ orignData, $ encryptData, $ privateKey) {return $ encryptData;} else {die ('encryption failed ');}} /*** Rsa decryption ** @ param string $ encryptData * @ return string */public static function decode ($ encryptData) {// path of the public key file $ publicKeyFilePath = self :: PUBLIC_KEY_FILE_PATH; extension_loaded ('openssl ') or die ('php requires openssl extension support'); (file_exists ($ publicKeyFilePath) or die ('incorrect public key file path '); // generate a public key of the Resource type. if the content of the public key file is corrupted, the openssl_pkey_get_public function returns false $ publicKey = openssl_pkey_get_public (file_get_contents ($ publicKeyFilePath) or die ('public key unavailable '); // decrypted data $ decryptData = ''; //// // use the public key to decrypt the data ////// ///// // if (openssl_public_decrypt ($ encryptData, $ decryptData, $ publicKey) {return $ decryptData;} else {die ('decryption failed ');}}}
Appendix:
1. a BUG occurs when the private key is generated under Win:
Error:
WARNING: can't open config file: /usr/local/ssl/openssl.cnfLoading 'screen' into random state - doneGenerating RSA private key, 1024 bit long modulus.........++++++.........................................++++++unable to write 'random state'e is 65537 (0x10001)
Solution:
Perform the following operations 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] your OpenSSL path
II. references:
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