Class: OoxmlDecrypt::EncryptedFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxml_decrypt/encrypted_file.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ EncryptedFile

Returns a new instance of EncryptedFile.

Parameters:

  • filename (String)

    Path to the encrypted OOXML file



7
8
9
10
11
12
13
# File 'lib/ooxml_decrypt/encrypted_file.rb', line 7

def initialize(filename)
  @ole = Ole::Storage.open(filename)
  unless @ole.dir.entries(".").include?("EncryptionInfo") and
      @ole.dir.entries(".").include?("EncryptedPackage")
    raise "File does not appear to be an encrypted Office document"
  end
end

Class Method Details

.decrypt(filename, password) ⇒ Object

Decrypts the given file using the given password

Parameters:

  • filename (String)

    Path to the encrypted OOXML file

  • password (String)

    Password as a UTF-16-formatted binary string (e.g. the password ‘password’ should be passed as “p0a0s0s0w0r0d0”)



71
72
73
74
# File 'lib/ooxml_decrypt/encrypted_file.rb', line 71

def self.decrypt(filename, password)
  encrypted_file = EncryptedFile.new(filename)
  return encrypted_file.decrypt(password)
end

.decrypt_to_file(enc_filename, password, dec_filename) ⇒ Object

Decrypts the given file using the given password and writes the result to a second file

Parameters:

  • enc_filename (String)

    Path to the encrypted OOXML file

  • password (String)

    Password as a UTF-16-formatted binary string (e.g. the password ‘password’ should be passed as “p0a0s0s0w0r0d0”)

  • dec_filename (String)

    Path to the decrypted output file. If a file exists at this path, it will be overwritten.



83
84
85
86
# File 'lib/ooxml_decrypt/encrypted_file.rb', line 83

def self.decrypt_to_file(enc_filename, password, dec_filename)
  plaintext = decrypt(enc_filename, password)
  File.open(dec_filename, "wb") {|file| file.write(plaintext)}
end

Instance Method Details

#decrypt(password) ⇒ String

Decrypts this encrypted file using the given password

Parameters:

  • password (String)

    Password as a UTF-16-formatted binary string (e.g. the password ‘password’ should be passed as “p0a0s0s0w0r0d0”)

Returns:

  • (String)

    The decrypted file



62
63
64
65
# File 'lib/ooxml_decrypt/encrypted_file.rb', line 62

def decrypt(password)
  decryption_key = encrypted_key.key(password)
  return key_data.decrypt_encrypted_package_stream( encrypted_package, decryption_key )
end