Sha256: 32a17bcb087c964b58a108f59819309a0f2346214a249fd4dd86d29f8098b198

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

# coding: utf-8
# typed: strict
# frozen_string_literal: true

require 'digest/md5'

class PDF::Reader

  # Decrypts data using the AESV2 algorithim defined in the PDF spec. Requires
  # a decryption key, which is usually generated by PDF::Reader::StandardKeyBuilder
  #
  class AesV2SecurityHandler

    def initialize(key)
      @encrypt_key = key
    end

    ##7.6.2 General Encryption Algorithm
    #
    # Algorithm 1: Encryption of data using the AES-128-CBC algorithm
    #
    # version == 4 and CFM == AESV2
    #
    # buf - a string to decrypt
    # ref - a PDF::Reader::Reference for the object to decrypt
    #
    def decrypt( buf, ref )
      objKey = @encrypt_key.dup
      (0..2).each { |e| objKey << (ref.id >> e*8 & 0xFF ) }
      (0..1).each { |e| objKey << (ref.gen >> e*8 & 0xFF ) }
      objKey << 'sAlT'  # Algorithm 1, b)
      length = objKey.length < 16 ? objKey.length : 16
      cipher = OpenSSL::Cipher.new("AES-#{length << 3}-CBC")
      cipher.decrypt
      cipher.key = Digest::MD5.digest(objKey)[0,length]
      cipher.iv = buf[0..15]
      cipher.update(buf[16..-1]) + cipher.final
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pdf-reader-2.13.0 lib/pdf/reader/aes_v2_security_handler.rb
pdf-reader-2.12.0 lib/pdf/reader/aes_v2_security_handler.rb
pdf-reader-2.11.0 lib/pdf/reader/aes_v2_security_handler.rb
pdf-reader-2.10.0 lib/pdf/reader/aes_v2_security_handler.rb
pdf-reader-2.9.2 lib/pdf/reader/aes_v2_security_handler.rb
pdf-reader-2.9.1 lib/pdf/reader/aes_v2_security_handler.rb
pdf-reader-2.9.0 lib/pdf/reader/aes_v2_security_handler.rb