1
我一直試圖在C#中使用GpgAPI解密我的文件。它工作正常,但它不斷提示我輸入密碼。以下代碼是我使用的來自作者示例的代碼。如何在不提示輸入密碼的情況下使用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$");
}
我在做什麼錯在這段代碼?爲什麼它不傳遞密碼並繼續解密而不是提示輸入密碼?
這樣做!真棒! – user1828605