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

BouncyCastle Open PGP - unknown object in stream 47

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

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

unknown object in stream 47

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

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

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

目前我的代码如下所示:

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

这是我在 https://www.igolder.com/PGP/generate-key/

生成的测试密钥和 hello world 消息
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG C# v1.6.1.0

mQENBFoAOVkBCACQs2N2Z/b6Fpx+w6Et380qVCtwxJ6GBCRAgwVLGNSKoPJ/L7Sh
dBYJAY5hAWWkSNRislYUG7iykK3JtJVOetlts1xFQX5gVVABZphGx9OtbGo95dUJ
vxV3w7cpXwHPB45skd2qjzIZLh8PY0BYC4Rf8SIa2LBhPh5CqT339Z8h0dGBhykj
X8LyzCVnKVjUUZpaGHoM5zo3jtIhxBgYs6WowuWaMtpEynQW753klBIW+d7UsJ2x
zrHu3SNE4hU5ayNvZKwx3Mb1LRf2ydqaniyLoL4nrPzeIr19vBHRfr52itjcG1hD
4PoD5zECwKK4KOqfPAjg/8fKa/oDZserdIxnABEBAAG0AIkBHAQQAQIABgUCWgA5
WQAKCRC3T9Uo2/H1P2yQCACMjLPb3z1hRErwevJtyEvaixIovVS/I/8anW9t9egr
yaoufqC0WPGN8nDrFXRInYeV3hV1C6qSq1BUGsOhS1aFDsjqqUdtQ1oIemAs9BnV
trD/noOkR11THtA2+gO3nyIo6vLADiqDQ5RQjzOMTwGVAHJjf8JpxvlIyrb/09sO
j1CmhcZfUPYNLV15h3XWHLk1dJhtYTjKYtH+kbS2THln/0LOCd/h9+rdmojQmtau
8wkgeX9vhiaGItZ+xjTb7jNTTn+fzO3IkeffgklIZbhX7/NqUKKNFUKYzHDwuS2L
uTgd1VhOQWfI/ZTIwmVqoCMOlYdtmrsl12tBp0JZUXJS
=8xO7
-----END PGP PUBLIC KEY BLOCK-----

-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: BCPG C# v1.6.1.0

lQOsBFoAOVkBCACQs2N2Z/b6Fpx+w6Et380qVCtwxJ6GBCRA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vJYl6GoqO9ymDrelDWkSWFP6yaKiRR1e0mTbwX
UukQww0L8dJEYkKsy9rTFUTGq/PXRXxN/FVtNtLHEX8zGmBWUZgjpLwq+cc8CWmo
+vJnW5nfWSNySKv1aR0WvTTGK4hndJ3rpt1coUlrrbQAiQEcBBABAgAGBQJaADlZ
AAoJELdP1Sjb8fU/bJAIAIyMs9vfPWFESvB68m3IS9qLEii9VL8j/xqdb2316CvJ
qi5+oLRY8Y3ycOsVdEidh5XeFXULqpKrUFQaw6FLVoUOyOqpR21DWgh6YCz0GdW2
sP+eg6RHXVMe0Db6A7efIijq8sAOKoNDlFCPM4xPAZUAcmN/wmnG+UjKtv/T2w6P
UKaFxl9Q9g0tXXmHddYcuTV0mG1hOMpi0f6RtLZMeWf/Qs4J3+H36t2aiNCa1q7z
CSB5f2+GJoYi1n7GNNvuM1NOf5/M7ciR59+CSUhluFfv82pQoo0VQpjMcPC5LYu5
OB3VWE5BZ8j9lMjCZWqgIw6Vh22auyXXa0GnQllRclI=
=XAmm
-----END PGP PRIVATE KEY BLOCK-----

-----BEGIN PGP MESSAGE-----
Version: BCPG C# v1.6.1.0

hQEMA7dP1Sjb8fU/AQf9ERc725Bql1snAN5R/j/xbkRqtdmGPAbYkJQX/IcJYu9R
1txnd7Y7xH8FMj1a/QLB5migRO8O3UQ2KHiBTe/+QyeB+FiJVtaRndG/VDXUHATy
PRnusYszaYVA8YBAirDqjuuGqhIXym4v9jbyBJWYXNJnrJOPK+C7A6JQEKbQjdwX
vFj07faEDpn8RE6PnvU55EqcBH7D7uSMMKElknHUfyMyLrHOkrYky3uHKG/hF4QS
XE8jBcpNHBg5p35KH99eJafulhvt0XhGcKS1z41ftFNzGxcLLoxHWNZEGj2RG54h
m+5iWY0Ypqg4aTMm/SxWj41pBcc5B4deW8pO6cxC0Mkpjc/4FRl2/ZmnHr4BGIft
atuzKN21w4fIPkaNkz1NgfYbJs4Y/lnWjrg=
=wxc0
-----END PGP MESSAGE-----

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

谢谢

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

unknown object in stream 47

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

VS 默认使用 Unicode UTF-8 编码。

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

  • 从菜单 select 文件 -> 另存为
  • 在 "Save" 按钮上会有一个下拉菜单,您可以在其中 select "Save with Encoding"
  • Select US-ASCII