OpenSSLによって生成された秘密RSAキーを使用して、C# .NET 3.5 で XML ファイルに署名しようとしています。
手順は次のとおりです。chilkat フレームワーク (www.example-code.com/csharp/cert_usePrivateKeyFromPEM.asp) を使用して、RSA キーをPEM形式から XML 形式に変換しました。
XML キーを使用して、私が好むネイティブ .NET 関数を使用できるようになりました。そこで、MSDNで説明されている方法を使用しました。
最終的に、私のソースコードは次のようになります。
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
//Load the private key from xml file
XmlDocument xmlPrivateKey = new XmlDocument();
xmlPrivateKey.Load("PrivateKey.xml");
rsaProvider.FromXmlString(xmlPrivateKey.InnerXml);
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(Doc);
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, true));
この関数で取得した署名付き XML は問題ないように見えます。次のように、ファイルの末尾に XML 要素があります。
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>qoGPSbe4oR9e2XKN6MzP+7XlXYI=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>iPQ6IET400CXfchWJcP22p2gK6RpEc9mkSgfoA94fL5UM6+AB5+IO6BbjsNt31q6MB8hR6lAIcnjzHzc5SeXvFP8Py2bqHTYJvcSA6KcKCQl1LiDNt12UwWiKpSkus2p0LdAeeZJNy9aDxjC/blUaZEr4uPFt0kGCD7h1NQM2SY=</SignatureValue>
問題は、この URL で xmlsec を使用して署名を検証しようとしたときです: http://www.aleksey.com/xmlsec/xmldsig-verifier.html。署名が無効であるというメッセージが表示されます。
コード内のエラーを何日も探していましたが、見つけることができません。PEM から XML ファイルへの変換に問題があるのではないかと考え始めていますが、これをテストする方法がわかりません。さらに、キーに変換したり、.NET で PEM ファイルを直接使用したりする他の方法は見つかりませんでした。
.NET で有効な署名を取得できた人はいますか?