-
-
Save pahagon/daddd347ad4e31042511 to your computer and use it in GitHub Desktop.
This file contains 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
#just make sure that you have the appropriate openssl gem required. | |
#inspiration: http://blog.agoragames.com/blog/2010/05/13/rubyreversibleencryption/ | |
class Cryptor | |
self.encryption_type = "aes-256-cbc" | |
def self.encrypt(plaintext, key) | |
cipher = OpenSSL::Cipher::Cipher.new(self.encryption_type) | |
cipher.encrypt | |
cipher.key = key | |
cipher.iv = iv = cipher.random_iv | |
encrypted = cipher.update(plaintext) + cipher.final | |
encrypted = iv + encrypted | |
encrypted = Base64.encode64(encrypted) | |
encrypted | |
end | |
def self.decrypt(encrypted, key) | |
cipher = OpenSSL::Cipher::Cipher.new(self.encryption_type) | |
cipher.decrypt | |
cipher.key = key | |
encrypted = Base64.decode64(encrypted) | |
cipher.iv = encrypted.slice!(0,cipher.iv_len) | |
decrypted = cipher.update(encrypted) + cipher.final | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment