2012-12-03 53 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