Class: PDF::Reader::StandardSecurityHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/standard_security_handler.rb

Overview

class creates interface to encrypt dictionary for use in Decrypt

Constant Summary collapse

PassPadBytes =

7.6.3.3 Encryption Key Algorithm (pp61)

needs a document’s user password to build a key for decrypting an encrypted PDF document

[ 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08,
0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enc, file_id, password) ⇒ StandardSecurityHandler

Returns a new instance of StandardSecurityHandler.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pdf/reader/standard_security_handler.rb', line 50

def initialize( enc, file_id, password )
  @filter        = enc[:Filter]
  @subFilter     = enc[:SubFilter]
  @version       = enc[:V].to_i
  @key_length    = enc[:Length].to_i/8
  @crypt_filter  = enc[:CF]
  @stream_filter = enc[:StmF]
  @string_filter = enc[:StrF]
  @revision      = enc[:R].to_i
  @owner_key     = enc[:O]
  @user_key      = enc[:U]
  @permissions   = enc[:P].to_i
  @embedded_file_filter = enc[:EFF]

  @encryptMeta   = enc.has_key?(:EncryptMetadata)? enc[:EncryptMetadata].to_s == "true" : true;

  @file_id       = file_id.first

  @encrypt_key   = build_standard_key(password)
end

Instance Attribute Details

#crypt_filterObject (readonly)

Returns the value of attribute crypt_filter.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def crypt_filter
  @crypt_filter
end

#embedded_file_filterObject (readonly)

Returns the value of attribute embedded_file_filter.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def embedded_file_filter
  @embedded_file_filter
end

#encrypt_keyObject (readonly)

Returns the value of attribute encrypt_key.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def encrypt_key
  @encrypt_key
end

#file_idObject (readonly)

Returns the value of attribute file_id.



48
49
50
# File 'lib/pdf/reader/standard_security_handler.rb', line 48

def file_id
  @file_id
end

#filterObject (readonly)

Returns the value of attribute filter.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def filter
  @filter
end

#key_lengthObject (readonly)

Returns the value of attribute key_length.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def key_length
  @key_length
end

#owner_keyObject (readonly)

Returns the value of attribute owner_key.



48
49
50
# File 'lib/pdf/reader/standard_security_handler.rb', line 48

def owner_key
  @owner_key
end

#passwordObject (readonly)

Returns the value of attribute password.



48
49
50
# File 'lib/pdf/reader/standard_security_handler.rb', line 48

def password
  @password
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



48
49
50
# File 'lib/pdf/reader/standard_security_handler.rb', line 48

def permissions
  @permissions
end

#revisionObject (readonly)

Returns the value of attribute revision.



48
49
50
# File 'lib/pdf/reader/standard_security_handler.rb', line 48

def revision
  @revision
end

#stream_filterObject (readonly)

Returns the value of attribute stream_filter.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def stream_filter
  @stream_filter
end

#string_filterObject (readonly)

Returns the value of attribute string_filter.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def string_filter
  @string_filter
end

#subFilterObject (readonly)

Returns the value of attribute subFilter.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def subFilter
  @subFilter
end

#user_keyObject (readonly)

Returns the value of attribute user_key.



48
49
50
# File 'lib/pdf/reader/standard_security_handler.rb', line 48

def user_key
  @user_key
end

#versionObject (readonly)

Returns the value of attribute version.



45
46
47
# File 'lib/pdf/reader/standard_security_handler.rb', line 45

def version
  @version
end

Instance Method Details

#decrypt(buf, ref) ⇒ Object

7.6.2 General Encryption Algorithm

Algorithm 1: Encryption of data using the RC4 or AES algorithms

used to decrypt RC4 encrypted PDF streams (buf)

buf - a string to decrypt ref - a PDF::Reader::Reference for the object to decrypt



80
81
82
83
84
85
86
87
# File 'lib/pdf/reader/standard_security_handler.rb', line 80

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 ) }
  length = objKey.length < 16 ? objKey.length : 16
  rc4 = RC4.new( Digest::MD5.digest(objKey)[(0...length)] )
  rc4.decrypt(buf)
end