Project

General

Profile

Bug #12561 ยป cipher-truncate.rb

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

 
#! /usr/bun/env ruby

require 'openssl'

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

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

# Encrypt with K1:
encode = OpenSSL::Cipher.new(CIPHER)
encode.encrypt
encode.iv = IV
encode.key = K1
ciphertext = encode.update(plaintext0) + encode.final

# Decrypt with K1:
decode1 = OpenSSL::Cipher.new(CIPHER)
decode1.decrypt
decode1.iv = IV
decode1.key = K1
plaintext1 = decode1.update(ciphertext) + decode1.final

# Decrypt with K2:
decode2 = OpenSSL::Cipher.new(CIPHER)
decode2.decrypt
decode2.iv = IV
decode2.key = K2
plaintext2 = decode2.update(ciphertext) + decode2.final

puts "Plaintext: #{plaintext0}"
puts "Decoded K1: #{plaintext1}"
puts "Decoded K2: #{plaintext2}"
    (1-1/1)