如何将时间戳添加到文档(ITextSharp,BouncyCastle)
How to add TimesStamp to document (ITextSharp, BouncyCastle)
您好,我想使用 ItextSharp 将授权服务器的时间戳添加到 Pdf,但我不知道如何操作。我只能在带有数字签名的 PDF 中添加时间戳,但我想在没有数字签名的情况下添加。
当我从服务器获取 TimeStamp 时,它里面有证书,但它没有要签名的主密钥,是否有其他方法可以在没有证书的情况下进行签名,或者我需要它,如果是,如何获得用它签署 PDF 的主键。
通过 Bouncy Castle 库获取时间戳令牌
private static TimeStampToken Timestamp(string InputPath, string annexFileName, stringsignedFileName,string UserTs, string PassTs, string UrlTs)
{
FileStream stream = File.OpenRead(InputPath);
SHA1Managed sha = new SHA1Managed();
byte[] hashedFile = sha.ComputeHash(stream);
TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
reqGen.SetCertReq(true);
TimeStampRequest request = reqGen.Generate(
TspAlgorithms.Sha1, hashedFile, BigInteger.ValueOf(100));
stream.Close();
byte[] reqData = request.GetEncoded();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 |
SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(UrlTs);
httpReq.Method = "POST";
httpReq.ContentType = "application/timestamp-query";
httpReq.ContentLength = reqData.Length;
httpReq.Credentials = new NetworkCredential(UserTs, PassTs);
// Write the request content
Stream reqStream = httpReq.GetRequestStream();
reqStream.Write(reqData, 0, reqData.Length);
reqStream.Close();
HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
// Read the response
Stream respStream = new BufferedStream(httpResp.GetResponseStream());
TimeStampResponse response = new TimeStampResponse(respStream);
respStream.Close();
response.Validate(request);
TimeStampToken tsToken = response.TimeStampToken;
Org.BouncyCastle.X509.Store.IX509Store x509Certs = tsToken.GetCertificates("Collection");
ArrayList certs = new ArrayList(x509Certs.GetMatches(null));
tsToken.Validate((Org.BouncyCastle.X509.X509Certificate)certs[0]);
return tsToken;
}
谢谢大家的回答。
我已经尝试了很多方法来弄清楚,但没有成功。
现在我正在使用 Syncfusion FileFormats Library,它工作得很好......遗憾的是它不是免费的,但它可以提供很多关于文件的功能和它的安全性......
//Creates a new PDF document
PdfDocument document = new PdfDocument();
//Adds a new page
PdfPage page = document.Pages.Add();
//Creates a digital signature
PdfSignature signature = new PdfSignature(page, "Signature");
//Add the time stamp by using the server URI
signature.TimeStampServer = new TimeStampServer(new Uri("http://syncfusion.digistamp.com"), "user", "123456");
//Save and close the document
document.Save("Output.pdf");
document.Close(true);
这是如何通过 Syncfusion 将时间戳添加到 Pdf 的示例。
您好,我想使用 ItextSharp 将授权服务器的时间戳添加到 Pdf,但我不知道如何操作。我只能在带有数字签名的 PDF 中添加时间戳,但我想在没有数字签名的情况下添加。
当我从服务器获取 TimeStamp 时,它里面有证书,但它没有要签名的主密钥,是否有其他方法可以在没有证书的情况下进行签名,或者我需要它,如果是,如何获得用它签署 PDF 的主键。
通过 Bouncy Castle 库获取时间戳令牌
private static TimeStampToken Timestamp(string InputPath, string annexFileName, stringsignedFileName,string UserTs, string PassTs, string UrlTs)
{
FileStream stream = File.OpenRead(InputPath);
SHA1Managed sha = new SHA1Managed();
byte[] hashedFile = sha.ComputeHash(stream);
TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
reqGen.SetCertReq(true);
TimeStampRequest request = reqGen.Generate(
TspAlgorithms.Sha1, hashedFile, BigInteger.ValueOf(100));
stream.Close();
byte[] reqData = request.GetEncoded();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 |
SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(UrlTs);
httpReq.Method = "POST";
httpReq.ContentType = "application/timestamp-query";
httpReq.ContentLength = reqData.Length;
httpReq.Credentials = new NetworkCredential(UserTs, PassTs);
// Write the request content
Stream reqStream = httpReq.GetRequestStream();
reqStream.Write(reqData, 0, reqData.Length);
reqStream.Close();
HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
// Read the response
Stream respStream = new BufferedStream(httpResp.GetResponseStream());
TimeStampResponse response = new TimeStampResponse(respStream);
respStream.Close();
response.Validate(request);
TimeStampToken tsToken = response.TimeStampToken;
Org.BouncyCastle.X509.Store.IX509Store x509Certs = tsToken.GetCertificates("Collection");
ArrayList certs = new ArrayList(x509Certs.GetMatches(null));
tsToken.Validate((Org.BouncyCastle.X509.X509Certificate)certs[0]);
return tsToken;
}
谢谢大家的回答。 我已经尝试了很多方法来弄清楚,但没有成功。 现在我正在使用 Syncfusion FileFormats Library,它工作得很好......遗憾的是它不是免费的,但它可以提供很多关于文件的功能和它的安全性......
//Creates a new PDF document
PdfDocument document = new PdfDocument();
//Adds a new page
PdfPage page = document.Pages.Add();
//Creates a digital signature
PdfSignature signature = new PdfSignature(page, "Signature");
//Add the time stamp by using the server URI
signature.TimeStampServer = new TimeStampServer(new Uri("http://syncfusion.digistamp.com"), "user", "123456");
//Save and close the document
document.Save("Output.pdf");
document.Close(true);
这是如何通过 Syncfusion 将时间戳添加到 Pdf 的示例。