3

Estou tendo dificuldade em obter o dia e hora para assinar digitalmente um documento em PDF para P7S.

Segui a documentação: https://msdn.microsoft.com/pt-br/library/ms148432(v=vs.110).aspx

Também há referências nesse site:

http://leonelfraga.com/neomatrixtech/posts/2009/12/assinatura-digital-com-c-net.html

Data e Hora Inválida

Alguém já passou por isso e pode me ajudar?

4
  • 1
    Veja: msdn.microsoft.com/pt-br/library/ms180956(v=vs.85).aspx . Só que em vez de assinar "This is the message to be signed.", você vai assinar os bytes do PDF. 7/06/2018 às 20:02
  • Na verdade esse link faz o mesmo que os links que eu citei, já consigo assinar o PDF como comentei, porém falta acrescentar o timestamp. 7/06/2018 às 20:08
  • O timestamp, você não acrescenta; o wrapper do .NET acrescenta por você. 7/06/2018 às 20:09
  • Me desculpe a ignorância, porém não entendi. 7/06/2018 às 20:13

1 Resposta 1

1

Lembre-se de adicionar referência ao 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

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .