Skip to content

Instantly share code, notes, and snippets.

@kusor
Created June 11, 2010 10:58
Show Gist options
  • Save kusor/434356 to your computer and use it in GitHub Desktop.
Save kusor/434356 to your computer and use it in GitHub Desktop.
Ruby OpenSSL Cipher
require 'openssl'
require 'digest/sha1'
require 'yaml'
puts "Enter passphrase: "
passphrase = gets.chomp
cipher = OpenSSL::Cipher::Cipher.new("AES-256-CBC")
cipher.decrypt
cipher.key = Digest::SHA1.hexdigest(passphrase).unpack('a2'*32).map{|x| x.hex}.pack('c'*32)
config = YAML::load_file("config.yml")
cipher.iv = config[:iv]
output = ""
output << cipher.update(config[:encrypted])
output << cipher.final
puts "Decrypted text: #{output}"
exit
require 'openssl'
require 'digest/sha1'
require 'yaml'
puts "Enter passphrase: "
passphrase = gets.chomp
puts "Enter text to encrypt: "
encrypt = gets.chomp
puts "Encrypting '#{encrypt}' with AES-256-CBC using passphrase '#{passphrase}'"
cipher = OpenSSL::Cipher::Cipher.new("AES-256-CBC")
cipher.encrypt
# your pass is what is used to encrypt/decrypt
cipher.key = key = Digest::SHA1.hexdigest(passphrase).unpack('a2'*32).map{|x| x.hex}.pack('c'*32)
cipher.iv = iv = cipher.random_iv
encrypted = cipher.update(encrypt)
encrypted << cipher.final
puts "Saving encrypted result: '#{encrypted}' with iv: #{iv}"
yaml_obj = {
:iv => iv,
:encrypted => encrypted
}
File.open('config.yml', 'w') do |out|
YAML::dump(yaml_obj, out)
end
exit
@scottcc
Copy link

scottcc commented Jun 11, 2010

Nice. Now, this does mean that the log files will have the cleartext though :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment