Share via


簽署檔

本主題描述如何簽署 XPS 檔。

在程式中使用下列程式碼範例之前,請先閱讀一般數位簽章程式設計 工作中的 免責聲明。

若要簽署 XPS 檔,請先將它載入簽章管理員,如初始化簽章管理員 中所述

若要簽署已載入至簽章管理員的檔:

  1. 具現化 IXpsSigningOptions 介面。
  2. 設定簽署原則。
  3. 設定簽章方法。 簽章方法 URI 字串常數定義于 cryptxml.h 中。 如需有效簽章方法值的詳細資訊,請參閱 IXpsSigningOptions::SetSignatureMethod
  4. 設定摘要方法。 摘要方法 URI 字串常數定義于 cryptxml.h 中。 如需有效摘要方法值的相關資訊,請參閱 IXpsSigningOptions::SetDigestMethod
  5. 載入憑證,如從檔案 載入憑證中所述
  6. 確認憑證支援簽章方法,如確認憑證支援簽章方法 中所述
  7. 確認系統支援摘要方法,如驗證系統支援摘要方法 中所述
  8. 如有需要,請在 XPS 檔中內嵌憑證信任鏈結的憑證,如在檔中內嵌憑證鏈結 中所述
  9. 簽署 XPS 檔。

下列程式碼範例說明如何在程式中使用上述步驟。

    // this example requires:
    //        cryptxml.h
    // and refers to local methods that are described
    // in other topics

    HRESULT                hr               = S_OK;
    BOOL                   supported        = FALSE;
    BOOL                   succeeded        = FALSE;
    IXpsSigningOptions     *signingOptions  = NULL;
    IXpsSignature          *signature       = NULL;
    PCCERT_CONTEXT         certificate      = NULL;
    
    // Instantiate an IXpsSigningOptions interface.
    hr = signatureManager->CreateSigningOptions (&signingOptions);
    
    if (SUCCEEDED(hr)) {
        // Set the signing policy to indicate the document parts 
        //  to sign.
        hr = signingOptions->SetPolicy (XPS_SIGN_POLICY_CORE_PROPERTIES);
    }

    if (SUCCEEDED(hr)) {
        // Set the digital signature method to use to generate the 
        //    signature hash value. 
        //
        // The signature method used in this example is 
        //    defined in cryptxml.h.
        hr = signingOptions->SetSignatureMethod (
            wszURI_XMLNS_DIGSIG_RSA_SHA1);
    }

    if (SUCCEEDED(hr)) {
        // Set the digest method to use.
        //
        // The digest method used in this example is 
        //    defined in cryptxml.h.
        hr = signingOptions->SetDigestMethod (wszURI_XMLNS_DIGSIG_SHA1);
    }

    if (SUCCEEDED(hr)) {
        // Load a certificate from a certificate file
        hr = LoadCertificateFromFile (signingCertificate, &certificate);
    }

    if (SUCCEEDED(hr)) {
        // Verify the certificate supports the digest method
        supported = SupportsDigestAlgorithm (
            wszURI_XMLNS_DIGSIG_SHA1);
        if (!supported) hr = E_FAIL;
    }

    if (SUCCEEDED(hr)) {
        // Verify the signature method is supported by the certificate
        //  and the system
        supported = SupportsSignatureAlgorithm(
            wszURI_XMLNS_DIGSIG_RSA_SHA1, certificate);
        if (!supported) hr = E_FAIL;
    }

    if (SUCCEEDED(hr)) {
        // Embed the certificate trust chain in the XPS package (optional).
        hr = EmbedCertificateChainInXpsPackage (signingOptions, certificate);
    }

    if (SUCCEEDED(hr)) {
        // Sign the XPS document
        hr = signatureManager->Sign (signingOptions, certificate, &signature);
    }

 //<Free the certificate context
    if (NULL != certificate) CertFreeCertificateContext (certificate);

    if (NULL != signingOptions) signingOptions->Release();
    if (NULL != signature) signature->Release();

後續步驟

將簽章要求新增至 XPS 檔

驗證檔簽章

用於本節

CertFreeCertificateCoNtext

IXpsSignatureManager

IXpsSignatureManager::CreateSigningOptions

IXpsSignatureManager::Sign

IXpsSigningOptions

IXpsSigningOptions::SetDigestMethod

IXpsSigningOptions::SetPolicy

IXpsSigningOptions::SetSignatureMethod

XPS_SIGN_POLICY

詳細資訊

密碼編譯 API

密碼編譯函式

從檔案載入憑證

確認憑證支援簽章方法

確認系統支援 Digest 方法

在檔中內嵌憑證鏈結

XPS 數位簽章 API 錯誤

XPS 檔錯誤

XML 紙張規格