summaryrefslogtreecommitdiff
path: root/sample/openssl/cipher.rb
diff options
context:
space:
mode:
Diffstat (limited to 'sample/openssl/cipher.rb')
-rw-r--r--sample/openssl/cipher.rb12
1 files changed, 8 insertions, 4 deletions
diff --git a/sample/openssl/cipher.rb b/sample/openssl/cipher.rb
index 844b6ee..6e8cdb9 100644
--- a/sample/openssl/cipher.rb
+++ b/sample/openssl/cipher.rb
@@ -2,19 +2,22 @@
require 'openssl'
text = "abcdefghijklmnopqrstuvwxyz"
-key = "key"
+pass = "secret password"
+salt = "8 octets" # or nil
alg = "DES-EDE3-CBC"
#alg = "AES-128-CBC"
puts "--Setup--"
puts %(clear text: "#{text}")
-puts %(symmetric key: "#{key}")
+puts %(password: "#{pass}")
+puts %(salt: "#{salt}")
puts %(cipher alg: "#{alg}")
puts
puts "--Encrypting--"
des = OpenSSL::Cipher::Cipher.new(alg)
-des.encrypt(key) #, "iv12345678")
+des.pkcs5_keyivgen(pass, salt)
+des.encrypt
cipher = des.update(text)
cipher << des.final
puts %(encrypted text: #{cipher.inspect})
@@ -22,7 +25,8 @@ puts
puts "--Decrypting--"
des = OpenSSL::Cipher::Cipher.new(alg)
-des.decrypt(key) #, "iv12345678")
+des.pkcs5_keyivgen(pass, salt)
+des.decrypt
out = des.update(cipher)
out << des.final
puts %(decrypted text: "#{out}")