Me and my friend would like to login to an SSH server using the same private RSA key.

However, we don't want to transmit the key over the internet, for security.

I am thinking if it's possible to generate the same RSA key pair using a mutual secret from the Diffie–Hellman Algorithm (or other key exchange algorithms).

So basically I want to ask:

  1. Is there a tool that does this already? I did some research and found none.
  2. If I want to implement this myself, how can I seed an RSA keygen algorithm with my mutual secret, without compromising the security? I read this post and it seems that some further processing is needed, but I'm not familiar with cryptography so IDK if that also applies to my application here.
  • 1
    $\begingroup$ Why? Sharing an SSH key is compromising security. And how would a DH exchange to get the same seed values be any more secure than a DH exchange to send a key file encrypted? $\endgroup$ Commented May 8, 2022 at 2:09

2 Answers 2


This is going in the wrong direction, so I'll answer by showing two alternatives:

  • Share e.g. a strong machine generated password for this with enough randomness to pass as a secret key. The reason to share a password instead of another secret key is because I don't know a well used variant of SSH that uses a secret key.
  • To share a private key it seems more logical to encapsulate it, i.e. by using regular encryption. You can always send a public key the other way, validate that it was generated by the other party and then encapsulate the existing key. It is probably easiest to do this with an existing protocol such as PGP, which provides helpful services such as key fingerprinting which can be used for the validation.

Many issues have been indicated when it comes to regenerating the same private key for RSA - indicated by the post you already included.

Basically everything about the key pair generation needs to remain the same for the generation algorithm to remain deterministic. Any significant change in the generation part and the resulting key pair will be different. I have for instance described a more efficient way to retrieve random values in a range. But any update in the prime generation part would also break the algorithm.

I would not expect any key pair generation mechanism to be present that is so well described that it can be used deterministically over different versions of the software. If you still want to agree on a private key then I'd go to ECDSA, where the private key is simply a single value in a range and where the public key can be directly derived from the private key.


There are two distinct steps in what's asked:

  • Use Diffie-Hellman Key Exchange to generate a secret shared only between the OP and their friend. Caveat: this is going to be vulnerable to an active Man in the Middle attack (MitM), unless there is some pre-shared information (e.g. a pre-shared secret, or public keys for both parties).
  • Use said secret shared only between the OP and their friend to generate an RSA public/private key pair with the private key shared between the OP and their friend (and the same public key).

The later one is easy, in principle: both the OP and their friend seed the same agreed-upon Cryptographically Secure Pseudo-Random Number Generator (CSPRNG) and use that to generate an RSA key using the same agreed-upon deterministic algorithm and parameters (e.g. 2048-bit public modulus, public exponent 65537). Since everything is deterministic, both the OP and their friend will get the same RSA key pair.

This is easy under many modern APIs/programming languages, including Java.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.