我试图在Java中执行gpg-command来创建新的Keypair,但是没有从控制台得到答案。如果尝试为版本gpg --version执行gpg-command或使用gpg --list-key检索键列表,我的代码将运行良好。

我正在使用另一个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 is not yet ready to exit.  Take a break and 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 the stream: " + ioe.getMessage());
        throw ioe;
    }
    return result.toString();
}


我也尝试使用ProcessBuilder代替Runtime,但这不是解决方案。
您是否有关于如何解决此问题的想法,或者在密钥生成过程中完全不可能与控制台进行交互?

最佳答案

gpg --genkey是一个交互式呼叫,它等待您从未提供的输入。两种可能的解决方案:




请改用bouncycastle,它是OpenPGP的本机Java库。
由于实现交互式GnuPG会话非常复杂且容易出错,因此您最好使用“实验功能”来生成批密钥。来自man gpg

--gen-key
      Generate a new key pair. This command is normally only used
      interactively.

      There is an experimental feature which allows you to create
      keys  in  batch  mode.  See  the  file `doc/DETAILS' in the
      source distribution on how to use this.


文件doc/DETAILS is also available online。您要查找的部分称为“无人值守密钥生成”。它相当冗长,因此我在这里没有引用它,但这是文档中有关如何执行此操作的示例:

$ cat >foo <<EOF
     %echo Generating a basic OpenPGP key
     Key-Type: DSA
     Key-Length: 1024
     Subkey-Type: ELG-E
     Subkey-Length: 1024
     Name-Real: Joe Tester
     Name-Comment: with stupid passphrase
     Name-Email: joe@foo.bar
     Expire-Date: 0
     Passphrase: abc
     %pubring foo.pub
     %secring foo.sec
     # Do a commit here, so that we can later print "done" :-)
     %commit
     %echo done
EOF
$ gpg --batch --gen-key foo

09-10 06:44
查看更多