3

PackageDigitalSignatureManager クラスを使用して、署名欄プロバイダーで Office ドキュメント (Word\Excel) にプログラムで署名しようとしていますが、サポートされていないハッシュ アルゴリズムの例外が発生しました。

一般的な例外: System.InvalidOperationException: サポートされていないハッシュ アルゴリズムが指定されました。   
MS.Internal.IO.Packaging.XmlDigitalSignatureProcessor.Sign (IEnumerable`1 パーツ、IEnumerable`1 relationshipSelectors、X509Certificate2 署名者、文字列 signatureId、ブール型のembedCertificate、IEnumerable`1 signatureObjects、IEnumerable`1 objectReferences) で    
System.IO.Packaging.PackageDigitalSignatureManager.Sign (IEnumerable`1 パーツ、X509Certificate 証明書、IEnumerable`1 relationshipSelectors、文字列 signatureId、IEnumerable`1 signatureObjects、IEnumerable`1 objectReferences) で

ここに私のコードの一部があります:

string SigInfoHashAlg = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
string HashAlg = "http://www.w3.org/2001/04/xmlenc#sha256";

PackageDigitalSignatureManager dsm = new PackageDigitalSignatureManager(package);
dsm.CertificateOption = CertificateEmbeddingOption.InSignaturePart;
dsm.HashAlgorithm = SigInfoHashAlg;
System.Collections.Generic.List<Uri> toSign = new System.Collections.Generic.List<Uri>();
System.Collections.Generic.List<PackageRelationshipSelector> relationshipSelectors = new System.Collections.Generic.List<PackageRelationshipSelector>();
foreach (PackageRelationship relationship in package.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"))
{
  AddSignableItems(relationship, toSign, relationshipSelectors);
}

X509Certificate cert = new X509Certificate(Cert);
System.Collections.Generic.List<System.Security.Cryptography.Xml.DataObject> signatureObjects = new System.Collections.Generic.List<System.Security.Cryptography.Xml.DataObject>();
System.Collections.Generic.List<Reference> objectReferences = new System.Collections.Generic.List<Reference>();
Reference officeItemRef = new Reference(_OfficeUri);
officeItemRef.Type = "http://www.w3.org/2000/09/xmldsig#Object";
try{
    NewSig = dsm.Sign(toSign, cert, relationshipSelectors, "idPackageSignature", signatureObjects, objectReferences);
}
catch (CryptographicException ex)
{
    throw ex;
}

PackageDigitalSignatureManager クラスを使用して多くのページを検索して見つけましたが、この手順に関する実際の例や説明はありません。

私の調査から理解したように、dsm.HashAlgorithm は署名アルゴリズムに影響を与えるパラメーターである必要があります (.Net フレームワーク XmlDigitalSignatureProcessor のソース コードでも見られます)。

SignatureDescription クラスが解決策になると誰かが思うことはわかっていますが、私もこれを試しました。

  1. 関連するハッシュ ID で動作するように machine.config を構成しました
  2. SHA1 は適切に機能しており、これまで SHA1 は一種のハード コードでした。その他の情報が必要な場合は、お気軽にお問い合わせください。
  3. 私は Net Framework 3.0 について話しているので、4.0 のソリューションは他に選択肢がない (最後の手段) と言わない限り関係ありません。

どうもありがとう、アレックス。

4

0 に答える 0