Created
June 23, 2010 20:27
-
-
Save zenchild/450498 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
require 'openssl' | |
class Encryptor | |
def initialize(key_pass, key_size = 1024, key_name = 'rsakey.sec', cipher = OpenSSL::Cipher.new('aes-256-cbc')) | |
@key_size = key_size | |
@cipher = cipher | |
@key_name = key_name | |
if( File.exists?(@key_name) ) | |
@rsakey = OpenSSL::PKey::RSA.new(File.read(@key_name), key_pass) | |
else | |
@rsakey = OpenSSL::PKey::RSA.generate(@key_size) | |
File.open(@key_name,'w+') do |priv| | |
priv.write(@rsakey.to_pem(@cipher, key_pass)) | |
end | |
end | |
end | |
def encrypt_string(txt) | |
begin | |
@rsakey.public_encrypt(txt) | |
rescue OpenSSL::PKey::RSAError => e | |
if e.message == 'data too large for key size' | |
STDERR.puts 'Your string is too large to encrypt' | |
else | |
raise | |
end | |
end | |
end | |
def decrypt_string(etxt) | |
@rsakey.private_decrypt(etxt) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment