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

3

I'm having a hard time getting the day and time to digitally sign a PDF document for P7S.

Follow the documentation: link

There are also references on this site:

link

Someone has already gone through this and can help me?

    
asked by anonymous 07.06.2018 / 21:23

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();
        }
    }
}
    
07.06.2018 / 22:24