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;
    process.StandardInput.WriteLine(sCommandLine);
process.StandardInput.Flush();
    process.StandardInput.Close();
     process.WaitForExit();
     //string result = process.StandardOutput.ReadToEnd();
     //string error = process.StandardError.ReadToEnd();
    process.Close();
     return decryptedFileName;
}

See also



Signing Soap Message With X509 Certificate

Generate A New Key Pair - GnuPG

ads

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

1 comentarios:

  1. Look here!! http://systemdeveloperpy.blogspot.com/

    ReplyDelete