console - How to call CMD to create GnuPG-Keypair from Java -
i'm trying execute gpg-command in java create new keypair, i'm not getting answer console. code works if try execute gpg-command version gpg --version
or retrieve keylist gpg --list-key
.
i'm using code stackoverflow-question:
public void getkeylist(){ try { process gpgprocess = runtime.getruntime().exec("gpg --gen-key"); bufferedreader gpgoutput = new bufferedreader(new inputstreamreader(gpgprocess.getinputstream())); bufferedwriter gpginput = new bufferedwriter(new outputstreamwriter(gpgprocess.getoutputstream())); bufferedreader gpgerroroutput = new bufferedreader(new inputstreamreader(gpgprocess.geterrorstream())); boolean executing = true; while(executing){ try { int exitvalue = gpgprocess.exitvalue(); if (gpgerroroutput.ready()){ string error = getstreamtext(gpgerroroutput); system.err.println(error); }else if (gpgoutput.ready()){ system.out.println(getstreamtext(gpgoutput)); } } catch (exception e){ //the process not yet ready exit. take break , try again. try { thread.sleep(100); } catch (interruptedexception e1) { system.err.println("this thread has insomnia: " + e1.getmessage()); } } } } catch (ioexception e){ e.printstacktrace(); } } private string getstreamtext(bufferedreader reader) throws ioexception{ stringbuilder result = new stringbuilder(); try{ while(reader.ready()){ result.append(reader.readline()); if(reader.ready()){ result.append("\n"); } } }catch(ioexception ioe){ system.err.println("error while reading stream: " + ioe.getmessage()); throw ioe; } return result.tostring(); }
i've tried processbuilder
instead of runtime
, that's not solution. have idea on how solve problem, or totally impossible interact console during key-generation process?
gpg --genkey
interactive call, waits input, never provide. 2 possible solutions:
- use bouncycastle instead, native java library openpgp.
as implementing interactive gnupg session rather complicated , error-prone, might better use "experimental feature" batch key generation.
man gpg
:--gen-key generate new key pair. command used interactively. there experimental feature allows create keys in batch mode. see file `doc/details' in source distribution on how use this.
the file
doc/details
available online. section want called "unattended key generation". it's rather lengthy, didn't quote here, example document on how it:$ cat >foo <<eof %echo generating basic openpgp key key-type: dsa key-length: 1024 subkey-type: elg-e subkey-length: 1024 name-real: joe tester name-comment: stupid passphrase name-email: joe@foo.bar expire-date: 0 passphrase: abc %pubring foo.pub %secring foo.sec # commit here, can later print "done" :-) %commit %echo done eof $ gpg --batch --gen-key foo
Comments
Post a Comment