简体   繁体   English

充气城堡 C# PGP 解密示例

[英]Bouncy Castle C# PGP Decryption example

我昨天一整天都在找,似乎找不到在 C# 中使用 Bouncy Castle 进行 PGP 解密的工作示例

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.这就是为什么我必须在密钥上添加对ElGamalPrivateKeyParameters类型的检查的ElGamalPrivateKeyParameters

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();
            }

       


            
            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);
        }
    }

I was running into issues with Ron Harlev's Decrypt function holding the output file until the program was terminated.在程序终止之前,我遇到了 Ron Harlev 的 Decrypt 函数保存输出文件的问题。 I added a few using statements around the Stream's to overcome this issue.我在 Stream 周围添加了一些 using 语句来解决这个问题。 I also replaced the hard coded passphrase in favor of an input argument.我还替换了硬编码的密码短语以支持输入参数。 I hope somebody finds this useful.我希望有人觉得这很有用。

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();
                }

                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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM