aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ossl_cipher.rb
blob: e198c4655a23df1b3f0dbc20ebbc6757642672ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env ruby

require 'openssl'
include OpenSSL

text = "abcdefghijklmnopqrstuvwxyz"
key = "key"
alg = "DES-EDE3-CBC"
#alg = "AES-128-CBC"

puts "ClearText = \"#{text}\""
puts "SymmetricKey = \"#{key}\""
puts "CipherAlg = \"#{alg}\""

des = Cipher::Cipher.new(alg)
puts "--Encrypting with key--"
des.encrypt("key")#, "iv12345678")
cipher = des.update(text)
cipher += des.final
puts "EncryptedText = #{cipher.inspect}"
puts "--Decrypting with key--"
des.decrypt(key) #, "iv12345678")
out = des.update(cipher) + des.final
puts "DecryptedText = \"#{out}\""

puts "DONE."