将数字签名插入现有的pdf文件中

我需要使用rails应用程序服务器将数字签名插入到现有的pdf文件中。 (基本上,客户端上传pdf文件,服务器使用本地证书对其进行签名)

我一直在使用JSignpdf将数字签名插入到pdf文件中,并开始探索ruby的gem……

我在rubypdf网站http://soft.rubypdf.com/software/pdf-digital-signe上找到了另一个可移植文件,但在ruby中找不到任何gem甚至示例代码。

我也看过OpenSSL的数字签名validation ,但无法理解如何使用本地证书文件对现有文档进行实际签名。

我还在http://code.google.com/p/origami-pdf/上达到了一个高峰,但对于假设“简单”(至少在概念上)的任务来说,这似乎有点苛刻。

有什么想法/建议吗?

谢谢

经过一些研究,重复使用OpenSSL文档并探索Origami解决方案 ,我构建了下面的代码,并设法将本地生成的签名/证书插入到pdf文档中。 现在我只需要弄清楚如何使用外部生成的证书(检查下面的版本2,我解决了它)。 我已经打开了一个新的问题 ,你可以在这里找到一些关于我使用OpenSSL和DER编码证书的难度的细节。

为了开发版本2,我还花了一些时间想知道如何添加注释 – 因此签名在Adobe阅读器中变得可见 – 而无需在文档中添加新页面。 从origami文档中 ,我找到了get_page方法,它解决了我的最后一个问题。 我正在使用Adobe Reader X进行记录。

希望你发现这个有用,因为我会;-)。

版本1 – 生成证书和密钥文件,并将它们直接插入到文档中

require 'openssl' begin require 'origami' rescue LoadError ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib" $: << ORIGAMIDIR require 'origami' end include Origami # Code below is based on documentation available on # http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL.html key = OpenSSL::PKey::RSA.new 2048 open 'private_key.pem', 'w' do |io| io.write key.to_pem end open 'public_key.pem', 'w' do |io| io.write key.public_key.to_pem end cipher = OpenSSL::Cipher::Cipher.new 'AES-128-CBC' pass_phrase = 'Origami rocks' key_secure = key.export cipher, pass_phrase open 'private_key.pem', 'w' do |io| io.write key_secure end #Create the certificate name = OpenSSL::X509::Name.parse 'CN=nobody/DC=example' cert = OpenSSL::X509::Certificate.new cert.version = 2 cert.serial = 0 cert.not_before = Time.now cert.not_after = Time.now + 3600 cert.public_key = key.public_key cert.subject = name OUTPUTFILE = "test.pdf" contents = ContentStream.new.setFilter(:FlateDecode) contents.write OUTPUTFILE, :x => 350, :y => 750, :rendering => Text::Rendering::STROKE, :size => 30 pdf = PDF.read('Sample.pdf') # Open certificate files #sigannot = Annotation::Widget::Signature.new #sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0] #page.add_annot(sigannot) # Sign the PDF with the specified keys pdf.sign(cert, key, :method => 'adbe.pkcs7.sha1', #:annotation => sigannot, :location => "Portugal", :contact => "myemail@email.tt", :reason => "Proof of Concept" ) # Save the resulting file pdf.save(OUTPUTFILE) 

版本2 – 使用现有证书签署pdf文档

 require 'openssl' begin require 'origami' rescue LoadError ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib" $: << ORIGAMIDIR require 'origami' end include Origami INPUTFILE = "Sample.pdf" @inputfile = String.new(INPUTFILE) OUTPUTFILE = @inputfile.insert(INPUTFILE.rindex("."),"_signed") CERTFILE = "certificate.pem" RSAKEYFILE = "private_key.pem" passphrase = "your passphrase" key4pem=File.read RSAKEYFILE key = OpenSSL::PKey::RSA.new key4pem, passphrase cert = OpenSSL::X509::Certificate.new(File.read CERTFILE) pdf = PDF.read(INPUTFILE) page = pdf.get_page(1) # Add signature annotation (so it becomes visibles in pdf document) sigannot = Annotation::Widget::Signature.new sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0] page.add_annot(sigannot) # Sign the PDF with the specified keys pdf.sign(cert, key, :method => 'adbe.pkcs7.sha1', :annotation => sigannot, :location => "Portugal", :contact => "myemail@email.tt", :reason => "Proof of Concept" ) # Save the resulting file pdf.save(OUTPUTFILE) 

如果您正在开发一个付费项目,您可能需要考虑jPDFSecure ,这是一个商业Java库,专为开发人员设计,可以对PDF文档进行数字签名并更改PDF文档的安全设置。 使用jPDFSecure,您的应用程序或Java applet可以加密PDF文档,设置权限和密码,以及创建和应用数字签名。 jPDFSecure针对性能进行了优化,基于Qoppa专有的PDF技术构建,因此无需任何第三方软件或驱动程序。

jPDFSecure有一个简单的界面,可以从文件,网络驱动器,URL甚至输入流加载PDF文档,这些文件可以生成运行时或直接来自数据库。 更改安全设置后,jPDFSecure可以在Java EE应用程序服务器中运行时将文档保存到文件,java.io.OutputStream或javax.servlet.ServletOutputStream,以将文件直接输出到浏览器。

jPDFSecure独立于平台,可用于支持Java的任何环境,包括Windows,Mac OSX和Linux。