Method: GPGME::Crypto#decrypt

Defined in:
lib/gpgme/crypto.rb

#decrypt(cipher, options = {}) ⇒ GPGME::Data

Decrypts a previously encrypted element

crypto.decrypt cipher, options, &block

Must have the appropiate key to be able to decrypt, of course. Returns a Data object which can then be read.

Examples:

Simple decrypt

crypto.decrypt encrypted_data

symmetric encryption, or passwored key

crypto.decrypt encrypted_data, :password => "gpgme"

Output to file

file = File.open("decrypted.txt", "w+")
crypto.decrypt encrypted_data, :output => file

Verifying signatures

crypto.decrypt encrypted_data do |signature|
  raise "Signature could not be verified" unless signature.valid?
end

Parameters:

  • cipher

    Must be something that can be converted into a Data object, or a Data object itself. It is the element that will be decrypted.

  • options (Hash) (defaults to: {})

    The optional parameters:

    • :output if specified, it will write the output into it. It will me converted to a Data object, so it can also be a file, for example.
    • If the file was encrypted with symmetric encryption, must provide a :password option.
    • Any other option accepted by GPGME::Ctx.new
  • &block

    In the block all the signatures are yielded, so one could verify them. See examples.

Returns:

Raises:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/gpgme/crypto.rb', line 164

def decrypt(cipher, options = {})
  options = @default_options.merge options

  plain_data   = Data.new(options[:output])
  cipher_data  = Data.new(cipher)

  GPGME::Ctx.new(options) do |ctx|
    begin
      ctx.decrypt_verify(cipher_data, plain_data)
    rescue GPGME::Error::UnsupportedAlgorithm => exc
      exc.algorithm = ctx.decrypt_result.unsupported_algorithm
      raise exc
    rescue GPGME::Error::WrongKeyUsage => exc
      exc.key_usage = ctx.decrypt_result.wrong_key_usage
      raise exc
    end

    verify_result = ctx.verify_result
    if verify_result && block_given?
      verify_result.signatures.each do |signature|
        yield signature
      end
    end

  end

  plain_data.seek(0)
  plain_data
end