From 78b97bdeeb62d741a7f36f16e520d80383fdbd98 Mon Sep 17 00:00:00 2001 From: thomashaw Date: Tue, 21 Mar 2017 11:40:38 +0000 Subject: [PATCH] Vignere Cipher - Takes strings_to_encode and encryption_key, outputs: KEY_CIPHERTEXT --- .../cipher/vignere/manifests/.no_puppet | 0 .../cipher/vignere/secgen_local/local.rb | 56 +++++++++++++++++++ .../cipher/vignere/secgen_metadata.xml | 37 ++++++++++++ modules/encoders/cipher/vignere/vignere.pp | 0 .../encoder_examples/vignere_cipher.xml | 20 +++++++ 5 files changed, 113 insertions(+) create mode 100644 modules/encoders/cipher/vignere/manifests/.no_puppet create mode 100644 modules/encoders/cipher/vignere/secgen_local/local.rb create mode 100644 modules/encoders/cipher/vignere/secgen_metadata.xml create mode 100644 modules/encoders/cipher/vignere/vignere.pp create mode 100644 scenarios/examples/parameterised_examples/encoder_examples/vignere_cipher.xml diff --git a/modules/encoders/cipher/vignere/manifests/.no_puppet b/modules/encoders/cipher/vignere/manifests/.no_puppet new file mode 100644 index 000000000..e69de29bb diff --git a/modules/encoders/cipher/vignere/secgen_local/local.rb b/modules/encoders/cipher/vignere/secgen_local/local.rb new file mode 100644 index 000000000..d39330108 --- /dev/null +++ b/modules/encoders/cipher/vignere/secgen_local/local.rb @@ -0,0 +1,56 @@ +#!/usr/bin/ruby +# Encryption algorithm code from http://rosettacode.org/wiki/Vigen%C3%A8re_cipher#Ruby +require_relative '../../../../../lib/objects/local_string_encoder.rb' +class VignereCipher < StringEncoder + attr_accessor :encryption_key + + def initialize + super + self.module_name = 'Vignere Cipher Encoder' + self.encryption_key = '' + end + + BASE = 'A'.ord + SIZE = 'Z'.ord - BASE + 1 + + def encrypt(text, key) + crypt(text, key, :+) + end + + def decrypt(text, key) + crypt(text, key, :-) + end + + def crypt(text, key, dir) + text = text.upcase.gsub(/[^A-Z]/, '') + key_iterator = key.upcase.gsub(/[^A-Z]/, '').chars.map{|c| c.ord - BASE}.cycle + text.each_char.inject('') do |ciphertext, char| + offset = key_iterator.next + ciphertext << ((char.ord - BASE).send(dir, offset) % SIZE + BASE).chr + end + end + + def encode(str) + self.encryption_key + '_' + encrypt(str, self.encryption_key) + end + + def process_options(opt, arg) + super + case opt + # Removes any non-alphabet characters + when '--encryption_key' + self.encryption_key << arg.upcase.gsub(/[^A-Z]/, ''); + end + end + + def get_options_array + super + [['--encryption_key', GetoptLong::REQUIRED_ARGUMENT]] + end + + def encoding_print_string + 'strings_to_encode: ' + self.strings_to_encode.to_s + ', + encryption_key: ' + self.encryption_key.to_s + end +end + +VignereCipher.new.run diff --git a/modules/encoders/cipher/vignere/secgen_metadata.xml b/modules/encoders/cipher/vignere/secgen_metadata.xml new file mode 100644 index 000000000..3435cecc8 --- /dev/null +++ b/modules/encoders/cipher/vignere/secgen_metadata.xml @@ -0,0 +1,37 @@ + + + + Vignere Cipher + Thomas Shaw + MIT + The Vignere cipher is a type of polyalphabetic cipher, a square table of alphabets shifted 1 space on + each row (aka a 'tabula recta'). + + The encryption key is repeated for the length of the string being encoded. To encrypt, select the column with the + letter to encode and pick the row which the corresponds with the current position in the encryption key and find + where they intercept. + + + vignere_cipher + alpha_reversable + cipher_encoder + linux + windows + + http://www.cs.mtu.edu/~shene/NSF-4/Tutorial/VIG/Vig-Base.html + http://rosettacode.org/wiki/Vigen%C3%A8re_cipher#Ruby + + strings_to_encode + encryption_key + + + + + + + + + encoded_strings_and_keys + diff --git a/modules/encoders/cipher/vignere/vignere.pp b/modules/encoders/cipher/vignere/vignere.pp new file mode 100644 index 000000000..e69de29bb diff --git a/scenarios/examples/parameterised_examples/encoder_examples/vignere_cipher.xml b/scenarios/examples/parameterised_examples/encoder_examples/vignere_cipher.xml new file mode 100644 index 000000000..73dc95481 --- /dev/null +++ b/scenarios/examples/parameterised_examples/encoder_examples/vignere_cipher.xml @@ -0,0 +1,20 @@ + + + + + + example_server + + + + + + + + + + + +