Created
October 5, 2011 23:41
-
-
Save reu/1266082 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
class String | |
def encrypt(options = {}) | |
crypt :encrypt, options | |
end | |
def decrypt(options = {}) | |
crypt :decrypt, options | |
end | |
private | |
def crypt(method, options) | |
raise "You must provide a key" unless options[:key] | |
options[:with] ||= "AES-128-CBC" | |
cipher = OpenSSL::Cipher.new(options[:with]) | |
cipher.send method | |
cipher.pkcs5_keyivgen(options[:key]) | |
cipher.update(self) + cipher.final | |
end | |
end | |
encrypted_thing = "EU VOU SER ENCRYPTADO YAY".encrypt :key => "LOL" | |
decrypted_thing = encrypted_thing.decrypt :key => "LOL" |
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" | |
encrypter = OpenSSL::Cipher.new("AES-128-CBC") | |
encrypter.encrypt | |
encrypter.pkcs5_keyivgen("LOL WUT WTF OMG") | |
encrypted_thing = encrypter.update("EU VOU SER ENCRYPTADO YAY") | |
encrypted_thing << encrypter.final | |
decrypter = OpenSSL::Cipher.new("AES-128-CBC") | |
decrypter.decrypt | |
decrypter.pkcs5_keyivgen("LOL WUT WTF OMG") | |
i_am_back = decrypter.update(encrypted_thing) | |
i_am_back << decrypter.final |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment