class OoxmlDecrypt::EncryptedKey

Constants

ENCRYPTED_KEY_VALUE_BLOCK_KEY

Static key used in decrypting the key-encryption key

ENCRYPTED_VERIFIER_HASH_INPUT_BLOCK_KEY

Integrity-verification constants (not currently used)

ENCRYPTED_VERIFIER_HASH_VALUE_BLOCK_KEY

Public Class Methods

from_xml(xml_doc) click to toggle source

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

the encrypted OOXML document
# File lib/ooxml_decrypt/encrypted_key.rb, line 21
def self.from_xml(xml_doc)
  ke_node = xml_doc.at_css("keyEncryptor")
  raise "Expected only one child for keyEncryptor" unless ke_node.children.count == 1

  ek_node = ke_node.child
  opts = KeyInfoBase.opts_from_xml_node(ek_node)
  opts[:spin_count] = ek_node["spinCount"].to_i
  opts[:encrypted_key] = ek_node["encryptedKeyValue"].base64_decode

  return self.new(opts)
end
new(opts) click to toggle source
Calls superclass method
# File lib/ooxml_decrypt/encrypted_key.rb, line 11
def initialize(opts)
  @spin_count = opts.delete(:spin_count)
  @encrypted_key = opts.delete(:encrypted_key)
  super(opts)
end

Public Instance Methods

key(password) click to toggle source

Decrypts the key-encryption key using the given password @param [String] password Password as a UTF-16-formatted binary string

(e.g. the password 'password' should be passed as "p\0a\0s\0s\0w\0r\0d\0")

@return [String] The key-encryption key

# File lib/ooxml_decrypt/encrypted_key.rb, line 48
def key(password)
  decrypt(@encrypted_key, key_encryption_key(password))
end

Private Instance Methods

key_encryption_key( password ) click to toggle source
# File lib/ooxml_decrypt/encrypted_key.rb, line 33
def key_encryption_key( password )
  temp = hash( @salt + password )
  @spin_count.times do |itr|
    temp = hash( [itr].pack("V") + temp )
  end

  temp = hash(temp + ENCRYPTED_KEY_VALUE_BLOCK_KEY)
  temp.pad_or_trim!( @key_bits/8 )
end