今天我们一起来看看使用 FastReport VCL 对文件进行数字签名,详细教程如下。
我们很难想象没有电子文档管理的生活,电子文档的文件不易丢失,易于存储,并且可以方便转移。但众所周知,只有签署的文件才能生效。
procedure TForm1.Button1Click(Sender: TObject);const FR3FileName = 'Signatures.fr3';var PDFExport: TfrxPDFExport; Report: TfrxReport;begin Report := TfrxReport.Create(nil); try Report.LoadFromFile(FR3FileName); Report.PrepareReport; // upload and prepare a report PDFExport := TfrxPDFExport.Create(nil); try PDFExport.Report := Report; PDFExport.ShowDialog := False; PDFExport.FileName := ExtractFileName(FR3FileName) + '.pdf'; Report.Export(PDFExport); // export the report SignExport(PDFExport); // sign the file finally PDFExport.Free; end; finally Report.Free; end;end; procedure SignExport(PDFExport: TfrxPDFExport);const CertificatePath = 'JaneDoe.pfx'; // The name of our certificate PasCert = '123'; // certificate passwordvar Lookup: TCertificateStoreLookup; FS: TfrxFileSignature; FSO: Integer;begin Lookup := TCertificateStoreLookup.Create; Lookup.IgnoreCase := true; Lookup.CertificatePath := CertificatePath; FSO := FileSignatureOptions( true, // Detached = true Signature in a detached file false, // Chain = false Certificate chain false, // OnlyGOST= true GOST certificate true, // DebugLog = true Debugging Information true); // PFX = false (true) indicates that the certificate should be searched in the pfx/p12 file. At the same time, the file name and, possibly, the password must be specified. FS := TfrxFileSignature.Create( Lookup, PDFExport.FileName, // PDF file name PDFExport.FileName + '.sig', // Name of the generated signature AnsiString(PasCert), FSO); FS.Sign; FS.Free; Lookup.Free;end;
写完程序,让我们继续运行它。
启动后,点击“导出”按钮。然后我们得到一个带有签名文件的签名PDF文件:

我们应该检查 PDF 文件是否正确签名。为此,打开控制台并输入以下命令:
openssl pkcs12 -in JohnDoe.pfx -out JohnDoe.pem
我们输入密码并使用以下命令检查后:
openssl smime -verify -binary -inform DER -in Signatures.fr3.pdf.sig -content Signatures.fr3.pdf -certfile JohnDoe.pem -nointern -noverify 1 > / dev / null

此屏幕截图显示 PDF 文件已通过签名检查,所以我们做到了。
因此,我们用FastReport VCL快速地得到了一个正确导出的PDF文件的签名。
FastReport技术
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!