我一直在看昨天一整天,我似乎无法找到在C#中使用充气城堡PGP解密的工作示例
Answer 1:
最后得到它的工作。 主要的问题我曾与其他样品是私钥环我已经包括了签约这是试图加载解密的密钥时,首先来了一个关键的事实。 这就是为什么,为什么我不得不添加一个检查的ElGamalPrivateKeyParameters
的密钥类型。
下面是我的代码。 不是很干净,但它的作品。
private static PgpPrivateKey GetPrivateKey(string privateKeyPath)
{
using (Stream keyIn = File.OpenRead(privateKeyPath))
using (Stream inputStream = PgpUtilities.GetDecoderStream(keyIn))
{
PgpSecretKeyRingBundle secretKeyRingBundle = new PgpSecretKeyRingBundle(inputStream);
PgpSecretKey key = null;
foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
{
foreach (PgpSecretKey secretKey in kRing.GetSecretKeys())
{
PgpPrivateKey privKey = secretKey.ExtractPrivateKey("1234567890".ToCharArray());
if (privKey.Key.GetType() ==
typeof (Org.BouncyCastle.Crypto.Parameters.ElGamalPrivateKeyParameters))
//Org.BouncyCastle.Crypto.Parameters.ElGamalPrivateKeyParameters
{
return privKey;
}
}
}
}
return null;
}
public static void Decrypt(Stream input, string outputpath, String privateKeyPath)
{
input = PgpUtilities.GetDecoderStream(input);
try
{
PgpObjectFactory pgpObjF = new PgpObjectFactory(input);
PgpEncryptedDataList enc;
PgpObject obj = pgpObjF.NextPgpObject();
if (obj is PgpEncryptedDataList)
{
enc = (PgpEncryptedDataList)obj;
}
else
{
enc = (PgpEncryptedDataList)pgpObjF.NextPgpObject();
}
var akp = new AsymmetricKeyParameter(true);
PgpPrivateKey privKey = GetPrivateKey(privateKeyPath);
PgpPublicKeyEncryptedData pbe = enc.GetEncryptedDataObjects().Cast<PgpPublicKeyEncryptedData>().First();
Stream clear;
clear = pbe.GetDataStream(privKey);
PgpObjectFactory plainFact = new PgpObjectFactory(clear);
PgpObject message = plainFact.NextPgpObject();
if (message is PgpCompressedData)
{
PgpCompressedData cData = (PgpCompressedData)message;
Stream compDataIn = cData.GetDataStream();
PgpObjectFactory o = new PgpObjectFactory(compDataIn);
message = o.NextPgpObject();
if (message is PgpOnePassSignatureList)
{
message = o.NextPgpObject();
PgpLiteralData Ld = null;
Ld = (PgpLiteralData)message;
Stream output = File.Create(outputpath + "\\" + Ld.FileName);
Stream unc = Ld.GetInputStream();
Streams.PipeAll(unc, output);
}
else
{
PgpLiteralData Ld = null;
Ld = (PgpLiteralData)message;
//Stream output = File.Create(outputpath + "\\" + Ld.FileName);
Stream output = File.Create(outputpath);
Stream unc = Ld.GetInputStream();
Streams.PipeAll(unc, output);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
Answer 2:
我跑与罗恩Harlev的解密函数保持输出文件,直到程序被终止的问题。 我加绕流的一个using语句数来克服这个问题。 我也换成了硬编码密码赞成的输入参数。 我希望有人认为这是有用的。
private static bool DecryptFile(Stream inputStream, string outputDir, char[] passPhrase, string privateKeyLoc)
{
try
{
using (var newStream = PgpUtilities.GetDecoderStream(inputStream))
{
PgpObjectFactory pgpObjF = new PgpObjectFactory(newStream);
PgpEncryptedDataList enc;
PgpObject obj = pgpObjF.NextPgpObject();
if (obj is PgpEncryptedDataList)
{
enc = (PgpEncryptedDataList)obj;
}
else
{
enc = (PgpEncryptedDataList)pgpObjF.NextPgpObject();
}
var akp = new AsymmetricKeyParameter(true);
PgpPrivateKey privKey = GetPrivateKey(privateKeyLoc, passPhrase, logger);
PgpPublicKeyEncryptedData pbe = enc.GetEncryptedDataObjects().Cast<PgpPublicKeyEncryptedData>().First();
using (Stream clear = pbe.GetDataStream(privKey))
{
PgpObjectFactory plainFact = new PgpObjectFactory(clear);
PgpObject message = plainFact.NextPgpObject();
if (message is PgpCompressedData)
{
PgpCompressedData cData = (PgpCompressedData)message;
Stream compDataIn = cData.GetDataStream();
PgpObjectFactory o = new PgpObjectFactory(compDataIn);
message = o.NextPgpObject();
if (message is PgpOnePassSignatureList)
{
message = o.NextPgpObject();
}
PgpLiteralData Ld = null;
Ld = (PgpLiteralData)message;
using (Stream output = File.Create(outputDir + "\\" + Ld.FileName))
{
Stream unc = Ld.GetInputStream();
Streams.PipeAll(unc, output);
}
}
}
}
return true;
}
catch (Exception e)
{
throw new Exception(e.Message);
return false;
}
}
文章来源: Bouncy Castle C# PGP Decryption example