PHP RSA Research

Source: Internet
Author: User
Tags modulus openssl rsa pear
Recently studied the QQ mailbox login process, found that QQ mailbox login more rigorous, plus a few tips, one point is the user's password with JavaScript to do a bit of RSA encryption, in its login page there is a

12345678
var PublicKey = " Cf87d7b4c864f4842f1d337491a48fff54b73a17300e8e42fa365420393ac0346ae55d8afad975dfa175faf0106cba81af1dde4acec284dac6ed9a0d8 Feb1cc070733c58213effed46529c54cea06d774e3cc7e073346aebd6c66fc973f299eb74738e400b22b1e7cdc54e71aed059d228dfeb5b29c530ff34 1502ae56ddcfe9 "; var RSA = new Rsakey (); Rsa.setpublic (PublicKey, "10001"); var Res = Rsa.encrypt (document.form1.pp.value + ' \ n ' + document.form1.ts.value + ' \ n ') ; if (res) {Document.form1.p.value = hex2b64 (res);}

Look at the relevant source of Rsakey, should be in http://m367.mail.qq.com/zh_CN/htmledition/js/ Safeauth.js, looked at a bit or is quite complicated, I just search on Google there is no relevant information, found the site "Bigintegers and RSA in JavaScript", looked at their code, and QQ in the basic same, It is estimated that QQ is also used by someone else's code, this article has a key pair of the generation, encryption and decryption of the test page, thus inferred QQ code in the PublicKey, should be the key pair of the modulus (also can be understood as the public key bar), and QQ with a 1024-bit key, Can we use this modulus to encrypt passwords in other languages (like PHP)?

Think about doing, I search on google "php RSA", found this site http://www.edsko.net/misc/, there is PHP RSA implementation, but I rsa_encrypt it ($message, $public _ Key, $modulus, $keylength) method produced doubts, it has 4 parameters, the first is to encrypt the string, then three how to fill, what we now only know is modulus, and $public_key and $keylength from where, Really a little touch the head, or look at it with the example of it, the original example is from the key to the file to obtain relevant information, QQ key to the document is not natural, then I can only generate a key to the file, to see what the law it, under Linux with OpenSSL do

OpenSSL genrsa-out Key.pem 1024

Generated a 1024-bit key pair file, you can open to see, it should be Base64 encoded, and then we can get modulus by the following command

12
OpenSSL rsa-in key.pem-noout-modulusmodulus= D192471b8699640f931fe6f4facc3e990b894f894cea5bee0dcbd7a4b76752f7345cf9b5f1271001b724f7a0abf0a6e911e309536f4be4749e92dcc53 1b8e36b95969d206649c9dd2371b413a8dfd9b92569660b1499a5cd310b86a8fde24988e456897a416d2e7b0b649f0714f322c57ef92563b21a448d10 72ff3806c34c75

Compared to QQ, the number of digits is the same, next we use the command

OpenSSL rsa-in Key.pem-text-noout

