2

How can I create a custom RSA key from a String? Assume I have a string key which is like a password.

String key = md5("testing");

Is there any way to create a private key using the key and derive a public key from the generated private key using RSA?

I am aware it's best practice to use a symmetric key when you have a specific String however I would like to know if it is possible to do this with RSA and how?

1
  • 1
    It's possible. But it's inevitably a bad idea, especially for someone who has to ask if it's possible. Commented Jun 2, 2018 at 1:55

2 Answers 2

3

Yes it is possible, because the RSA key generation algorithm basically generates random numbers using a (cryptographically secure) pseudo-random number generator, until it finds some that are prime. By seeding the PRNG with your key, you can deterministically generate the key pair. You can similarly generate ECC key pairs using a password.

I should note that the resulting public key can be brute-forced: if someone knows you've been using this method to generate the key pairs, they can use it themselves and try many passwords to see if they get the same public key. If so, they have also found the private key. For this reason, you should use the same defenses against password hashes bruteforce as usual: use strong passwords, a slow/non-parallelizable key derivation function (such as scrypt, or the older PBKDF2), and salts. So the proposed method to use md5 to derive the key should not be used.

In Java, generating the key pair from the key would look like this (untested):

KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048, new SecureRandom(key));
KeyPair keyPair = generator.generateKeyPair();
Sign up to request clarification or add additional context in comments.

1 Comment

Nah, that won't do it. But you can write your own subclass of SecureRandom to accomplish it. There an example in bouncycastle
1

No, that's not possible. Private/public key pairs are based on math on giant prime numbers. There's no way to use a pre-existing 16-byte MD5 hash as a key.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.