class creates interface to encrypt dictionary for use in Decrypt
7.6.3.3 Encryption Key Algorithm (pp61)
needs a document’s user password to build a key for decrypting an encrypted PDF document
# File lib/pdf/reader/standard_security_handler.rb, line 48 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
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
# File lib/pdf/reader/standard_security_handler.rb, line 78 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