0

I am trying to write a program to generate RSA keys private.der, and public.der in PKCS#8, DER format.

I can do it in OpenSSL manually easily, but I have no idea how to do it in java. I read about Keytool that you can also use manually. But I want to automate the process in a program to generate a unique usable keypair each time the program is ran, and export them to a folder.

Any help would be appreciated.

jww
  • 97,681
  • 90
  • 411
  • 885
Tevor
  • 316
  • 2
  • 4
  • 15
  • [OpenSSL commands in Java](https://stackoverflow.com/questions/23328928/openssl-commands-in-java) and [Generate RSA key pair and encode private as string](https://stackoverflow.com/questions/1709441/generate-rsa-key-pair-and-encode-private-as-string). If all else fails, then try [Google: java generate rsa keypair](https://www.google.com/search?q=java+generate+rsa+keypair). – jww May 04 '14 at 21:32

1 Answers1

1

Key generation works as follows:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048); // Keysize
KeyPair kp = keyGen.genKeyPair():
PrivateKey privKey = kp.getPrivate();
PublicKey pubKey = kp.getPublic();

Then use privKey.getEncoded() and pubKey.getEncoded() to get the encoded versions.

Drunix
  • 3,313
  • 8
  • 28
  • 50
  • How am I able to export this? I tried converting to bytes then writing to a fileoutputstream. But it says I cannot convert rsaPriv to bytes? Edit: nevermind I figured it out. Thanks for the help, sorry I have seen this source before but couldn't figure out how to implement it. – Tevor May 04 '14 at 20:12
  • @user3579224 Please indicate the exact error and your runtime. I don't think you've got the string `"cannot convert rsaPriv to bytes"` anywhere in your stacktrace or log file. – Maarten Bodewes May 04 '14 at 20:20
  • Drunix - the preferred method of dealing with questions that simply ask you for the answer with no research or effort is to *not* answer them. Answer to those types of questions are not supposed to be voted. See [Question quality is dropping on Stack Overflow](https://meta.stackoverflow.com/questions/252506/question-quality-is-dropping-on-stack-overflow). – jww May 04 '14 at 20:21
  • @jww I think this question shows at least minimal research. Maybe not enough, but OP at least found out about keytool. One could say that this is not the required level of own effort, but not "no research". – Drunix May 04 '14 at 20:27
  • Honestly I've been stuck on this simple piece of code for about 2 weeks, researched for quite a few hours. Cryptography is a new concept for me and I've been learning as much as possible, but all the tutorials online show external programs as there are limited sources to look at where everything is done in java. I had a code I was trying to get to work, and those 2 simple lines, "privkey.getEncoded(); & pubKey.getEncoded();" are what solved it for me. I was trying to convert just the keys to a file and encrypt which kept throwing errors. Was at the point of wanting any help possible so thanks! – Tevor May 04 '14 at 20:41
  • @Drunix - the number one hit from that Google search is the answer to the question. And it comes from Stack Overflow and its dated from 2009. And that answer from 2009 had the same problem, and someone else pointed to a Google search, too. The number three hit is Oracle's Java tutorial on the task. – jww May 04 '14 at 20:58