Project

General

Profile

Bug #12561 ยป cipher-truncate.rb

Minimal working example. - xavierholt (Kevin Burk), 07/06/2016 08:24 PM

 
1
#! /usr/bun/env ruby
2

    
3
require 'openssl'
4

    
5
# Generated with OpenSSL::Cipher.new('AES-128-CBC').random_iv
6
IV = "\xC9\xA6\xFA]\xF5\x8F\x8B14.RK\xA6N\x16k"
7
K1 = "This is a key string!"
8
K2 = "This is a key string too, but it's different."
9

    
10
CIPHER     = 'AES-128-CBC'
11
plaintext0 = "Secret message!"
12

    
13
# Encrypt with K1:
14
encode = OpenSSL::Cipher.new(CIPHER)
15
encode.encrypt
16
encode.iv  = IV
17
encode.key = K1
18
ciphertext = encode.update(plaintext0) + encode.final
19

    
20
# Decrypt with K1:
21
decode1 = OpenSSL::Cipher.new(CIPHER)
22
decode1.decrypt
23
decode1.iv  = IV
24
decode1.key = K1
25
plaintext1  = decode1.update(ciphertext) + decode1.final
26

    
27
# Decrypt with K2:
28
decode2 = OpenSSL::Cipher.new(CIPHER)
29
decode2.decrypt
30
decode2.iv  = IV
31
decode2.key = K2
32
plaintext2  = decode2.update(ciphertext) + decode2.final
33

    
34
puts "Plaintext:  #{plaintext0}"
35
puts "Decoded K1: #{plaintext1}"
36
puts "Decoded K2: #{plaintext2}"