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();
}
}
}
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.– Marcelo Shiniti Uchimura
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.
– Eduardo F G
The timestamp, you do not add; the . NET wrapper adds by you.
– Marcelo Shiniti Uchimura
I’m sorry I didn’t understand.
– Eduardo F G