I am working on an application that will encrypt a file using a public key as part of an automated process. So far I have been able to write code that can capture the output of some of GPG's commands. For example, I am able execute 'gpg.exe --list-keys' and capture the output from stdout and parse the results.
currently the code can check for the existence of the public key in the keyring without any problems. In the case where the public key is not already in the keyring, I want to prompt the user to locate the key file. From the shell, I can call 'gpg.exe --dry-run --import c:\some\path\key.asc' and inside the shell gpg will display some basic information about the file. I want my program to be able to use this output to check that the user selected the file with the desired key in it. The problem that I am seeing the output during an import command can not be redirected.
The work around that I have is that I could import the file, then use list keys again to check again for the public key I want to use. However I consider this to be a sub optimal solution since this could cause the user to import keys that aren't needed.
What I am looking for is either a workaround that allows me to capture the output of an import command, or a really good reason why I can't do this. I expect that a good reason why I can't do this is going to be difficult because the key gets imported anyway, even if the output is not captured, and the output of a import command does not contain any information that is not also displayed in a list keys command. If the output of an import command is somehow meant to be secret, then why is it so easy to capture when listing keys? The second point is that this is a public key, there shouldn't be any harm in seeing any output related to a public key.
I've also heard the argument that you shouldn't parse the output of a program like GPG because later changes to the program can cause different output and would break my code, on this point I simply don't care. I'll update the code when that happens.
Start Free Trial