Skip to content

Instantly share code, notes, and snippets.

@kenn
Created April 18, 2014 05:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenn/11025659 to your computer and use it in GitHub Desktop.
Save kenn/11025659 to your computer and use it in GitHub Desktop.
require 'openssl'
module Crypto
module_function
def encrypt(iv, data)
enc = OpenSSL::Cipher::AES256.new(:CBC)
enc.encrypt
enc.key = key_with_padding(iv)
(enc.update(data) + enc.final).unpack("H*").first
end
def decrypt(iv, data)
dec = OpenSSL::Cipher::AES256.new(:CBC)
dec.decrypt
dec.key = key_with_padding(iv)
(dec.update(Array.new([data]).pack("H*")) + dec.final)
end
def key_with_padding(iv)
padding = "\x00" * (32 - iv.size)
return (iv + padding).encode(Encoding::BINARY)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment