Skip to main content
 首页 » 编程设计

c#之如何从 USB token (etoken pro 72 k(Java))读取证书并附加到 pdf

2024年02月13日33cyq1162

我想从 Usb token 安全网络 (alladin etoken pro 72 k(Java)) 读取签名并附加到 pdf。我不知道该怎么做。之前他们提供了导出 .pfx 文件的选项。现在他们提供了导出 .cer 文件的选项。当我用谷歌搜索时,我得到了这段代码。当我运行此代码时它会在输入密码后提示 token 的密码我可以验证签名但我不知道如何将签名附加到 pdf。请指导我是否方向正确。我使用的是c#语言

private void btnGenpdfdigitalSignature_Click(object sender, EventArgs e) 
        { 
            try 
            { 
 
               // Cert myCert = null; 
 
                // Sign text 
                byte[] signature = Sign("Test", "Name of the signature person"); 
 
                // Verify signature. Testcert.cer corresponds to "cn=my cert subject" 
                if (Verify("Test", signature,"jai.cer")) 
                { 
 
 
                } 
                else 
                { 
                    Console.WriteLine("ERROR: Signature not valid!"); 
                } 
            } 
            catch (Exception ex) 
            { 
                Console.WriteLine("EXCEPTION: " + ex.Message); 
            } 
           // Console.ReadKey(); 
        } 
 
        static byte[] Sign(string text, string certSubject) 
        { 
            // Access Personal (MY) certificate store of current user 
            X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
            my.Open(OpenFlags.ReadOnly); 
 
            // Find the certificate we'll use to sign             
            RSACryptoServiceProvider csp = null; 
            foreach (X509Certificate2 cert in my.Certificates) 
            { 
                if (cert.Subject.Contains(certSubject)) 
                { 
                    // We found it.  
                    // Get its associated CSP and private key 
                    csp = (RSACryptoServiceProvider)cert.PrivateKey; 
 
                } 
 
            } 
            if (csp == null) 
            { 
                throw new Exception("No valid cert was found"); 
            } 
 
            // Hash the data 
            SHA1Managed sha1 = new SHA1Managed(); 
            UnicodeEncoding encoding = new UnicodeEncoding(); 
            byte[] data = encoding.GetBytes(text); 
            byte[] hash = sha1.ComputeHash(data); 
 
            // Sign the hash 
            return csp.SignHash(hash, CryptoConfig.MapNameToOID("Test")); 
 
 
        } 
 
 
        public bool Verify(string text, byte[] signature, string certPath) 
        { 
            // Load the certificate we'll use to verify the signature from a file  
             cert = new X509Certificate2(certPath); 
            // Note:  
            // If we want to use the client cert in an ASP.NET app, we may use something like this instead: 
            // X509Certificate2 cert = new X509Certificate2(Request.ClientCertificate.Certificate); 
 
            // Get its associated CSP and public key 
            RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PublicKey.Key; 
 
            // Hash the data 
            SHA1Managed sha1 = new SHA1Managed(); 
            UnicodeEncoding encoding = new UnicodeEncoding(); 
            byte[] data = encoding.GetBytes(text); 
            byte[] hash = sha1.ComputeHash(data); 
 
            // Verify the signature with the hash 
            return csp.VerifyHash(hash, CryptoConfig.MapNameToOID("Test"), signature); 
 
 
        } 

请您参考如下方法:

看起来,您需要使用存储在 USB token 上的 key 签署 PDF。

首先要弄清楚要使用的签名格式。 PDF 可以根据 PDF 规范(包括数字签名)、PAdES(扩展 PDF 签名)或使用 CMS/CAdES 甚至 XMLDSig/XAdES 作为通用二进制数据进行签名。

假设您需要根据 PDF 规范对 PDF 进行签名,您很可能需要使用一些库,例如我们的 PDFBlackbox或 iText (注意许可证和定价!)。

回到技术方面——你提到的.cer文件只包含证书的公共(public)部分,用于签名的私钥通常不能从USB token 等安全设备中提取出来。 PDF 签名库必须支持通过某些编程接口(interface)调用 USB token (我们的 PDFBlackbox 支持 CryptoAPI 和 PKCS#11),以使其对数据的散列进行签名。