Algoritmo de Vigenère em ruby

July 9th, 2009 by Global Leave a reply »

O algoritmo de cesar permaneceu como fonte suprema de criptografia por muitos anos. Entretanto, com a descoberta do ataque de frequencias ele foi facilmente quebrado. A substituição monoalfabética foi transformada por Vigenère que criou a polialfabética, realizando várias substuições mono a partir de uma tabela. Assim, a letra A poderia ser cifrada para quaisquer das 26 letras no mesmo texto, a depender da chave utilizada. Esse método durou mais de 200 anos quando Charles Babbage em 1854 quebrou o código.

Veja a implementação da criptografia e descriptografia de Vigenère.

#!/usr/bin/ruby1.9
 
# Vigenère crypt and decrypt algorithms
# 
# by Anderson Goulart <global@codekab.com>
# on Thu, 09 Jul 2009 17:16:20 -0300
 
$vigenere_matrix = [ 
[ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"], 
[ "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a"],
[ "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b"], 
[ "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c"],
[ "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d"], 
[ "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e"],
[ "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f"], 
[ "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g"],
[ "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h"], 
[ "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i"],
[ "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"], 
[ "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"],
[ "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"], 
[ "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"],
[ "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"], 
[ "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"],
[ "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"], 
[ "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q"],
[ "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r"], 
[ "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s"],
[ "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"], 
[ "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u"], 
[ "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v"],
[ "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w"], 
[ "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x"],
[ "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"]]
 
 
$alphabet = { "a" => 0, "b" => 1, "c" => 2,
	 		  "d" => 3, "e" => 4, "f" => 5,
  		  	  "g" => 6, "h" => 7, "i" => 8,
			  "j" => 9, "k" => 10, "l" => 11,
			  "m" => 12, "n" => 13, "o" => 14,
		 	  "p" => 15, "q" => 16, "r" => 17,
			  "s" => 18, "t" => 19, "u" => 20,
			  "v" => 21, "w" => 22, "x" => 23,
			  "y" => 24, "z" => 25 }
 
def crypt(text, key)
 
	cipher = Array.new()
 
	j = 0
 
	for i in 0..text.length-1
		cipher[i] = $vigenere_matrix[$alphabet[text[i]]][$alphabet[key[j]]]
		j += 1
		if j == key.length
			j = 0
		end
	end
 
	return cipher
end
 
def decrypt(text, key)
 
	clear_text = Array.new()
 
	j = 0
 
	for i in 0..text.length-1
		clear_text[i] = $alphabet.key(($alphabet[text[i]] - $alphabet[key[j]] + 26)%26)
		j += 1
		if j == key.length
			j = 0
		end
	end
 
	return clear_text
end
 
# main program
 
# choose your own values below
$t = "cryptography is cool"
$key = "ruby"
 
# removes white spaces
$text = $t.gsub(/ /,'')
 
# prints the result
puts "Original text:  " + $text + "\n"
 
puts "key: " + $key + "\n"
 
$cipher = crypt($text, $key).join
puts "Cipher text:    #$cipher \n"
 
$clear_text = decrypt($cipher, $key).join
puts "Decrypted text: #$clear_text"
Advertisement

Comments are closed.