Created
June 11, 2010 10:58
-
-
Save kusor/434356 to your computer and use it in GitHub Desktop.
Ruby OpenSSL Cipher
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' | |
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 |
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' | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. Now, this does mean that the log files will have the cleartext though :)