IronPDF 操作指南 使用 HSM 签署 PDF How to Digitally Sign PDFs with C# using HSM Curtis Chau 已更新:十一月 3, 2025 Download IronPDF NuGet 下载 DLL 下载 Windows 安装程序 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English Adding a signature to a PDF document is a common requirement in many applications. However, mission-critical applications require a higher level of security where the key itself cannot be tampered with. A normal signing operation with a .pfx file is akin to having a master key at your house. The application needs to load the key into your computer's memory to sign the document. If the computer itself is compromised, the key may be stolen. A far more secure alternative is using a Hardware Security Module (HSM). With an HSM (like your USB token), the private key is generated inside the device and is physically incapable of ever leaving it. This process is akin to bringing the document to the bank. The application provides a PIN, and the "bank manager" (the HSM) takes the document into the vault, stamps it with the key, and hands the stamped document back. The main aspect is that the key never leaves the vault. This provides an additional security measure, as the key cannot be copied or stolen. How to digitally Sign PDFs with HSM Download the IronPDF C# library for signing PDFs with HSM Import your existing HSM or simulate it with other libraries Create a new UsbPkcs11HsmSigner object Sign and save the PDF with SignAndSave Verify the PDF output and certificate with a PDF viewer 今天在您的项目中使用 IronPDF,免费试用。 第一步: 免费开始 Signing with an HSM Signing with an HSM typically requires a physical device, such as a USB token, that the application interacts with. IronPDF is fully compatible with these operations, as both the library and standard HSMs often use PKCS#11 as a common API. For demonstrative purposes regarding IronPDF's functionality with HSMs, this guide will use a simulated HSM instead of a physical one. In a production or live testing environment, you should not use this simulation. Instead, you must use your actual HSM. To run this simulation, you must first install SoftHSM, OpenSSL, and OpenSC to generate the necessary key and token. For more information on how to utilize SoftHSM, please refer to their public GitHub repository. We'll start by creating a PDF from an HTML string. In the example below, we define the paths and credentials for our simulated SoftHSM. This includes providing the absolute path to the SoftHSM .dll library file and the .crt certificate file that you created. Next, we specify the output path, which in this instance is output.pdf. Furthermore, we define three strings: hsmTokenLabel, hsmPin, and hsmKeyLabel. These strings are case-sensitive and must exactly match the credentials you created when generating the token and certificate with SoftHSM. Afterwards, we initialize the UsbPkcs11HsmSigner object, passing the SoftHSM library path, PIN, token label, and key label as parameters. We additionally create a PdfSignatureImage to add a visual representation of the signature onto the document. Finally, we call SignAndSave, which uses the hsmSigner we configured to sign the document and save it to the specified output path. Code :path=/static-assets/pdf/content-code-examples/how-to/signing-with-hsm.cs using IronPdf; using IronPdf.Signing; using IronSoftware.Pdfium.Signing; using System.Drawing; ChromePdfRenderer renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Testing</h1>"); // Define Paths and Credentials string softhsmLibraryPath = @"D:\SoftHSM2\lib\softhsm2-x64.dll"; // These MUST match what you created string hsmTokenLabel = "MyTestToken"; string hsmPin = "123456"; string hsmKeyLabel = "my-key"; // The label for the key *inside* the token // Create the HsmSigner object. UsbPkcs11HsmSigner hsmSigner = new UsbPkcs11HsmSigner( softhsmLibraryPath, hsmPin, hsmTokenLabel, hsmKeyLabel ); // Create the Signature Image string signatureImagePath = "IronSoftware.png"; PdfSignatureImage sigImage = new PdfSignatureImage(signatureImagePath, 0, new Rectangle(50, 50, 150, 150)); // Sign PDF with HSM pdf.SignAndSave("signedWithHSM.pdf", hsmSigner); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel The UsbPkcs11HsmSigner additionally takes two optional parameters, the digestAlgorithm and signingAlgorithm. By default, they are set to SHA256 and RSA. Output Below is the output generated. As you can see, it displays the signature field and confirms that it is signed with the certificate we generated. Troubleshooting If you encounter the error shown below while running the code example, follow these troubleshooting steps to debug and verify your configuration. This CKR_GENERAL_ERROR commonly occurs when the program cannot find the SoftHSM configuration file or when the .NET application is running as a 32-bit process while the SoftHSM library is 64-bit. Changing the Platform Target A common cause for this error is an architecture mismatch. Your C# application must run as a 64-bit process to match the 64-bit SoftHSM library (softhsm2-x64.dll). In your Visual Studio project properties, change the Platform target from 'Any CPU' or 'x86' to 'x64' to ensure compatibility. Setting the Environment Variable Another common cause of error is that the program cannot find the .conf file in SoftHSM. You must tell the library where to look by setting a system-wide environment variable. Create a new variable named SOFTHSM2_CONF and set its value to the full path of your configuration file (e.g., D:\SoftHSM2\etc\softhsm2.conf). Afterwards, remember to restart Visual Studio after making the changes. Additionally, you can verify whether the variable is found by adding this line. Console.WriteLine($"Verifying variable: {Environment.GetEnvironmentVariable("SOFTHSM2_CONF")}"); Console.WriteLine($"Verifying variable: {Environment.GetEnvironmentVariable("SOFTHSM2_CONF")}"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel If the console output returns blank, then the program can't find the environment variable. You must set it, restart Visual Studio or your computer, and try again. 常见问题解答 什么是硬件安全模块(HSM)?为什么它用于PDF签名? 硬件安全模块 (HSM) 是一种用于管理数字密钥并执行加密和解密功能的物理设备。它用于对 PDF 文件进行签名,通过确保签名过程安全且符合各种标准来增强安全性。 如何在 C# 中配置 HSM 以进行 PDF 签名? 要在 C# 中配置 HSM 以对 PDF 进行签名,您需要使用适当的驱动程序和软件设置 HSM,确保它已连接到您的系统,并使用 IronPDF 在您的 C# 应用程序中配置签名过程。 使用 IronPDF 对 PDF 文件进行数字签名的步骤是什么? 要使用 IronPDF 对 PDF 进行数字签名,您需要先初始化 HSM,在 C# 代码中配置数字签名,然后使用 IronPDF 的签名功能将签名应用到 PDF 文档。 我可以使用 IronPDF 对其他类型的数字证书进行 PDF 签名吗? 是的,IronPDF 支持使用各种类型的数字证书对 PDF 文件进行签名,而不仅仅是 HSM 管理的证书。这包括存储在软件密钥库中的证书。 使用 IronPDF 进行 PDF 签名有哪些好处? IronPDF 提供了一个简单易用的 PDF 签名 API,让您可以轻松地将数字签名集成到 C# 应用程序中。它支持多种证书类型,包括由 HSM 管理的证书,从而确保了灵活性和安全性。 是否可以验证使用 IronPDF 创建的 PDF 签名? 是的,一旦使用 IronPDF 对 PDF 进行签名,就可以使用支持数字签名的标准 PDF 阅读器验证签名,从而确保文档的完整性和真实性。 我需要有编程经验才能使用 IronPDF 进行 PDF 签名吗? 建议具备 C# 编程基础知识才能使用 IronPDF 进行 PDF 签名,因为它涉及编写代码来管理签名过程并适当配置 HSM。 为将 HSM 与 IronPDF 集成,可提供哪些支持? Iron Software 提供文档和技术支持,帮助您将 HSM 与 IronPDF 集成,确保数字签名功能的顺利设置和运行。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 16,133,208 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:16,133,208 查看许可证