Skip to content

Instantly share code, notes, and snippets.

@dr-r3d
Created September 15, 2011 10:54
Show Gist options
  • Save dr-r3d/1218999 to your computer and use it in GitHub Desktop.
Save dr-r3d/1218999 to your computer and use it in GitHub Desktop.
#This module does all the encryption and decryption using OpenSSL ruby class
module EncryptionEngine
#encrypt method as the name suggest encrypts 'data' using 'algorithm' with 'key'
def encrypt( data, key, algorithm)
#Do not encrypt if data is nil
if !data.nil? && !data.empty?
cipher = OpenSSL::Cipher::Cipher.new algorithm
cipher.encrypt
cipher.key = key
ciphertext = cipher.update data
encryptext= Base64::encode64(ciphertext << cipher.final)
end
end
#decrypt method as the name suggest decrypts 'encrypted_data' using 'algorithm' with 'key'
def decrypt( encrypted_data, key, algorithm)
#If encrypted_data is nil, don not decrypt and return '0'
if !encrypted_data.nil? && !encrypted_data.empty?
puts encrypted_data
cipher = OpenSSL::Cipher::Cipher.new algorithm
cipher.decrypt
cipher.key = key
encrypted_data = Base64::decode64(encrypted_data)
plaintext = cipher.update encrypted_data
plaintext = (plaintext << cipher.final)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment