当前位置: 首页 > news >正文

【实用技能】如何使用 .NET C# 中的 Azure Key Vault 中的 PFX 证书对 PDF 文档进行签名

TX Text Control 是一款功能类似于 MS Word 的文字处理控件,包括文档创建、编辑、打印、邮件合并、格式转换、拆分合并、导入导出、批量生成等功能。广泛应用于企业文档管理,网站内容发布,电子病历中病案模板创建、病历书写、修改历史、连续打印、病案归档等功能的实现。

Azure Key Vault 是 Microsoft Azure 提供的一项云服务,用于安全地存储和访问敏感信息(例如 API 密钥、密码、证书和加密密钥)。它充当管理机密和加密操作的中央保管库,为静态和传输中的数据提供强大的保护。

Azure Key Vault 为使用 PFX 证书对 PDF 文档进行签名提供了几个关键优势。最重要的是,它通过将敏感证书存储在集中、高度安全的环境中来提高安全性,从而消除了在本地设备或服务器上暴露的风险。私钥永远不会离开保管库,因为加密操作是在保管库内执行的。

这种方法简化了证书管理,并允许组织集中和简化密钥轮换、撤销和审核。 Azure Key Vault 还提供可扩展性,以无缝满足不断增长的应用程序的需求,同时保持高可用性和强大的性能。合规性是另一个主要优势,因为该服务有助于满足严格的行业标准和法规,例如 GDPR、HIPAA 和 PCI DSS,从而确保数据保护和隐私。

这些优势结合起来,提供了一种安全、可靠且经济高效的方法来管理 PDF 签名和其他加密操作的证书。

Azure Key Vault 中的 PFX 证书

本文介绍如何使用存储在 Azure Key Vault 中的 PFX 证书对 PDF 文档进行签名。以下屏幕截图显示了已上传 PFX 证书的 Azure Key Vault 门户:

Azure Key Vault 门户

Azure SDK

Azure 需要以下 NuGet 包:

  • Azure.身份
  • Azure.Security.KeyVault.证书
  • TXTextControl.TextControl.ASP.SDK(试用下载)

Azure Key Vault 门户

将 PFX 证书上传到 Azure Key Vault

以下代码展示了将本地 PFX 文件上传到 Azure Key Vault 的一种方法,尽管本文不应重点介绍如何上传证书。如果您拥有适当的用户访问权限,也可以将证书上传到门户。

using System;
using System.Security.Cryptography.X509Certificates;
using Azure.Identity;
using Azure.Security.KeyVault.Certificates;
string keyVaultUrl = "https://txtestvault.vault.azure.net/";
string certificateName = "txselfcert";
string password = "123";
// Create a CertificateClient using DefaultAzureCredential for authentication
var credential = new DefaultAzureCredential(true);
var client = new CertificateClient(new Uri(keyVaultUrl), credential);
// Read the PFX file into a byte array
byte[] pfxBytes = await File.ReadAllBytesAsync("certificate.pfx");
// Import the certificate into Key Vault
var importOptions = new ImportCertificateOptions(certificateName, pfxBytes)
{
Password = password
};
var importedCertificate = await client.ImportCertificateAsync(importOptions);
Console.WriteLine($"Certificate '{certificateName}' imported successfully.");
签署 PDF 文档

将 PFX 证书上传到 Azure Key Vault 后,可以使用 Azure SDK 访问该证书并签署 PDF 文档。以下代码片段演示了如何使用存储在 Azure Key Vault 中的 PFX 证书签署 PDF 文档:

using System;
using System.Security.Cryptography.X509Certificates;
using Azure.Identity;
using Azure.Security.KeyVault.Certificates;
using TXTextControl;
string keyVaultUrl = "https://txtestvault.vault.azure.net/";
string certificateName = "txselfcert";
// Create a CertificateClient using DefaultAzureCredential for authentication
var credential = new DefaultAzureCredential(true);
var client = new CertificateClient(new Uri(keyVaultUrl), credential);
// Retrieve the certificate from the Azure Key Vault
var certificateWithPolicy = client.GetCertificate(certificateName);
byte[] pfxBytes = certificateWithPolicy.Value.Cer; // Extract the certificate's byte array
// Load the certificate into an X509Certificate2 object for digital signature purposes
var cert = new X509Certificate2(pfxBytes, (string)null, X509KeyStorageFlags.PersistKeySet);
// Initialize TXTextControl to create and save a document with the digital signature
using (var tx = new ServerTextControl())
{
tx.Create();
tx.Text = "Hello, World!";
// Prepare the digital signature for the document
var saveSettings = new SaveSettings
{
DigitalSignature = new DigitalSignature(cert, null)
};
// Save the document as a PDF with the digital signature
tx.Save("result.pdf", StreamType.AdobePDF, saveSettings);
}
Console.WriteLine("PDF saved with digital signature.");

当您在 Acrobat Reader 中打开创建的 PDF 文档时,您可以在签名面板中看到有效的证书。

已签名的 PDF 文档

结论

使用 Azure Key Vault 存储用于 PDF 签名的 PFX 证书,为管理敏感加密操作提供了一种安全、可扩展且合规的解决方案。通过使用 Azure Key Vault,组织可以提高安全性、简化证书管理并确保符合行业标准和法规。

http://www.lryc.cn/news/517465.html

相关文章:

  • 前端基础函数算法整理应用(sort+reduce+date+双重for循环)
  • 鸿蒙MPChart图表自定义(六)在图表中绘制游标
  • poi-tl+kkviewfile实现生成pdf业务报告
  • 【Uniapp-Vue3】scroll-view可滚动视图区域组件
  • asp.net core webapi中的数据注解与数据验证
  • PixPin—— 高效截图工具的下载与使用攻略
  • Go语言的 的多态性(Polymorphism)基础知识
  • Vue框架主要用来做什么?Vue框架的好处和特性.
  • 科普CMOS传感器的工作原理及特点
  • tensorflow 内存错误
  • spring boot解决swagger中的v2/api-docs泄露漏洞
  • 计算机网络 (25)IPV6
  • 小程序组件 —— 30 组件 - 背景图片的使用
  • 《Opencv》信用卡信息识别项目
  • Matlab贝叶斯估计MCMC分析药物对不同种群生物生理指标数据评估可视化
  • java 转义 反斜杠 Unexpected internal error near index 1
  • 网络安全常见的问题
  • 在ubuntu22.04中使用bear命令追踪内核编译报错的原因分析和解决方案
  • 【软考网工笔记】操作系统管理与配置——Windows
  • vue3 css实现文字输出带光标显示,文字输出完毕,光标消失的效果
  • 什么情况会导致JVM退出?
  • CentOS7修改Docker默认存储路径
  • OpenCV相机标定与3D重建(46)将三维空间中的点投影到二维图像平面上函数projectPoints()的使用
  • 基于Elasticsearch8的向量检索实现相似图形搜索
  • springboot+vue使用easyExcel实现导出功能
  • ffmpeg-avio实战:打开本地文件或者网络直播流dome
  • css预处理器sass
  • VulnHub-Acid(1/100)
  • MATLAB语言的正则表达式
  • 通过 route 或 ip route 管理Linux主机路由