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


 
ori$ = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
key$ = filter$("vigenerecipher")
print ori$ 
print key$
enc$ = encrypt$(ori$, key$)
print enc$
dec$ = decrypt$(enc$, key$)
print dec$
 
end
 
function encrypt$(text$, key$)
    flt$ = filter$(text$)
    encrypt$ = ""
    j = 1
    for i = 1 to len(flt$)
        m$ = mid$(flt$, i, 1)
        m = asc(m$)-asc("A")
        k$ = mid$(key$, j, 1)
        k = asc(k$)-asc("A")
        j = (j mod len(key$)) + 1
        c = (m + k) mod 26
        c$=chr$(asc("A")+c)
        encrypt$=encrypt$+c$
    next
end function
 
function decrypt$(flt$, key$)
    decrypt$ = ""
    j = 1
    for i = 1 to len(flt$)
        m$ = mid$(flt$, i, 1)
        m = asc(m$)-asc("A")
        k$ = mid$(key$, j, 1)
        k = asc(k$)-asc("A")
        j = (j mod len(key$)) + 1
        c = (m - k + 26) mod 26
        c$=chr$(asc("A")+c)
        decrypt$=decrypt$+c$
    next
end function
 
function filter$(ori$)
'a..z A..Z go caps, other skipped
    filter$=""
    for i = 1 to len(ori$)
        c$ = upper$(mid$(ori$,i,1))
        if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", c$) then filter$ = filter$ + c$
    next
end function 
 