class OoxmlDecrypt::KeyData

Constants

ENCRYPTED_DATA_INTEGRITY_HMAC_VALUE_BLOCK_KEY
ENCRYPTED_DATA_INTEGRITY_SALT_BLOCK_KEY

Integrity-verification constants (not currently used)

Public Class Methods

from_xml(xml_doc) click to toggle source

Extracts key data parameters from the given XML document and populates a new KeyData object. @param [Nokogiri::XML::Document] xml_doc The EncryptionInfo section of

the encrypted document
# 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

Public Instance Methods

decrypt_encrypted_package_stream(encrypted_package, key) click to toggle source

Decrypts the given encrypted package using the given key. @param [String] encrypted_package The EncryptedPackage section of the

encrypted document

@param [String] key Decryption key

# 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