The output reads as follows:
Private-key: (1024x768 bit)
Modulus:
00:D1:92:47:1B:86:99:64:0F:93:1F:E6:F4:FA:CC:
3E:99:0B:89:4F:89:4C:EA:5B:EE:0D:CB:D7:A4:B7:
67:52:F7:34:5C:F9:B5:F1:27:10:01:B7:24:F7:A0:
AB:F0:A6:E9:11:E3:09:53:6F:4B:E4:74:9E:92:DC:
C5:31:B8:E3:6B:95:96:9D:20:66:49:C9:DD:23:71:
B4:13:A8:DF:D9:B9:25:69:66:0B:14:99:A5:CD:31:
0b:86:a8:fd:e2:49:88:e4:56:89:7a:41:6d:2e:7b:
0B:64:9F:07:14:F3:22:C5:7E:F9:25:63:B2:1A:44:
8d:10:72:ff:38:06:c3:4c:75
publicexponent:65537 (0x10001)
Privateexponent:
00:83:D3:D9:08:F6:95:3C:BD:13:56:29:09:07:4E:
3D:3E:36:64:8C:74:98:BE:7F:4F:72:BC:3C:0C:F0:
15:7D:B9:E4:E5:6B:6A:C8:A4:42:CC:61:71:4E:97:
72:30:F2:3D:80:33:E9:A4:E3:48:C1:0F:9E:C4:51:
3D:75:F6:90:8E:F3:C3:F8:CE:45:59:2A:67:42:A8:
C6:D0:4C:1D:12:C4:CF:53:F8:B1:58:B4:E1:23:71:
0E:E9:E9:E0:40:3D:9A:99:E3:5F:E1:93:04:E2:0A:
60:34:77:56:BE:F9:8F:E6:4E:87:23:46:48:BA:38:
9d:dd:46:ce:20:b7:82:27:cd
PRIME1:
00:ee:a9:e4:70:9c:d4:fe:bf:cd:87:5c:00:cb:ea:
EF:82:92:E1:88:F7:99:6A:42:09:F4:FD:78:93:BD:
30:28:1F:2E:ED:C1:CD:D3:60:8B:34:52:89:A7:AC:
98:37:cd:96:81:1e:57:2f:46:08:0e:8d:fb:13:92:
8d:f5:7a:50:5f
Prime2:
00:E0:CB:65:5E:31:F2:3B:C0:7F:93:AE:D9:6C:35:
75:E5:CE:8B:37:7D:39:CE:82:DD:9B:43:00:09:A6:
D8:C1:AB:BC:10:FE:3D:56:34:FE:BD:38:FE:FC:6C:
F2:74:A8:D6:40:25:E5:5A:35:7B:D0:24:71:44:8D:
53:23:71:83:ab
Exponent1:
4B:D5:7F:D8:A8:7C:A5:55:9C:A0:DE:03:02:C8:6B:
C2:39:99:A0:43:CC:63:8F:08:4A:E8:1F:60:12:45:
32:FA:75:96:E6:75:D8:2C:5D:0F:0B:0A:E2:54:5D:
29:9E:11:AC:85:4F:7E:9D:EA:01:75:EB:C9:94:4F:
B7:28:5e:51
Exponent2:
00:9B:9F:D4:56:A8:E7:55:3C:88:55:FA:97:A5:55:
41:80:CE:44:0D:2F:51:A4:C9:6E:97:FD:83:7A:2B:
1B:26:C1:38:DA:DE:D8:21:E5:60:72:29:92:45:B9:
3B:05:4E:99:BD:21:3F:2D:FB:96:F2:DB:37:DB:48:
a7:c5:02:e2:2f
Coefficient:
00:C2:75:38:A5:02:24:39:1E:0E:E9:EC:56:6A:31:
5D:38:82:CA:3E:9B:67:CB:40:7E:7B:2F:91:26:BB:
4E:64:3D:60:53:F1:21:67:8B:B7:AF:F8:2E:95:F7:
AF:CF:42:75:AB:6C:5C:42:97:42:17:94:17:FF:E0:
b9:cb:c9:e8:6d

Through the code of its example, I understand, $public _key should be the 65537, $modulus can not directly use this text, to first turn into BigInteger, and then into the text to pass in, BigInteger realization in pear, http://pear.php.net/package/Math_BigInteger

We'll write the program next.

1234567891011
Include (' rsa.php '); include (' biginteger.php '); $public = 65537; $modulus = " D192471b8699640f931fe6f4facc3e990b894f894cea5bee0dcbd7a4b76752f7345cf9b5f1271001b724f7a0abf0a6e911e309536f4be4749e92dcc53 1b8e36b95969d206649c9dd2371b413a8dfd9b92569660b1499a5cd310b86a8fde24988e456897a416d2e7b0b649f0714f322c57ef92563b21a448d10 72ff3806c34c75 "; $keylength = 1024x768; $modulus _16 = new Math_biginteger ($modulus, +); $mend = $modulus _16->tostring (); $encrypted = Rsa_encrypt ("Test", $public, $mend, $keylength); Echo Bin2Hex ($encrypted); You can use BASE64,QQ here, or Base64.

Finally, my understanding of RSA, first generate a public/private key pair, and then publish the public key, the external system with public key encryption, to the internal system with the private key decryption.

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