Skip to content

Instantly share code, notes, and snippets.

@toland
Created July 13, 2010 16:47
Show Gist options
  • Save toland/474152 to your computer and use it in GitHub Desktop.
Save toland/474152 to your computer and use it in GitHub Desktop.
Simple Ruby module for encrypting text with Blowfish
require 'openssl'
module Blowfish
def self.cipher(mode, key, data)
cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').send(mode)
cipher.key = Digest::SHA256.digest(key)
cipher.update(data) << cipher.final
end
def self.encrypt(key, data)
cipher(:encrypt, key, data).unpack('H*').first
end
def self.decrypt(key, text)
cipher(:decrypt, key, [text].pack('H*'))
end
end
if $0 == __FILE__
p "text" == Blowfish.decrypt("key", Blowfish.encrypt("key", "text"))
end
@stephan-buckmaster
Copy link

Not making use of the initial vector which is part of CBC. IV is stored outside of the data, in plaintext. For your simple demo, add cipher.iv = 'elsewher' after line 6, or new param iv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment