This article is available only because it will be submitted to internal publications next week.
In fact, the content mainly comes from the previous blog post, which only re-organizes the language and elaborates on its principles to make it easier to understand: P
----------------------------------------- Gorgeous split line -----------------------------------------
On most websites on the Internet, user data is directly submitted to the backend CGI in plaintext format, and access between servers is mostly transmitted in plaintext, this can be monitored by some ulterior motives. Websites with high security requirements, such as banks and large enterprises, Use https to encrypt the communication process.
However, https is expensive. It is not only about purchasing CA certificates, but also about serious performance bottlenecks. Currently, only dedicated SSL hardware acceleration devices such as F5 bigip can be used. Therefore, some websites adopt the simple practice of simulating SSL and use RSA and AES to encrypt the transmitted data. Shows the principle:
This improves the security of data transmission to a certain extent. However, for most websites, most of the data is usually not so strict, so you can selectively encrypt only some important small data, such as passwords. ForSmall Data VolumeFor encryption, you do not need to use the entire process. You only need to use RSA, which greatly simplifies the process.
Why small data volumes? As the data volume increases, the encryption process is extremely slow compared with symmetric encryption. Therefore, symmetric encryption algorithms are generally used for actual data encryption. Therefore, the OpenSSL extension public/private key encryption function in PHP only supports small data (117 bytes for encryption and 128 bytes for decryption ).
There are some open source JavaScript algorithm libraries for AES and RSA on the Internet, which can be directly implemented through related extensions in PHP (AES algorithm can be implemented through mcrypt related functions, RSA can be implemented through functions related to OpenSSL), instead of using pure PHP code to implement algorithms. Due to space limitations, this article only introduces the implementation of RSA encrypted communication between JavaScript and PHP, and uses password encryption as an example.
First run the Code:
Front-end encryption
First load three rsa js library files, you can download the http://www.ohdave.com/rsa/ here
$ (Document ). ready (function () {<br/> // hexadecimal Public Key <br/> var rsa_n = "regular "; </P> <p> $ ("# submit "). click (function () {<br/> setmaxdigits (131); // 131 => hexadecimal digits of N /2 + 3 <br/> var key = new rsakeypair ("10001", '', rsa_n ); // 10001 => E hexadecimal <br/> var Password = $ ("# password "). val (); <br/> Password = encryptedstring (Key, password); // Chinese characters are not supported ~ <Br/> $ ("# password "). val (password); <br/> $ ("# login "). submit (); <br/> });
PHP encryption functions
/** <Br/> * public key encryption <br/> * @ Param string plaintext <br/> * @ Param string Certificate file (. CRT) <br/> * @ return string ciphertext (base64 encoded) <br/> */<br/> function publickey_encodeing ($ sourcestr, $ filename) <br/>{< br/> $ key_content = file_get_contents ($ filename); <br/> $ pubkeyid = openssl_get_publickey ($ key_content ); <br/> If (openssl_public_encrypt ($ sourcestr, $ crypttext, $ pubkeyid) <br/>{< br/> return base64_encode ("". $ crypttext); <br/>}< br/> return false; <br/>}< br/>
PHP decryption Function
/** <Br/> * Private Key decryption <br/> * @ Param string ciphertext (base64 encoding) <br/> * @ Param string key file (. PEM) <br/> * @ Param string whether the ciphertext is from RSA encryption in JS <br/> * @ return string plaintext <br/> */<br/> function privatekey_decodeing ($ crypttext, $ filename, $ fromjs = false) <br/>{< br/> $ key_content = file_get_contents ($ filename); <br/> $ prikeyid = openssl_get_privatekey ($ key_content ); <br/> $ crypttext = base64_decode ($ crypttext); <B R/> $ padding = $ fromjs? Openssl_no_padding: openssl_pkcs1_padding; <br/> If (openssl_private_decrypt ($ crypttext, $ sourcestr, $ prikeyid, $ padding) <br/>{< br/> return $ fromjs? Rtrim (strrev ($ sourcestr), "/0 "):"". $ sourcestr; <br/>}< br/> return false; <br/>}< br/>
Test code
Define ("CRT", "SSL/server. CRT "); // public key file <br/> define (" PEM "," SSL/server. PEM "); // Private Key File <br/> // js-> PHP test <br/> $ data =$ _ post ['Password']; <br/> $ txt_en = base64_encode (pack ("H *", $ data); // convert it to base64 format <br/> $ txt_de = privatekey_decodeing ($ txt_en, PEM, true); <br/> var_dump ($ txt_de); <br/> // PHP-> PHP test <br/> $ DATA = "test "; // PHP supports Chinese characters: d <br/> $ txt_en = publickey_encodeing ($ data, CRT); <br/> $ txt_de = privatekey_decodeing ($ txt_en, PEM ); <br/> var_dump ($ txt_de); <br/>
After the code is pasted, You need to describe several points. The key is to obtain the hexadecimal public key. Because the key is obtained from the X.509 Certificate, You need to convert it into a key and a certificate file (the 1024-bit key used in this article). Please Google the method for generating the key. Here we will focus on how to obtain the hexadecimal key from it.
I have tried many methods to read the hexadecimal key from a file. I have mentioned that data is encoded using ASN.1 on the internet ...... Region ~
Finally, I accidentally noticed that OpenSSL in Linux Shell seems to be able to be extracted from the private key file (key or PEM.
OpenSSL asn1parse-out temp. ANS-I-inform PEM <server. pem
The result is as follows:
Here we can finally see the hexadecimal Public Key key required in JavaScript.