November 7, 2013

Decrypt files encrypted with GnuPG FROM C#

Decrypt files encrypted with GnuPG FROM C#

During these last days I saw the need to encrypt files with GnuPG program at the same time I had to be able to decode the same file
have a C #.
GnuPG To decrypt an encrypted file without using a DLL, use the Process class
I leave the method I have used for this purpose, hope it helps.

In the web.config we need to add the following key:

<add key="passphrase" value="1324356asdf"/>

<add key="WorkingDirectory" value="C:\Program Files\GNU\GnuPG"/>

public static string DecryptFile(string encryptedFilePath)
     FileInfo info = new FileInfo(encryptedFilePath);
     string decryptedFileName = info.FullName.Substring(0, info.FullName.LastIndexOf('.')) + "Dec.TXT";
     string encryptedFileName = info.FullName;
     string password = System.Configuration.ConfigurationManager.AppSettings["passphrase"].ToString();         
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");           
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
psi.WorkingDirectory = @System.Configuration.ConfigurationManager.AppSettings["WorkingDirectory"].ToString();
     System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);
     string sCommandLine = @"echo " + password + "|gpg.exe --passphrase-fd 0 --batch --verbose --yes --output " + decryptedFileName + @" --decrypt """ + encryptedFileName;
     //string result = process.StandardOutput.ReadToEnd();
     //string error = process.StandardError.ReadToEnd();
     return decryptedFileName;

See also

Signing Soap Message With X509 Certificate

Generate A New Key Pair - GnuPG

Ditulis Oleh : Angelo Hari: 1:06 PM Kategori:

3 comentarios:

  1. Look here!!

  2. This is great work. Thanks for sharing it.

  3. Thanks This is Exactly what I have been looking for!

    I finally got GPG working in my .NET Application now. There wasn't much information on how to do exactly this but this works!

    Thanks a ton.