Generate 1024 bit RSA keypair and save public key in ASN.1 format (hex) in node.js

Hopefully someone will be good enough to give me a little help here.

I need to generate a 1024 bit RSA key for one time use (gets discarded after each run) and pass the public key to a remote client. The remote client expects the key to be "in ASN.1 format as defined in x509". (I know how to make the private key in the first place with openssl, and then how to extract the public key - both in ascii format. I am stuck with converting to ASN.1, which needs to be in hex)

It is for use when communicating with a Minecraft client. (What I am writing acts as the server)

Here is the information I have to work off that describes what is required: http://wiki.vg/Protocol_Encryption#Key_Exchange (specifically the key exchange section)

I ideally need to do this in node.js. (or a syscall to openssl would be just fine if that will do it)

!((info below this line potentially not relevant))

The idea is that I send the ASN.1 formatted public key to the client. The client encrypts a password and sends it back to me (the server) where I need to decrypt and store the password for later use.

My server is being written in node.js so any suggestions how I can do it within node.js, or by calling openssl as command prompt would be very much appreciated.

Please be aware that I know very little about ASN.1 so I am probably missing something obvious. (hopefully)

I have been looking around google for several hours and I have found a lot of things that touch on the subject but I have either not been able to properly understand them, or simply could not make them work.

To be clear - I am not looking to encrypt any data with the ASN.1 - I just need to send the public key to the client in ASN.1 format. (Presumably I can easily decrypt the encrypted password that is sent back with a call to openssl, using the private key generated to start with)

If anyone can help me a little here it would be very much appreciated.

James

Answers:

Answer

To get what I needed (using openssl) I did the following:

1 Generate Private Key

openssl genrsa -out private_key 1024

2 Export Public Key

openssl rsa -in private_key -pubout -out public_key

3 Export Public Key in DER format, which seems to be in the ASN.1 format I needed.

openssl rsa -outform der -pubin -in public_key -pubout -out public_key.asn1

To decrypt the passkey sent back from minecraft client

openssl rsautl -decrypt -inkey private_key -in encrypted_data_in -out data_out

Hope that helps someone. :)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.