0

I generated a KeyPair like this:

ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1");
KeyPairGenerator generator = KeyPairGenerator.getInstance("EC");
generator.initialize(ecSpec, new SecureRandom());
KeyPair kp = generator.generateKeyPair();

Then I generate Strings from the KeyPair:

String privateKeyString = Base64.getEncoder().encodeToString(kp.getPrivate().getEncoded();

So my first Question is how can I generate a PrivateKey object from privateKeyString and my second question is how can I generate a PublicKey / KeyPair from the PrivateKey object?

GLedger
  • 11
  • 2
  • You can't. PKI would be useless if this was possible. Your question doesn't make sense. Either you want asymmetric encryption or you want symmetric encryption. Not both at the same time. – user207421 May 03 '21 at 10:09
  • @user207421 i thought it was possible to recreate the pair from the private / secret key? – Reto Höhener May 03 '21 at 10:27
  • First question: `KeyFactory.getInstance("EC").generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64)))` – Reto Höhener May 03 '21 at 11:25
  • 2nd question: Probably not possible, as @user207421 noted. See https://stackoverflow.com/questions/11345346/how-to-get-a-rsa-publickey-by-giving-a-privatekey. Except if you have a private key certificate which might also contain the public key information. – Reto Höhener May 03 '21 at 11:26
  • You thought so why? If you could recreate a private key from anything other than its own encoding it wouldn't be private and the whole PKI system would be pointless. – user207421 May 03 '21 at 12:29
  • @RetoHöhener Certificates contain public keys but they never contain private keys. – user207421 May 03 '21 at 12:30
  • @user207421 you are right. i was thinking about PEM key files. – Reto Höhener May 03 '21 at 13:03
  • Despite the claims in the other comments it is possible to create an instance of the KeyPair class with just the base64-encoded private key, provided you know the EC domain parameters. And you do in your case, it is secp256k1. There should be plenty of related questions and answer here to guide you. The SecretKey class is for symmetric cryptography and is not applicable here. – President James K. Polk May 04 '21 at 16:31
  • @user207421 Sorry, I think I may have caused some confusion with the SecretKey class. I only want asymetric encryption. – GLedger May 09 '21 at 16:02
  • @RetoHöhener That works. Thanks for your answer. – GLedger May 09 '21 at 16:04
  • @PresidentJamesK.Polk Thanks for your answer. Yes I realised the SecretKey class doesn't really make sense here and changed it. Also could you maybe provide some links to the related questions if it isn't to much work. I've already searched a lot but didn't really find anything. – GLedger May 09 '21 at 16:09
  • See if [this question](https://stackoverflow.com/q/48832170/238704) together with the answer by @dave_thompson_085 helps. – President James K. Polk May 09 '21 at 16:20

1 Answers1

0

If you are trying to generate the private/public key pair from created secrect string, you can follow this example.

Security.addProvider(new BouncyCastleProvider());    
bytes secretKey = Base64.getEncoder().decode("Secret Key/ Created Secure Random Key");

ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1");
KeyPairGenerator generator = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
generator.initialize(ecSpec, new SecureRandom(secretKey));
KeyPair kp = generator.generateKeyPair();

I hope this is your need.

Kyaw Thura
  • 51
  • 7