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 As String) As String
    Dim As String letra
    Dim As String filtrado = ""
    For i As Integer = 1 To Len(cadorigen)
        letra = Ucase(Mid(cadorigen, i, 1))
        If Instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", letra) Then filtrado += letra
    Next i
    Return filtrado
End Function 
 
Function Encriptar(texto As String, llave As String) As String
    texto = Filtrar(texto)
    Dim As String mSS, kSS, letra, cifrado = ""
    Dim As Integer m, k, c, j = 1
    For i As Integer = 1 To Len(texto)
        mSS = Mid(texto, i, 1)
        m = Asc(mSS) - Asc("A")
        kSS = Mid(llave, j, 1)
        k = Asc(kSS) - Asc("A")
        j = (j Mod Len(llave)) + 1
        c = (m + k) Mod 26
        letra = Chr(Asc("A") + c)
        cifrado += letra
    Next i
    Return cifrado
End Function
 
Function DesEncriptar(texto As String, llave As String) As String
    Dim As String mSS, kSS, letra, descifrado = ""
    Dim As Integer m, k, c, j = 1
    For i As Integer = 1 To Len(texto)
        mSS = Mid(texto, i, 1)
        m = Asc(mSS) - Asc("A")
        kSS = Mid(llave, j, 1)
        k = Asc(kSS) - Asc("A")
        j = (j Mod Len(llave)) + 1
        c = (m - k + 26) Mod 26
        letra = Chr(Asc("A")+c)
        descifrado += letra
    Next i
    Return descifrado
End Function
 
Dim Shared As String llave
llave = Filtrar("vigenerecipher")
 
Dim As String cadorigen = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
Print cadorigen 
Print llave
 
Dim As String cadcifrada = Encriptar(cadorigen, llave)
Print "   Cifrado: "; cadcifrada
Print "Descifrado: "; DesEncriptar(cadcifrada, llave)
Sleep
 