#!/usr/bin/env ruby DEBUG = false class CVigenere attr_reader :alphabet,:matrix attr_accessor :key,:crypted,:decrypted def initialize @alphabet = ('A'..'Z').to_a @matrix = [] @crypted = [] @decrypted = [] for i in 0..@alphabet.size-1 do @matrix << (@alphabet[i..-1] << @alphabet[0..i-@alphabet.size]).flatten.uniq end @key = '' end def fixmk(m,k) m = m.upcase.gsub(/\W/,'').split(//) k = k.upcase.gsub(/\W/,'').split(//) aux = [] while aux.size < m.size for i in 0..k.size-1 do aux << k[i] if aux.size < m.size end end return m,k,aux end def crypt(m='',k='') m,k,aux = fixmk(m,k) for i in 0..m.size-1 do self.crypted << self.matrix[self.alphabet.index(m[i])][self.alphabet.index(aux[i])] end self.crypted = self.crypted.join puts "CRYPTED: #{self.crypted}" if DEBUG end def decrypt(m='',k='') m,k,aux = fixmk(m,k) for i in 0..m.size-1 do self.decrypted << self.alphabet[self.matrix[self.alphabet.index(aux[i])].index(m[i])] end self.decrypted = self.decrypted.join puts "DECRYPTED: #{self.decrypted}" if DEBUG end end c = CVigenere.new if ARGV.size == 2 || (ARGV.size == 3 && ARGV[2].downcase == "c") c.crypt ARGV[0],ARGV[1] puts c.crypted elsif ARGV.size == 3 && ARGV[2].downcase == "d" c.decrypt ARGV[0],ARGV[1] puts c.decrypted else puts "No sabes usar esta mugre" Kernel::exit! end