Invalid Date and Time on signature using A3 digital certificate in C#

Asked

Viewed 276 times

3

  • 1

    See: https://msdn.microsoft.com/pt-br/library/ms180956(v=vs.85). aspx . Only instead of signing "This is the message to be signed.", you will sign the PDF bytes.

  • In fact this link does the same as the links that I mentioned, I can already sign the PDF as I said, but still need to add the timestamp.

  • The timestamp, you do not add; the . NET wrapper adds by you.

  • I’m sorry I didn’t understand.

1 answer

1

Remember to add reference to System.Security:

using System.IO;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApplication
{
    class AssinaDigitalmente
    {
        public static void Main(string[] args)
        {
            string certificadoPath = args[0];
            string pdfPath = args[1];

            byte[] pdfBytes = File.ReadAllBytes(pdfPath);

            byte[] pdfAssinado = Assina(certificadoPath, pdfBytes);

            File.WriteAllBytes(Path.Combine(Path.GetDirectoryName(pdfPath), Path.GetFileNameWithoutExtension(pdfPath) + ".p7s"), pdfAssinado);
        }

        public static byte[] Assina(string certificadoPath, byte[] meusBytes)
        {
            ContentInfo info = new ContentInfo(meusBytes);
            SignedCms signedCms = new SignedCms(info);
            CmsSigner cmsSigner = new CmsSigner(X509Certificate2.CreateFromCertFile(certificadoPath) as X509Certificate2);

            signedCms.ComputeSignature(cmsSigner);
            return signedCms.Encode();
        }
    }
}
  • 1

    I resolved the date issue by following the link : https://msdn.microsoft.com/ru-RU/library/windows/desktop/ms180964(v=vs.85). aspx used: CmsSigner signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, certificate);

signer.SignedAttributes.Add(new Pkcs9SigningTime());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.