class CryptoToolchain::BlackBoxes::EcbOrCbcEncryptor

Constants

ENCRYPTION_ALGORITHMS

Attributes

key[R]
plaintext[R]

Public Class Methods

new(plaintext, algorithm: random_algorithm) click to toggle source
# File lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb, line 7
def initialize(plaintext, algorithm: random_algorithm)
  raise ArgumentError.new("Unsupported algorithm #{algorithm}") unless ENCRYPTION_ALGORITHMS.include? algorithm
  @plaintext = plaintext
  @key = String.random_bytes(16)
  @algorithm = algorithm
end

Public Instance Methods

encrypt(_algo = algorithm) click to toggle source
# File lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb, line 14
def encrypt(_algo = algorithm)
  case _algo
  when :ecb
    encrypt_ecb
  when :cbc
    encrypt_cbc
  end
end

Private Instance Methods

encrypt_cbc() click to toggle source
# File lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb, line 35
def encrypt_cbc
  obfuscate(plaintext).encrypt_cbc(key: key,
                        iv: String.random_bytes(16),
                        blocksize: 16)
end
encrypt_ecb() click to toggle source
# File lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb, line 31
def encrypt_ecb
  obfuscate(plaintext).encrypt_ecb(key: key, blocksize: 16)
end
obfuscate(text) click to toggle source
# File lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb, line 25
def obfuscate(text)
  append_len = (rand(5..10))
  prepend_len = (rand(5..10))
  "#{String.random_bytes(append_len)}#{text}#{String.random_bytes(prepend_len)}"
end
random_algorithm() click to toggle source
# File lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb, line 41
def random_algorithm
  ENCRYPTION_ALGORITHMS[rand(2)]
end