Task

Implement a   Vigenère cypher,   both encryption and decryption.

The program should handle keys and text of unequal length,
and should capitalize everything and discard non-alphabetic characters. 

(If your program handles non-alphabetic characters in another way,
make a note of it.)



Related tasks

   Caesar cipher
   Rot-13
   Substitution Cipher


 
function Filtrar(cadorigen)
	filtrado = ""
	for i = 1 to length(cadorigen)
		letra = upper(mid(cadorigen, i, 1))
		if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", letra) then filtrado += letra
	next i
	return filtrado
end function
 
function Encriptar(texto, llave)
	texto = Filtrar(texto)
	cifrado = ""
	j = 1
	for i = 1 to length(texto)
		mSS = mid(texto, i, 1)
		m = asc(mSS) - asc("A")
		kSS = mid(llave, j, 1)
		k = asc(kSS) - asc("A")
		j = (j mod length(llave)) + 1
		c = (m + k) mod 26
		letra = chr(asc("A") + c)
		cifrado += letra
	next i
	return cifrado
end function
 
function DesEncriptar(texto, llave)
	descifrado = ""
	j = 1
	for i = 1 to length(texto)
		mSS = mid(texto, i, 1)
		m = asc(mSS) - asc("A")
		kSS = mid(llave, j, 1)
		k = asc(kSS) - asc("A")
		j = (j mod length(llave)) + 1
		c = (m - k + 26) mod 26
		letra = chr(asc("A")+c)
		descifrado += letra
	next i
	return descifrado
end function
 
llave = Filtrar("vigenerecipher")
cadorigen = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
print cadorigen
print llave
 
cadcifrada = Encriptar(cadorigen, llave)
print "   Cifrado: "; cadcifrada
print "Descifrado: "; DesEncriptar(cadcifrada, llave)
end
 