Why is the first line missing after encrypting and decrypting when calling GnuPG from C#?

StackOverflow https://stackoverflow.com/questions/22500043

  •  17-06-2023
  •  | 
  •  

質問

I am encrypting a file using GnuPG in Visual Studio 2012 using C#.

When I decrypt this encrypted file it doesn't show row headers in the file. Below is what I have written for the encryption.

string gpgOptions = "--homedir "C:\GnuPG" --batch --yes --encrypt --armor --recipient ankit.gupta1@nirvana-sol.com --default-key ankit.gupta1@nirvana-sol.com --passphrase-fd 0 --no-verbose"; 
string inputText = "Name, Age
                    Dave, 19
                    Ryan, 21";

ProcessStartInfo pInfo = new ProcessStartInfo(gpgExecutable, gpgOptions);

pInfo.CreateNoWindow = true;
pInfo.UseShellExecute = false;

pInfo.RedirectStandardInput = true;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
_processObject = Process.Start(pInfo);
_processObject.StandardInput.Write(inputText);

I decrypt it using stdin with the below command:

gpg  --output my.csv --decrypt A.gpg

my.csv is:

               "Dave, 19
                Ryan, 21";

I want it to be decrypted with the headers. What am I missing here?

役に立ちましたか?

解決

You're telling gpg to read the passphrase from stdin using --passphrase-fd 0. Because of this, GnuPG will use everything up to the first newline as password.

This works anyway, as encryption does not require any passphrase. Try this (example is for unix systems, not Windows, but probably wouldn't look much different over there) to demonstrate the problem:

$ cat <<EOT | gpg -e --passphrase-fd 0 --recipient email@jenserat.de | gpg -d
> foo
> bar
> EOT
Reading passphrase from file descriptor 0

You need a passphrase to unlock the secret key for
user: "Jens Erat <email@jenserat.de>"
2048-bit RSA key, ID 3A5E68F7, created 2010-12-12 (main key ID D745722B)

gpg: encrypted with 2048-bit RSA key, ID 3A5E68F7, created 2010-12-12
      "Jens Erat <email@jenserat.de>"
bar

Solution: Remove --passphrase-fd 0 from the parameters.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top