在c#

时间:2019-10-08 12:39:29

标签: c# pgp

这是我拥有的:

  • 一个名为update.zip的大文件
  • 一个名为update.zip.sha256sum的文本文件,包含...是的,您猜对了,当前为“ 204f687dae2e9b66afce079b29abf935949e31de838aa4f5bd8f9b4440fadf2d update.zip”(1行)
  • 第三个名为update.zip.sha256sum.asc的文件,其中包含“ ----- BEGIN PGP签名-----” 版本:GnuPG v1 ... ----- END PGP SIGNATURE -----“,因此只有签名。 所有这些都在网站上。
  • 然后,我当然拥有公用密钥来验证签名。我将其作为项目内部的资源,因为它不会更改。

我需要通过代码做什么:检查update.zip是否受到限制。因此,检查签名,确认文件update.zip.sha256sum没问题,因此我可以使用其中的sha256与我计算出的签名进行比较。 一切都很基本。 问题是:verify方法始终返回false。 从桌面应用程序执行此操作可以正常工作,因此可以确定文件没有问题。

我做了什么:当然,我在这里阅读了所有类似的问题,特别是this。我还阅读了所有充气城堡文档。那是快速和容易的-没有。

最后,我使用了其中一个示例,将其命名为.. \ crypto \ test \ src \ openpgp \ examples \ DetachedSignatureProcessor.cs中的一个

这是(失败的)代码:

    private static bool VerifySignature(string fileName, Stream inputStream, Stream keyIn)
    {
        inputStream = PgpUtilities.GetDecoderStream(inputStream);

        PgpObjectFactory pgpFact = new PgpObjectFactory(inputStream);
        PgpSignatureList p3 = null;
        PgpObject o = pgpFact.NextPgpObject();

        p3 = (PgpSignatureList)o;


        PgpPublicKeyRingBundle pgpPubRingCollection = new PgpPublicKeyRingBundle(
            PgpUtilities.GetDecoderStream(keyIn));
        Stream dIn = GetStream(fileName);
        PgpSignature sig = p3[0];
        PgpPublicKey key = pgpPubRingCollection.GetPublicKey(sig.KeyId);
        sig.InitVerify(key);

        int ch;
        while ((ch = dIn.ReadByte()) >= 0)
        {
            sig.Update((byte)ch);
        }

        dIn.Close();

        if (sig.Verify())
        {
            Console.WriteLine("signature verified.");
            return true;
        }
        else
        {
            Console.WriteLine("signature verification failed.");
            return false;
        }
    }

    public static Stream GetStream(string stringData)
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(stringData);
        writer.Flush();
        stream.Position = 0;
        return stream;
    }

代码运行愉快,但始终选择“签名验证失败”。路线。

我不确定是错过了一些步骤还是使用了错误的配料。我使用的3个是纯文本sha256sum文件(fileName),签名文件sha256sum.asc(inputStream)和公共密钥(keyIN)。

1 个答案:

答案 0 :(得分:0)

最后我解决了。但不要问哪个步骤有所不同;-)

 public static bool VerifySignature(string fileName, Stream inputStream, Stream keyIn)
    {
        PgpPublicKeyRingBundle pgpPubRingCollection = new PgpPublicKeyRingBundle(keyIn);
        PgpPublicKey pubicKey = pgpPubRingCollection.GetPublicKey(4100000000000000000);    //once I knew the right one, I put it here directly as it won't change anyway
        Stream signaturStrom = PgpUtilities.GetDecoderStream(inputStream);
        PgpObjectFactory pgpWTFactory = new PgpObjectFactory(signaturStrom);
        PgpSignature signtr = ((PgpSignatureList)pgpWTFactory.NextPgpObject())[0];
        signtr.InitVerify(pubicKey);

        try
        {
            Stream dIn = ReadStream(fileName);  // get stream from ext. file

            int ch;
            while ((ch = dIn.ReadByte()) >= 0)
            {
                signtr.Update((byte)ch);
            }
            dIn.Close();

        }
        catch (Exception ex)
        {
            return false;
        }
        if (signtr.Verify())
        {
            Console.WriteLine("signature verified.");
            return true;
        }
        else
        {
            Console.WriteLine("signature verification failed.");
            return false;
        }
    }