Digital signature asking password while signing PDF file using c#

Digital Signature in PDF File using DSC Token.

while signing pdf document file alway open popup for asking DSC Password.If you want to hide popup window of password then write below code

 RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
                try
                {
                    if (certificate.HasPrivateKey)
                    {
                        rSACryptoServiceProvider = (certificate.PrivateKey as RSACryptoServiceProvider);
                        if (rSACryptoServiceProvider.CspKeyContainerInfo.HardwareDevice)
                        {
                            SecureString secureString = new SecureString();
                            string smartCardPin = DscPassword;
                            foreach (char c in smartCardPin)
                            {
                                secureString.AppendChar(c);
                            }
                            CspParameters parameters = new CspParameters(rSACryptoServiceProvider.CspKeyContainerInfo.ProviderType, rSACryptoServiceProvider.CspKeyContainerInfo.ProviderName, rSACryptoServiceProvider.CspKeyContainerInfo.KeyContainerName, new CryptoKeySecurity(), secureString);
                            rSACryptoServiceProvider = new RSACryptoServiceProvider(parameters);
                            // SignTestMessage(certificate);
                            useSmartCardPIN = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new CryptographicException("Bypassing PIN Exception: " + ex.Message);
                }
                finally
                {
                    rSACryptoServiceProvider.Clear();
                }

Method are Below

 public static void SignTestMessage(X509Certificate2 cert)
        {
            try
            {
                byte[] content = new byte[12]
                {
                166,
                100,
                234,
                184,
                137,
                4,
                194,
                172,
                72,
                67,
                65,
                14
                };
                System.Security.Cryptography.Pkcs.ContentInfo contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(content);
                SignedCms signedCms = new SignedCms(contentInfo);
                CmsSigner cmsSigner = new CmsSigner(cert);
                cmsSigner.IncludeOption = X509IncludeOption.EndCertOnly;
                signedCms.ComputeSignature(cmsSigner, silent: false);
                signedCms.Encode();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

Leave a Comment