2012-12-03 52 views
3

我试图使用名为PgpDecrypt的类来解密客户端给出的这个示例文件。但是,当代码涉及到这一行:使用BouncyCastle解密文件的例外PGP

Stream clear = pbe.GetDataStream(privKey); 

它返回一个错误:例外解密密钥

这里是我的解密代码:

PgpDecrypt test = new PgpDecrypt(string.Concat(pathh, "TestDecryptionFile"), 
              string.Concat(pathh, "mypgpprivatekey.key"), 
              "mypassphrase", 
              @"d:/test/", 
              string.Concat(pathh, "clientpublickey.key")); 

FileStream fs = File.Open(string.Concat(pathh, "TestDecryptionFile"), FileMode.Open); 
test.Decrypt(fs, @"d:\test\"); 

我使用BouncyCastle的是我的第三个.NET的第三方库。

任何想法解决这将是一个很大的帮助。提前致谢!

+1

在哪里分配了“pathh”? –

+0

文件所在的本地路径... @“D:\ Users \ MyUser \ Documents \ Visual Studio 2008 \ Projects \ sFTPwithPGP \ keys \ keys26112012 \” – iceheaven31

+1

为了增加构建路径时的安全性,您应该使用[Path.Combine] (http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx)。 –

回答

5

如果您关注的BouncyCastle的类PGPEncrypt,PGPDecrypt和PGPEncryptionKeys ...

下PGPEncryptionKeys类,添加这个方法:

/// <summary> 
/// Return the last key we can use to decrypt. 
/// Note: A file can contain multiple keys (stored in "key rings") 
/// </summary> 
private PgpSecretKey GetLastSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle) 
{ 
    return (from PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings() 
      select kRing.GetSecretKeys().Cast<PgpSecretKey>() 
              .LastOrDefault(k => k.IsSigningKey)) 
              .LastOrDefault(key => key != null); 
} 

仍在PgpEncryptionKeys类中,确保ReadSecretKey方法是这样的:

​​

^_^

+1

工作完美。对于任何人谁在这一点上,不要忘了修改构造函数,如: '公共PgpEncryptionKeys(字符串publicKeyPath,字符串privateKeyPath,字符串passPhrase,bool toEncrypt)' – Corwin01

+0

感谢您指出了这一点。我很高兴它也帮助你。 :) – iceheaven31

+1

是的。现在工作完美=) – Corwin01