Class: OoxmlDecrypt::KeyData

Inherits:
KeyInfoBase show all
Defined in:
lib/ooxml_decrypt/key_data.rb

Constant Summary collapse

ENCRYPTED_DATA_INTEGRITY_SALT_BLOCK_KEY =

Integrity-verification constants (not currently used)

"5FB2AD010CB9E1F6".unhexify
ENCRYPTED_DATA_INTEGRITY_HMAC_VALUE_BLOCK_KEY =
"A0677F02B22C8433".unhexify

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KeyInfoBase

#initialize, opts_from_xml_node

Constructor Details

This class inherits a constructor from OoxmlDecrypt::KeyInfoBase

Class Method Details

.from_xml(xml_doc) ⇒ Object

Extracts key data parameters from the given XML document and populates a new KeyData object.

Parameters:

  • xml_doc (Nokogiri::XML::Document)

    The EncryptionInfo section of the encrypted document



13
14
15
16
17
18
# File 'lib/ooxml_decrypt/key_data.rb', line 13

def self.from_xml(xml_doc)
  kd_node = xml_doc.at_css("keyData")
  opts = KeyInfoBase.opts_from_xml_node(kd_node)

  return self.new(opts)
end

Instance Method Details

#decrypt_encrypted_package_stream(encrypted_package, key) ⇒ Object

Decrypts the given encrypted package using the given key.

Parameters:

  • encrypted_package (String)

    The EncryptedPackage section of the encrypted document

  • key (String)

    Decryption key



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ooxml_decrypt/key_data.rb', line 24

def decrypt_encrypted_package_stream(encrypted_package, key)
  # Get the length of the real data in the cleartext (which may be shorter
  # than the full decrypted ciphertext)
  final_length = encrypted_package[0,8].unpack("Q<").first
  # The rest of the encrypted package is the ciphertext
  ciphertext = encrypted_package[8..-1]

  chunk_size = 4096
  ciphertext_chunks = (0..(ciphertext.length-1)/chunk_size).map{|i| ciphertext[i*chunk_size, chunk_size]}

  plaintext = ""
  ciphertext_chunks.each_with_index do |ciphertext_chunk, index|
    iv = hash(@salt + [index].pack("V"))
    iv.pad_or_trim!(@block_size)

    plaintext += decrypt(ciphertext_chunk, key, iv)
  end

  return plaintext[0,final_length]
end