【发布时间】:2020-02-05 18:02:42
【问题描述】:
这是我所拥有的:
- 一个名为 update.zip 的大文件
- 名为 update.zip.sha256sum 的文本文件包含...是的,您猜对了,目前它是“204f687dae2e9b66afce079b29abf935949e31de838aa4f5bd8f9b4440fadf2d update.zip”(1 行)
- 名为 update.zip.sha256sum.asc 的第三个文件包含“-----BEGIN PGP SIGNATURE----- 版本: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)。
【问题讨论】: