Author Topic: gpg --gen-key hangs as TProcess  (Read 2055 times)


  • Jr. Member
  • **
  • Posts: 97
gpg --gen-key hangs as TProcess
« on: February 26, 2016, 12:56:22 pm »
Under Linux running from the Terminal Command Line,

Code: Pascal  [Select][+][-]
  1. morbid@morbid ~ $ gpg --gen-key
  2. gpg (GnuPG) 1.4.16; Copyright (C) 2013 Free Software Foundation, Inc.
  3. This is free software: you are free to change and redistribute it.
  4. There is NO WARRANTY, to the extent permitted by law.
  6. Please select what kind of key you want:
  7.    (1) RSA and RSA (default)
  8.    (2) DSA and Elgamal
  9.    (3) DSA (sign only)
  10.    (4) RSA (sign only)
  11. Your selection?

In my code...

Code: Pascal  [Select][+][-]
  1. {To Create a PGP Key}
  2. procedure TGPGForm.GenerateGPG();
  3. begin
  4.   GPGKey := TProcess.Create(nil);                                               //Create the Process.
  5.   OutLst := TStringList.Create;                                                 //To Capture the running Output.
  6.   ErrLst := TStringList.Create;                                                 //To Capture the running Errors.
  7.   GPGLst := TStringList.Create;                                                 //To Capture the final Output.
  8.   try
  9.     GPGKey.Executable :=  'gpg';                                                //Command to Run is gpg.
  10.     GPGKey.Parameters.Add('--gen-key');
  11.     GPGKey.Options := [poUsePipes, poStderrToOutPut];                           //Use Pipes, STDOut/Err for Communications.
  12.     GPGKey.Execute;                                                             //Run the Command.
  13.     while  GPGKey.Active = True do                                              //While the Command is Running.
  14.     begin
  15.       case ReadSTDOutError('GPGKey') of                                         //Read output from the Command.
  16.         01:WriteSTDIn('1'                    + LineEnding, 'GPGKey');           //RSA Keys.
  17.         02:WriteSTDIn('4096'                 + LineEnding, 'GPGKey');           //Biggest Length.
  18.         03:WriteSTDIn('0'                    + LineEnding, 'GPGKey');           //Does Not Expire.
  19.         04:WriteSTDIn('Y'                    + LineEnding, 'GPGKey');           //Yes it is Incorrect.
  20.         05:WriteSTDIn('BobBruce'             + LineEnding, 'GPGKey');           //That's My Real Name.
  21.         06:WriteSTDIn('' + LineEnding, 'GPGKey');           //That's My Real E-Mail Address.
  22.         07:WriteSTDIn('bobsPGP'              + LineEnding, 'GPGKey');           //That's what I want to call it.
  23.         08:WriteSTDIn('O'                    + LineEnding, 'GPGKey');           //OK Go!!
  24.         09:WriteSTDIn('general'              + LineEnding, 'GPGKey');           //Pass!? Falls through Twice?? Go!!!!
  25.         10:GetFOutput('GPGKey');
  26.       end;
  27.     end;
  28.     GPGKey.Free;                                                                //Job Done so Free the Process.
  29.     OutLst.Free;                                                                //and Free the Lists.
  30.     ErrLst.Free;
  31.     GPGLst.Free;
  32.   except
  33.   end;
  34. end;    


Code: Pascal  [Select][+][-]
  1. {Handles Communications from the Runnning Process}
  2. function TGPGForm.ReadSTDOutError(TheName: String): integer;                    //Use Name of The Running Process.
  3. begin
  4.   NewBit := 0;                                                                  //No Output From Process Yet.
  5.   repeat                                                                        //Repeat Getting Output From Process.
  6.     OldBit := NewBit;                                                           //Still No Output.
  7. //  Sleep(100);                                                                 //Might Have to Wait a Bit.
  8.     if TheName = 'GPGKey' then NewBit := GPGKey.Output.NumBytesAvailable;       //If There Was Some Output From the Named Process.
  9.   until OldBit = NewBit;                                                        //Register it to Continue or Check For More Output.
  10.   if (TheName =  'GPGKey') and (NewBit > 0) then                                //Check if There Was Some Output.
  11.   begin                                                                         //If There Was
  12.     OutLst.LoadFromStream(GPGKey.Output);                                       //Load it Up.
  13.     for CountA := 0 to OutLst.Count-1 do
  14.     Memo1.Lines.Add(OutLst[CountA]);
  15.     Memo1.Lines.Add('End');
  16.     Memo1.Update;
  17.     GPGForm.Update;
  18.     Result := 00;                                                               //Start With No Result.
  19.     if AnsiContainsStr(OutLst[OutLst.Count-1],'Your sel')  then Result:=01 else //Assign Result.
  20.     if AnsiContainsStr(OutLst[OutLst.Count-1],'What key')  then Result:=02 else
  21.     if AnsiContainsStr(OutLst[OutLst.Count-1],'Key is v')  then Result:=03 else
  22.     if AnsiContainsStr(OutLst[OutLst.Count-1],'Is this ')  then Result:=04 else
  23.     if AnsiContainsStr(OutLst[OutLst.Count-1],'Real nam')  then Result:=05 else
  24.     if AnsiContainsStr(OutLst[OutLst.Count-1],'E-mail a')  then Result:=06 else
  25.     if AnsiContainsStr(OutLst[OutLst.Count-1],'Comment:')  then Result:=07 else
  26.     if AnsiContainsStr(OutLst[OutLst.Count-1],'/(Q)uit?')  then Result:=08 else
  27.     if AnsiContainsStr(OutLst[OutLst.Count-1],'You need')  then Result:=09 else
  28.     if AnsiContainsStr(OutLst[OutLst.Count-1],'You need')  then Result:=10;
  29.   end;                                                                          //Ends With 0 When Done.
  30. end;  

So basically I set up the Process and run it then read the output from it deciding what the next thing I should send is based on the final line of the output. First time around that should be Your selection?

However, if I bust things to see what is going on then... Picture attached. The output from the process ends with a blank line after There is NO WARRANTY, to the extent permitted by law. so I do not get to see the rest and the program thrashes about with itself. Note that the final End in the picture has been inserted by myself.

Once again... any ideas?


TinyPortal © 2005-2018