充气城堡C#PGP解密的例子 [英] Bouncy Castle C# PGP Decryption example
本文介绍了充气城堡C#PGP解密的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我昨天一直在寻找了一整天,我似乎无法找到C#
解决方案使用充气城堡PGP解密的工作示例DIV>
终于得到它的工作。主要的问题我曾与其他样品是私钥环我已经包含了签约这是试图加载解密的密钥时,首先来了一个关键的事实。这就是为什么,为什么我不得不添加了检查 ElGamalPrivateKeyParameters
输入该按键。
下面是我的代码。不是很干净,但很有效。
私有静态PgpPrivateKey GetPrivateKey(字符串privateKeyPath)使用
{
使用(流KEYIN = File.OpenRead(privateKeyPath))
(流的InputStream = PgpUtilities.GetDecoderStream(KEYIN))
{
PgpSecretKeyRingBundle secretKeyRingBundle =新PgpSecretKeyRingBundle(InputStream的);
PgpSecretKey键= NULL;
的foreach(PgpSecretKeyRing克林在secretKeyRingBundle.GetKeyRings())
{
的foreach(PgpSecretKey SecretKey的在kRing.GetSecretKeys())
{
PgpPrivateKey的privKey = secretKey.ExtractPrivateKey (1234567890.ToCharArray());
如果(privKey.Key.GetType()==
typeof运算(Org.BouncyCastle.Crypto.Parameters.ElGamalPrivateKeyParameters))
//Org.BouncyCastle.Crypto.Parameters。 ElGamalPrivateKeyParameters
{
返回的privKey;
}
}
}
}
返回NULL;
}
公共静态无效解密(流输入,串outputpath,字符串privateKeyPath)
{
输入= PgpUtilities.GetDecoderStream(输入);
试
{
PgpObjectFactory pgpObjF =新PgpObjectFactory(输入);
PgpEncryptedDataList ENC;
PgpObject OBJ = pgpObjF.NextPgpObject();
如果(obj是PgpEncryptedDataList)
{
ENC =(PgpEncryptedDataList)目标文件;
}
,否则
{
ENC =(PgpEncryptedDataList)pgpObjF.NextPgpObject();
}
变种AKP =新AsymmetricKeyParameter(真);
PgpPrivateKey的privKey = GetPrivateKey(privateKeyPath);
PgpPublicKeyEncryptedData PBE = enc.GetEncryptedDataObjects()演员LT; PgpPublicKeyEncryptedData方式>()第一();
流清晰;
=清晰pbe.GetDataStream(的privKey);
PgpObjectFactory plainFact =新PgpObjectFactory(清晰);
PgpObject消息= plainFact.NextPgpObject();
如果(消息PgpCompressedData)
{
PgpCompressedData CDATA =(PgpCompressedData)消息;
流compDataIn = cData.GetDataStream();
PgpObjectFactory O =新PgpObjectFactory(compDataIn);
=消息o.NextPgpObject();
如果(消息PgpOnePassSignatureList)
{
=消息o.NextPgpObject();
PgpLiteralData LD = NULL;
LD =(PgpLiteralData)消息;
流输出= File.Create(outputpath +\\+ Ld.FileName);
流UNC = Ld.GetInputStream();
Streams.PipeAll(UNC,输出);
}
,否则
{
PgpLiteralData LD = NULL;
LD =(PgpLiteralData)消息;
//流输出= File.Create(outputpath +\\+ Ld.FileName);
流输出= File.Create(outputpath);
流UNC = Ld.GetInputStream();
Streams.PipeAll(UNC,输出);
}
}
}
赶上(例外五)
{
抛出新的异常(e.Message);
}
}
I have been looking all day yesterday, and I can't seem to find a working example of PGP decryption using Bouncy Castle in c#
解决方案
Finally got it to work. The main issue I had with other samples was the fact that the private key ring I had included a key for signing which was coming up first when trying to load the key for decryption. This is why why I had to add a check for the ElGamalPrivateKeyParameters
type on the key.
Below is my code. Not very clean, but it works.
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);
}
}
这篇关于充气城堡C#PGP解密的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文