我发现这篇文章与pkcs#7解密相关,这表明没有开箱即用的解密签名pkcs#7文件的方法。 Decrypting PKCS#7 encrypted data in C#
但是,我也看到了这篇文章,并设法使用SignedCms类进行验证。 http://blogs.msdn.com/b/shawnfa/archive/2006/02/27/539990.aspx
在帖子结束时,使用 cms.ContentInfo.Content 提取数据似乎返回签名文件(.zip.p7)而不是实际内容(.zip)。我在这里错过了什么吗?使用充气城堡,我能够获得实际数据,但如果没有必要,我宁愿不使用第三方。
static void Main(string[] args)
{
var encodedFile = File.ReadAllBytes(InPath);
var signedData = new SignedCms();
signedData.Decode(encodedFile);
signedData.CheckSignature(true);
if (!Verify(signedData))
throw new Exception("No valid cert was found");
var trueContent = new CmsSignedData(File.ReadAllBytes(InPath)).SignedContent;
using (var str = new MemoryStream())
{
trueContent.Write(str);
var zip = new ZipArchive(str, ZipArchiveMode.Read);
zip.ExtractToDirectory(OutPath);
}
//using (var str = new MemoryStream(signedData.ContentInfo.Content))
//{
// var zip = new ZipArchive(str, ZipArchiveMode.Read);
// zip.ExtractToDirectory(OutPath);
//}
}
static bool Verify(SignedCms signedData)
{
var myCetificates = new X509Store(StoreName.My, StoreLocation.LocalMachine);
myCetificates.Open(OpenFlags.ReadOnly);
var certs = signedData.Certificates;
return (from X509Certificate2 cert in certs
select myCetificates.Certificates.Cast<X509Certificate2>()
.Any(crt => crt.Thumbprint == cert.Thumbprint))
.Any();
}
更新:将返回base64示例文件。需要提出一个不包含敏感信息的内容。
更新:这是我可以使用充气城堡提取但不使用ContentInfo.Content属性的文件的基本64版本。我已经在代码中添加了一条评论,我将如何直接提取SignedCms。
MIIFfQYJKoZIhvcNAQcCoIIFbjCCBWoCAQExCzAJBgUrDgMCGgUAMIHOBgEAoIHIBIHFUEsDBBQAAAAIAGRuWEggPN74JwAAADsAAAAMAAAAdGVzdGRhdGEudHh0bcixDQAgCEXB3sQd / gq6EVFaLHz7hwHgysM / uobpvMCDOUppdbkTUEsBAj8AFAAAAAgAZG5YSCA83vgnAAAA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