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


 
cypher: procedure options (main);      /* 21 September 2012 */
   declare t(26) character (26);
   declare (i, j, k, L) fixed binary;
   declare (original, encoded, coder) character (1000) varying initial ('');
   declare cypher character (30) varying;
   declare (co, ct, cc) character (1);
 
   /* Set up cypher table. */
   t(1) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   do i = 2 to 26;
      t(i) =  substr(t(i-1), 2, 25) || substr(t(i-1), 1, 1);
   end;
 
   cypher = 'VIGILANCE';
   original = 'Meet me on Tuesday evening at seven.';
   put edit ('Message=', original) (a);
   original = uppercase(original);
 
   /* Create the cypher text, same length as original, or longer. */
   coder = repeat(cypher, length(original)/length(cypher));
 
   /* Encode the original message, character by character. */
   /* Non-alphabetic characters are ignored. */
   L = 0;
   do i = 1 to length(original);
      co = substr(original, i, 1);
      j = index(t(1), co);
      if j = 0 then iterate; /* Ignore non-alphabetic character */
      L = L + 1;
      ct = substr(coder, L, 1);
      k = index(t(1), ct);
      encoded = encoded || substr(t(j), k, 1);
   end;
   put skip data (encoded);
 
   /* DECODING. */
   put skip list ('Decoded=');
   do i = 1 to length(encoded);
      cc = substr(coder, i, 1);
      j = index(t(1), cc);
      k = index(t(j), substr(encoded, i, 1));
      put edit (substr(t(1), k, 1) ) (a(1));
   end;
end cypher;
 