简体   繁体   中英

Extracting modulus and exponent from public key using Openssl and C++

So I'm currently studying how networking works. I've got a bunch of packets from wireshark which I have already copied in buffers(only the content, without header. Wireshark->Follow TPC Stream->C arrays). I can understand the protocol but in the 3rd packet it requires a key exchange. After some googling I found that I need RSA encryption. It sends me a public key + token. And I shoud return encrypted shared key(just generated) and encrypted token which I received earlier.

RSA_key needs exponent and modulus, So my question is: How can I create RSA_key object from single char[] buffer(the public key).

NOTE:I'm posting here after long time googling. I may not be using the right keywords because I'm not english speaker. I could find threads asking this with php only. The most useful (I think) link I found is http://www.techper.net/2012/06/01/converting-rsa-public-key-modulus-and-exponent-into-pem-file/

Thanks.

PS I'm sniffing minecraft and simulating client side(I choosed it because its popular game and I knew I will find resources on the web if I get stuck). However my question is exacly how to get modulus and exponent from a public key with format: http://pastebin.com/J9ddhgW5 (thats the bytes representing the public key)

Ohh I forgot, first column is the byte position, second column is the byte value typecasted to int, 3th column is again value but (int)(unsigned char) so I can see if its +127, 4th colums is the hex representation of the value and the last column is character based on ascii table(if its visible character).

The data you posted is an ASN.1 encoded (DER) RSA public key:

$ openssl ssl rsa -inform der -pubin -text < 12120862.key 
Public-Key: (1024 bit)
Modulus:
    00:81:1f:1d:00:7e:d0:c7:e2:2f:31:3d:0d:f0:a8:
    ab:c1:ea:66:ba:af:1d:a4:eb:b3:fd:51:58:1c:1d:
    81:ae:f0:99:9e:5c:26:67:b5:41:14:28:79:c0:29:
    e5:56:96:06:b7:4b:a0:c9:7f:41:46:9a:7e:85:10:
    a0:91:ea:58:bd:78:78:6d:3c:07:2a:3d:61:f3:ed:
    42:8b:1e:dc:6d:2d:21:41:7a:e8:15:51:0d:75:84:
    be:20:8c:76:43:8b:4b:67:6b:49:09:e9:20:a1:11:
    53:a0:d9:30:b1:c2:27:a6:09:e1:56:36:ed:7e:9b:
    23:e2:df:5b:bd:c5:66:ca:c5
Exponent: 65537 (0x10001)
writing RSA key
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBHx0AftDH4i8xPQ3wqKvB6ma6
rx2k67P9UVgcHYGu8JmeXCZntUEUKHnAKeVWlga3S6DJf0FGmn6FEKCR6li9eHht
PAcqPWHz7UKLHtxtLSFBeugVUQ11hL4gjHZDi0tna0kJ6SChEVOg2TCxwiemCeFW
Nu1+myPi31u9xWbKxQIDAQAB
-----END PUBLIC KEY-----

In OpenSSL, you can use the d2i_RSA_PUBKEY function to get a public key from an ASN.1-encoded file (like this one).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM