Skip to content

Instantly share code, notes, and snippets.

@guivinicius
Created January 29, 2015 16:04
Show Gist options
  • Select an option

  • Save guivinicius/6888adaf30924621c4ef to your computer and use it in GitHub Desktop.

Select an option

Save guivinicius/6888adaf30924621c4ef to your computer and use it in GitHub Desktop.

Revisions

  1. guivinicius created this gist Jan 29, 2015.
    27 changes: 27 additions & 0 deletions enc_dec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    class EncDec

    KEY = "FA 0E 3C 9B 4F A2 C7 AA 37 0F CA 9B 5A 91 64 08"
    IV = "AA AD 8B 4C 00 E1 3F 53 B8 E7 16 BC B5 F4 D1 B9"

    attr_reader :cipher, :message, :key, :iv

    def initialize(message, key = nil, iv = nil)
    @key, @iv = key, iv
    @cipher = OpenSSL::Cipher::AES128.new(:CBC)
    @message = message

    cipher.key = key || KEY.delete(' ')
    cipher.iv = iv || IV.delete(' ')
    end

    def decrypt
    cipher.decrypt
    cipher.update(message) + cipher.final
    end

    def encrypt
    cipher.encrypt
    cipher.update(message) + cipher.final
    end

    end