13

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

我一直在使用 JSignpdf 将数字签名插入到 pdf 文件中,并开始为 ruby​​ 寻找 gem ......

我在 ruby​​pdf 网站http://soft.rubypdf.com/software/pdf-digital-signe上找到了另一个可移植的文件来完成这项工作,但在 ruby​​ 中找不到任何 gem 甚至示例代码来执行此操作。

我还查看了使用 OpenSSL 进行数字签名验证,但无法理解如何使用本地证书文件实际签署已经存在的文档。

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

有什么想法/建议吗?

谢谢

4

2 回答 2

16

经过一些研究,反复阅读 OpenSSL 文档并探索Origami 解决方案,我构建了下面的代码,并设法将本地生成的签名/证书插入到 pdf 文档中。现在我只需要弄清楚如何将它与外部生成的证书一起使用(查看下面的版本 2,我在其中解决了它)。我打开了一个新问题,您可以在其中找到有关我在使用 OpenSSL 和DER 编码证书时遇到的困难的一些详细信息。

为了开发第 2 版,我还花了一些时间想知道如何添加注释——以便签名在 Adob​​e 阅读器中变得可见——而不向文档添加新页面。从折纸文档中,我找到了 get_page 方法,它解决了我最后一个问题。我正在使用 Adob​​e 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)
于 2012-08-29T11:05:26.847 回答
-2

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

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

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

于 2012-08-23T03:02:42.407 回答