【问题标题】:How to decrypt file using GpgAPI for c# without prompting for the password如何在不提示输入密码的情况下使用 GpgAPI for c# 解密文件
【发布时间】:2015-06-23 02:05:38
【问题描述】:

我一直在尝试使用 C# 中的 GpgAPI 解密我的文件。它工作正常,但它一直提示我输入密码。以下代码是我使用的来自作者示例的代码。

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$");

        }

我在这段代码中做错了什么?为什么不通过密码继续解密,而不是提示输入密码?

【问题讨论】:

    标签: c# encryption gnupg


    【解决方案1】:

    gpg2 不能正常工作...下载 gpg 经典。 访问https://www.gnupg.org/download/ 转到 GnuPG 二进制 -> Windows -> GnuPG 经典版的简单安装程序。

    将 GpgInterface.ExePath = @"C:\Program Files (x86)\GNU\GnuPG\gpg2.exe"; 更改为 "C:\Program Files (x86)\GNU \GnuPG\gpg.exe";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 2016-05-27
      • 2015-04-08
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2022-07-28
      相关资源
      最近更新 更多