BouncyCastle Open PGP - 流 47 中的未知对象

Bar*_*osz 7 c# encryption bouncycastle pgp

我正在尝试使用 BouncyCastle PGP SDK 解密 PGP 加密字符串。

我尝试了几种方法,在每种情况下,我都会在过程开始时遇到以下错误(具体位置如下所示):

流 47 中的未知对象

到目前为止,我已经尝试了两个 nuget 包:

  • BouncyCastle.OpenPGP v.1.8.1。
  • BouncyCastle-Ext v1.7.0

我尝试了两篇文章中介绍的方法:

我尝试了三种获取测试密钥和加密字符串的方法:

目前我的代码如下所示:

  public void DecryptFile(string inputFilePath, string outputPath)
    {
        using (FileStream fsEncryptedFile = File.Open(inputFilePath, FileMode.Open))
        using (Stream decoderStream = PgpUtilities.GetDecoderStream(fsEncryptedFile))
        {
            PgpObjectFactory factory = new PgpObjectFactory(decoderStream);
            PgpObject obj = factory.NextPgpObject(); //<-- ERROR THROWN HERE
            if (!(obj is PgpEncryptedDataList))
            {
                obj = factory.NextPgpObject(); 
            }
            PgpEncryptedDataList edl = obj as PgpEncryptedDataList;

            foreach (PgpPublicKeyEncryptedData data in edl.GetEncryptedDataObjects())
            {
                PgpPrivateKey privateKey = null;
                using (FileStream fsKeyring = File.Open(this.privateKeyPath, FileMode.Open))
                {
                    PgpSecretKeyRingBundle bundle = new PgpSecretKeyRingBundle(fsKeyring); // <-- also same error can happen  here if I skip the error above
                    PgpSecretKey secretKey = bundle.GetSecretKey(data.KeyId);
                    privateKey = secretKey.ExtractPrivateKey(this.signature.ToCharArray());
                }
                PgpObjectFactory plainFactory = new PgpObjectFactory(data.GetDataStream(privateKey));
                PgpObject message = plainFactory.NextPgpObject();
                if (message is PgpCompressedData)
                {
                    message = new PgpObjectFactory(((PgpCompressedData) message).GetDataStream()).NextPgpObject();
                }
                if (message is PgpLiteralData)
                {
                    PgpLiteralData ld = (PgpLiteralData) message;
                    using (FileStream outStream = File.Create(outputPath))
                    {
                        Stream inStream = ld.GetInputStream();
                        byte[] buffer = new byte[0x100000];
                        int count = inStream.Read(buffer, 0, buffer.Length);
                        while (count > 0)
                        {
                            outStream.Write(buffer, 0, count);
                            count = inStream.Read(buffer, 0, buffer.Length);
                        }

                        outStream.Close();
                    }
                }
                else
                {
                    Console.WriteLine("ERROR: Unknown type of message in PGP file: {0}", message.GetType().FullName);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我在https://www.igolder.com/PGP/generate-key/生成的测试密钥和 hello world 消息

  public void DecryptFile(string inputFilePath, string outputPath)
    {
        using (FileStream fsEncryptedFile = File.Open(inputFilePath, FileMode.Open))
        using (Stream decoderStream = PgpUtilities.GetDecoderStream(fsEncryptedFile))
        {
            PgpObjectFactory factory = new PgpObjectFactory(decoderStream);
            PgpObject obj = factory.NextPgpObject(); //<-- ERROR THROWN HERE
            if (!(obj is PgpEncryptedDataList))
            {
                obj = factory.NextPgpObject(); 
            }
            PgpEncryptedDataList edl = obj as PgpEncryptedDataList;

            foreach (PgpPublicKeyEncryptedData data in edl.GetEncryptedDataObjects())
            {
                PgpPrivateKey privateKey = null;
                using (FileStream fsKeyring = File.Open(this.privateKeyPath, FileMode.Open))
                {
                    PgpSecretKeyRingBundle bundle = new PgpSecretKeyRingBundle(fsKeyring); // <-- also same error can happen  here if I skip the error above
                    PgpSecretKey secretKey = bundle.GetSecretKey(data.KeyId);
                    privateKey = secretKey.ExtractPrivateKey(this.signature.ToCharArray());
                }
                PgpObjectFactory plainFactory = new PgpObjectFactory(data.GetDataStream(privateKey));
                PgpObject message = plainFactory.NextPgpObject();
                if (message is PgpCompressedData)
                {
                    message = new PgpObjectFactory(((PgpCompressedData) message).GetDataStream()).NextPgpObject();
                }
                if (message is PgpLiteralData)
                {
                    PgpLiteralData ld = (PgpLiteralData) message;
                    using (FileStream outStream = File.Create(outputPath))
                    {
                        Stream inStream = ld.GetInputStream();
                        byte[] buffer = new byte[0x100000];
                        int count = inStream.Read(buffer, 0, buffer.Length);
                        while (count > 0)
                        {
                            outStream.Write(buffer, 0, count);
                            count = inStream.Read(buffer, 0, buffer.Length);
                        }

                        outStream.Close();
                    }
                }
                else
                {
                    Console.WriteLine("ERROR: Unknown type of message in PGP file: {0}", message.GetType().FullName);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

知道为什么我经常收到这个错误吗?

谢谢

D. *_*ott 6

当我调用PgpPublicKeyRingBundle函数时,我在使用 Visual Studio 2015 和 Bouncy-Castle 库时遇到了这个问题。

流 47 中的未知对象

我使用 Visual Studio 文本文件创建了 PublicKey.asc 和 PrivateKey.asc 文本文件。

默认情况下,VS 使用 Unicode UTF-8 编码。

您需要将其更改为 US-ASCII。

  • 从菜单中选择文件 -> 另存为
  • 在“保存”按钮上会有一个下拉菜单,您可以在其中选择“使用编码保存”
  • 选择 US-ASCII