【发布时间】:2021-09-30 18:20:31
【问题描述】:
我无法在 C# 中解密一些 XML 文件。
我对密码学知之甚少,但据我了解,我有一个受密码保护的证书,其中包含私钥和公钥。
通过将密码应用于证书,我获得了解密收到的文件所需的私钥。
我要解密的消息位于 PKCS7 (CMS?) 格式的文件中。我相信我需要解析文件,解密它们,然后取消签名。
我不知道如何做到这一点。我已经尝试了我发现的工具,这些工具显然可以帮助我完成这项任务,但我无法让它发挥作用。
此时,我真的很想知道;有什么我不明白的吗?还是我在工作中使用了错误的工具?
这是我在一堆代码中的一些尝试:
public string DecryptFailingExample()
{
var content = File.ReadAllBytes("encryptedFileWithDataThatIWant.xml.enc");
var cert = new X509Certificate2("certificateFile.pfx", "PasswordForFile");
//This code didn't work. rsa.Decrypt would throw: "The length of the data to decrypt is not valid for the size of this key."
//using RSA rsa = cert.GetRSAPrivateKey();
//var decryptedBytes = rsa.Decrypt(content, RSAEncryptionPadding.OaepSHA1); //I tried all possible paddings - Nothing worked
//So I tried this instead:
SignedCms signedCms = new SignedCms();
signedCms.Decode(content); //Crash: ASN1 corrupted data; The provided data is tagged with 'Universal' class value '13', but it should have been 'Universal' class value '16'.
var ecms = new EnvelopedCms();
//If I try to skip SignedCms and pass "content" directly to EnvelopedCms, I will get: "ASN1 bad tag value met."
ecms.Decode(signedCms.ContentInfo.Content); //Crash:
ecms.Decrypt(new X509Certificate2Collection(cert));
}
这是我想要解密的消息的样子:
-----开始PKCS7-----
MIAGCSqGSIb3DQEHA6BAMIACAQAxggGEMIIBgAIBADBoMFExCzAJBgNVBAYTAkRF MSQwIgYDVQQKExtFdXJvcGVhbiBFbmVyZ3kgRXhjaGFuZ2UgQUcxHDAaBgNVB4MT
等
3nPyd3c9iGyKhWdQPPr/SRWB/l9bCjkla81MgTcj1rRGQyPJXpkyxpc9U4YYZnxt eHkcJMVWDw9ErThok8nh/7bkE4KbWBaLZAac+9occ4deDrlu0wAAAAAAAAAAAAA=
-----结束PKCS7-----
编辑: 这是另一个不起作用的最小示例。
//Step 1: Load PKCS7 message from disk
var content = File.ReadAllBytes("encryptedFileWithDataThatIWant.xml.enc");
//Step 2: Load .pfx certificate from disk
var cert = new X509Certificate2("certificateFile.pfx", "Password");
//Step 3: Create instance of EnvelopedCms
var ecms = new EnvelopedCms();
//Step 4: Add certificate for decoding
ecms.Certificates.Add(cert);
//Step 5: Decode the PKCS7 message
ecms.Decode(content); //Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException: 'ASN1 bad tag value met.'
除了 .PFX 文件,我还有一个带有私有证书的 PEM 文件。它看起来像这样:
-----开始 RSA 私钥--
Proc-Type:4,加密 DEK-信息:AES-256-CBC,915538553FEFBA6A98930E4BFFDA1E68
Pk0FHxXNHukA62FSmuzzE+gqHgeauZr6z+lqTcbf55cakrQzHQOQUnR0w5kCFPPg
等
L7U2cWJpbNsRNmBcTyH1WWJ4hYoCqdl9G6Zey4y/EQbZl1DKXtmIiLZSneF0VU9u
-----结束 RSA 私钥-----
我尝试像这样导入密钥,但这似乎不起作用:
var pem = File.ReadAllBytes("pemFile.pem");
var pemChars = Encoding.UTF8.GetString(pem);
var rsa = RSA.Create();
rsa.ImportFromEncryptedPem(pemChars, "Password"); //System.ArgumentException: 'No supported key formats were found. Check that the input represents the contents of a PEM-encoded key file, not the path to such a file. (Parameter 'input')'
【问题讨论】:
-
是的,PKCS#7 指定 CMS,查找 RFC。您应该首先进行 PEM 解码,然后使用结果调用
EnvolopedCms#Decode(binaryDerEncoding)。通常消息是经过签名然后加密的(当然,如果它们被签名的话)。 -
@MaartenBodewes 谢谢你的提示。在 EnvelopedCms.Decode(binaryDerEncoding) 之前,您希望“PEM 解码”部分是什么样子?我承认我很难将我认为应该发生的事情翻译成代码。在我的脑海中,我应该能够:加载证书(.PFX),应用正确的密码->从证书中提取私钥->使用私钥解密消息。但根据你的回答,我想我错过了一步?对不起,如果我听起来很困惑。我是。
-
PKCS#7 是二进制编码。 PEM 使用带有页眉和页脚行的 base 64 将其转换为文本(最初是为了使其可以在 S/MIME/电子邮件中使用)。您当前拥有的代码似乎需要二进制编码。如果你给
Decode函数提供文本,它会在尝试对文本进行二进制解码时崩溃。因此,您需要一个已添加到 .NET 最新版本中的 PEM 解析器(我知道,因为我前段时间已经为它们编写了一些代码,开源)。当然任何 PEM 解析器都可以,Bouncy Castle 也包含一个。
标签: c# encryption rsa x509certificate pkcs#7