Php rsa Research

Source: Internet
Author: User
Tags modulus openssl rsa

I recently studied the logon process of QQ mail, and found that QQ mail logon is rigorous, with some tips added. One of them is to encrypt the user's password using JavaScript, there is a section in its login page

12345678
 VaR Publickey =   "Province"  ;  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 )  ;  } 

Let's look at the source code of rsakey, which should be in response and RSA in JavaScript.CodeIt is basically the same as that in QQ. It is estimated that QQ also uses people's code.ArticleThere is a test page for key pair generation and encryption and decryption. It is inferred that the publickey In the QQ code should be the modulus of the key pair (which can also be understood as the public key ), qq uses a 1024-bit key. Can we use modulus to encrypt passwords in other languages (such as PHP?

Think of it, I search for "php RSA" on Google, find the http://www.edsko.net/misc/ of this website, there is Php RSA implementation, but I am on it rsa_encrypt ($ message, $ public_key, $ modulus, $ keylength) has four parameters. The first one is the string to be encrypted, and the last three parameters are entered, all we know now is modulus, and where does $ public_key and $ keylength come from? It's a bit confusing. Let's take a look at its example, in the original example, the related information is obtained from the key pair file, and QQ's key pair file cannot be obtained, so I can only generate one key pair file by myself, let's take a look at the rules. OpenSSL is used in Linux.

OpenSSL genrsa-out key. pem 1024

A 1024-bit key pair file is generated. You can open it and check that it is base64-encoded. Then we can use the following command to obtain modulus.

12
OpenSSL RSA-InKey. pem-Noout -ModulusModulus= Bytes

Compared with QQ, the number of digits is the same. Next we will use the command

OpenSSL RSA-in key. pem-text-noout

The output content is as follows:
Private-key: (1024 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 (0 ×10001)
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 in its example, I understand that $ public_key should be 1024, and $ keylength is 65537. $ modulus cannot directly use this text. It must be converted to biginteger first and then converted to text, biginteger implementation in pear has, http://pear.php.net/package/Math_BigInteger

Next we will writeProgramRight

1234567891011
 Include  (  'Rsa. php'  )  ; Include  (  'Biginteger. php'  )  ;  $ Public   =   65537  ;  $ Modulus   =   "Province"  ; $ Keylength   =   1024  ;  $ Modulus_16   =   New Math_biginteger (  $ Modulus  ,  16  )  ;  $ Mend   =   $ Modulus_16  -> Tostring  (  )  ;  $ Encrypted   = Rsa_encrypt (  "Test"  ,   $ Public  ,   $ Mend  ,   $ Keylength  )  ;  Echo  Bin2hex  (  $ Encrypted  )  ;   // Here you can also use base64, QQ is base64 

Finally, I understand RSA. First, I generated a public key/Private Key key pair, and then published the public key. The external system encrypted it with the public key and sent it to the internal system for Private Key decryption.

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.