简体   繁体   English

如何从 Usb Token(etoken pro 72 k(Java) ) 读取证书并附加到 pdf

[英]How to Read a certificate from Usb Token(etoken pro 72 k(Java) )and attach to pdf

I want to read the signature from Usb token safenet (alladin etoken pro 72 k(Java)) and attach to pdf.我想从 Usb 令牌安全网 (alladin etoken pro 72 k(Java)) 读取签名并附加到 pdf。 I dont know how to do this.我不知道该怎么做。 In previously they given an option to export .pfx file.以前,他们提供了导出.pfx文件的选项。 Now they are giving an option to export .cer file.现在他们提供了导出.cer文件的选项。 When i googled i get this code.当我用谷歌搜索时,我得到了这个代码。 When i run this code works it prompts the password of the token after enter the password i can able to verify the signature but i dont know how to attach the signature to the pdf.当我运行此代码时,它会在输入密码后提示令牌的密码,我可以验证签名,但我不知道如何将签名附加到 pdf。 please guide me whether i am in correct direction or not.请指导我是否在正确的方向。 I am using c# language我正在使用 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);


        }

As it seems, you need to sign the PDF with the key stored on USB token. 看起来,您需要使用存储在USB令牌中的密钥对PDF进行签名

First thing to figure out is what signing format to use. 首先要弄清楚使用哪种签名格式。 PDFs can be signed according to PDF specification (which includes digital signing), PAdES (extended PDF signing), or as a generic binary data using CMS/CAdES or even XMLDSig/XAdES. 可以根据PDF规范(包括数字签名),PAdES(扩展的PDF签名)对PDF进行签名,或者使用CMS / CAdES甚至XMLDSig / XAdES作为通用二进制数据进行签名。

Assuming you need to sign the PDF according to PDF specification, you most likely need to use some library such as our PDFBlackbox or iText (watch the license and pricing!). 假设您需要根据PDF规范对PDF进行签名,则很可能需要使用某些库,例如我们的PDFBlackboxiText (请注意许可证和价格!)。

Back to technical side -- .cer file that you mentioned contains only public part of the certificate, and the private key, used for signing, can not usually be extracted from the security device such as USB token. 回到技术方面-您提到的.cer文件仅包含证书的公共部分,并且通常不能从安全设备(如USB令牌)中提取用于签名的私钥。 The PDF signing library must support calling the USB token via some programming interface (our PDFBlackbox supports both CryptoAPI and PKCS#11) to have it sign the hash of the data. PDF签名库必须支持通过某些编程接口(我们的PDFBlackbox支持CryptoAPI和PKCS#11)调用USB令牌,以使它对数据的哈希签名。

If you want to sign PDF with embedded signature you would most likely need to use PDF processing library such as iTextSharp which will embed the signature into the structure of PDF document. 如果要使用嵌入式签名对PDF进行签名,则很可能需要使用PDF处理库(例如iTextSharp) ,该库会将签名嵌入到PDF文档的结构中。 Bruno Lowagie from iText Software has written white paper called " Digital Signatures for PDF documents " which is a great source of information about digital signatures in PDF documents. iText Software的Bruno Lowagie撰写了名为“ PDF文档的数字签名 ”的白皮书,该白皮书是有关PDF文档中数字签名的大量信息来源。

If you want to use your application also on platforms other than Windows then you should take a look at my Pkcs11Interop.PDF library that extends iTextSharp with the ability to digitally sign PDF document with the private key stored on almost any PKCS#11 compatible device (smartcard, HSM, etc.). 如果您想在Windows以外的平台上也使用您的应用程序,那么您应该看一下我的Pkcs11Interop.PDF库,该库扩展了iTextSharp的功能,可以使用几乎存储在任何PKCS#11兼容设备上的私钥对PDF文档进行数字签名(智能卡,HSM等)。

The great thing about iTextSharp and Pkcs11Interop.PDF libraries is they are available under the dual license model so if you are able to comply with the terms of AGPL license then you can use both libraries for free. 关于iTextSharp和Pkcs11Interop.PDF库的妙处在于,它们在双重许可模式下可用,因此,如果您能够遵守AGPL许可的条款,则可以免费使用这两个库。

Modern token middleware (drivers) comes with CSP (Crypto Service Provider) on the top of it. 现代令牌中间件(驱动程序)在其顶部附带有CSP(加密服务提供程序)。 The CSP detects and handles event when USB Token plugged in or removed and makes the Certificates in the token available in the Windows Certificate Store. 当插入或卸下USB令牌时,CSP会检测并处理事件,并使令牌中的证书在Windows证书存储区中可用。 You may use Windows X509 Security libraries in C# to access the certificate for signing, which access the Certificate Store which in turns uses CSP to get content signed using the Private Key in the Token. 您可以使用C#中的Windows X509安全性库来访问用于签名的证书,后者可以访问证书存储,而证书存储又使用CSP获取使用令牌中的私钥进行签名的内容。 Private keys never comes out of the token. 私钥永远不会从令牌中冒出来。

If you are looking to use it in Web Application, please refer to this SO Answer 如果要在Web应用程序中使用它,请参考此SO解答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM