Created
April 18, 2014 05:06
-
-
Save kenn/11025659 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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