How to decrypt file using GpgAPI for c# without prompting for the password

4k views Asked by At

I've been trying to decrypt my file using GpgAPI in C#. It works fine, but it keeps prompting me for the password. The following code is what I used which is from the author's example.

public bool DecryptReport(string dataFileLocation, string filename)
        {
            log.Info("Starting GPG decryption.");

            GpgInterface.ExePath = @"C:\Program Files (x86)\GNU\GnuPG\gpg2.exe";

            String encryptedFile = dataFileLocation + filename;
            filename = filename.ToString().Substring(0, filename.ToString().IndexOf(".gpg")).ToString();
            string file = dataFileLocation + filename;

            try
            {
                log.Info("Decrypting " + file);
                GpgDecrypt decrypt = new GpgDecrypt(encryptedFile, file);
                decrypt.AskPassphrase = GetPassword;
                {
                    log.Info("Password received.");
                    GpgInterfaceResult result = decrypt.Execute();
                    Callback(result);
                }
            }
            catch(Exception e)
            {
                log.Error("Caught an exception" + e.InnerException);
                return false;
            }

            return true;
        }

        public static string Callback(GpgInterfaceResult result)
        {
            if(result.Status == GpgInterfaceStatus.Success)
            {
                return "successfully decrypted.";
            }
            else
            {
                return "Error was found during decryption. Check the log.";
            }

        }

        public static SecureString GetPassword(AskPassphraseInfo arg)
        {
            return GpgInterface.GetSecureStringFromString(“password$");

        }

What am I doing wrong in this code? Why does it not pass the password and continue decrypting instead of prompting for the password?

1

There are 1 answers

0
Diogo Luiz On BEST ANSWER

The gpg2 does not work as it should ... download the gpg classic. Visit https://www.gnupg.org/download/ go to GnuPG binary -> Windows -> Simple installer for GnuPG classic.

Change GpgInterface.ExePath = @"C:\Program Files (x86)\GNU\GnuPG\gpg2.exe"; to "C:\Program Files (x86)\GNU\GnuPG\gpg.exe";