Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save reu/1266082 to your computer and use it in GitHub Desktop.
Save reu/1266082 to your computer and use it in GitHub Desktop.
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"
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