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.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/sample/openssl/cipher.rb b/sample/openssl/cipher.rb
new file mode 100644
index 0000000..844b6ee
--- /dev/null
+++ b/sample/openssl/cipher.rb
@@ -0,0 +1,29 @@
+#!/usr/bin/env ruby
+require 'openssl'
+
+text = "abcdefghijklmnopqrstuvwxyz"
+key = "key"
+alg = "DES-EDE3-CBC"
+#alg = "AES-128-CBC"
+
+puts "--Setup--"
+puts %(clear text: "#{text}")
+puts %(symmetric key: "#{key}")
+puts %(cipher alg: "#{alg}")
+puts
+
+puts "--Encrypting--"
+des = OpenSSL::Cipher::Cipher.new(alg)
+des.encrypt(key) #, "iv12345678")
+cipher = des.update(text)
+cipher << des.final
+puts %(encrypted text: #{cipher.inspect})
+puts
+
+puts "--Decrypting--"
+des = OpenSSL::Cipher::Cipher.new(alg)
+des.decrypt(key) #, "iv12345678")
+out = des.update(cipher)
+out << des.final
+puts %(decrypted text: "#{out}")
+puts