4 def crypt_by_password(alg, pass, salt, text)
6 puts %(cipher alg: "#{alg}")
7 puts %(plain text: "#{text}")
8 puts %(password: "#{pass}")
9 puts %(salt: "#{salt}")
13 enc = OpenSSL::Cipher::Cipher.new(alg)
15 enc.pkcs5_keyivgen(pass, salt)
16 cipher = enc.update(text)
18 puts %(encrypted text: #{cipher.inspect})
22 dec = OpenSSL::Cipher::Cipher.new(alg)
24 dec.pkcs5_keyivgen(pass, salt)
25 plain = dec.update(cipher)
27 puts %(decrypted text: "#{plain}")
32 ciphers = OpenSSL::Cipher.ciphers.sort
34 if i.upcase != i && ciphers.include?(i.upcase)
41 puts "Supported ciphers in #{OpenSSL::OPENSSL_VERSION}:"
42 ciphers.each_with_index{|name, i|
44 puts if (i + 1) % 5 == 0
49 alg = ARGV.shift || ciphers.first
50 pass = "secret password"
51 salt = "8 octets" # or nil
52 text = "abcdefghijklmnopqrstuvwxyz"
54 crypt_by_password(alg, pass, salt, text)