首页 > 解决方案 > 登录iTextSharp时如何自动输入密码

问题描述

我必须使用存储在安全令牌中的证书。我可以从 Windows 证书存储区访问它,但设备有密码,因此会显示一个带有输入字段的弹出窗口,我使用CspParameters作为自动输入密码。

但是,当我签署数千个文件时,弹出的输入密码再次出现。这是我用来加载证书的代码:

public X509Certificate2 loadX509byPass()
        {
            var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.MaxAllowed);
            var certificates = store.Certificates.Find(X509FindType.FindBySerialNumber, SerialNumber, false);
            var cert = certificates[0];
            store.Close();

            var pass = new SecureString();           
            char[] array = Password.ToCharArray();
            foreach (char ch in array)
            {
                pass.AppendChar(ch);
            }
             var privateKey = cert.PrivateKey as RSACryptoServiceProvider;
             CspParameters cspParameters = new CspParameters(privateKey.CspKeyContainerInfo.ProviderType,
             privateKey.CspKeyContainerInfo.ProviderName,
             privateKey.CspKeyContainerInfo.KeyContainerName,
               new System.Security.AccessControl.CryptoKeySecurity(),
             pass);  

            var rsaCsp = new RSACryptoServiceProvider(cspParameters);              
            // set modified RSA crypto provider back
            cert.PrivateKey = rsaCsp;
            return cert;
        }

当签署文件时:

                         Cert = SingletonCA.Instance.loadX509byPass();
                         ....
                         BcX509.X509Certificate bcCert = DotNetUtils.FromX509Certificate(Cert);
                        var chain = new List<BcX509.X509Certificate> { bcCert };
                        var FontColour = new BaseColor(0, 0, 255); 
                        IExternalSignature pks = new X509Certificate2Signature(Cert, "SHA1");                        

                        MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, CryptoStandard.CMS);  

是否可以在代码中发送密码以不显示此消息?

谢谢你的帮助。

标签: .netitextdigital-signaturesmartcardx509certificate2

解决方案


推荐阅读