Visual Basic for Applications/Simple Vigenere Cipher in VBA

Summary

 * This VBA code module runs the sixteenth century Vigenere cipher. It is used to conceal the contents of a message,  and was probably used in short term military messaging .   The method is an early example of an attempt to disguise the natural frequency of the language used in communication. The sender and recipient share a secret word or phrase, the so-called key, used to scramble (encrypt) and unscramble (decrypt) the message.   The code does not include spaces, since these tend to assist in code breaking, and although it could have been further restricted to just capital letters, it was decided that adding integers would make it more useful.
 * The code is intended for Microsoft Excel, but is easily adapted to work in other MS Office products that run VBA.  For example, to run it in MS Word, the results will still display in the message box, but it will be necessary to comment-out all lines, (put an apostrophe before each), of the ' output to sheet 1  and  make columns fit sections .
 * Figure 1 shows the Vigenere table without integers or other characters. Figure 2, the basis for the coding, shows a similar table that includes integers and capitals, and no other characters.
 * The Vigenere cipher makes use of a repeated keyword or phrase. That is to say, the key string is repeated as often as necessary to cover the message, prior to working.  This can be seen in the example of Figure 1, where the very short key "BULGE" was extended to "BULGEBUL" to cover the eight characters of the message.   Clearly, the extension of the key in this way avoids any fixed relationship in the encryption, especially when more complex keys are used.   A very good description of the "Key Elimination" method for cracking such simplistic keys can be found in Vigenere Cipher.
 * The coded version of the cipher uses a calculation to simulate the tabular method. The twenty-six letters of the alphabet and the ten integers are assigned number values between zero and thirty-five. Then, for encryption, key values are modulo-36 added to message values to make the ciphertext.  For decryption, key values are subtracted from the ciphertext, again using modulo-36 arithmetic, and always producing positive values.   Numbers are converted back to characters for display.

Notes on the Code

 * No userform is provided. Instead, type message and key strings, and the boolean value for the working mode, into the top procedure directly.   Interested parties might well add a user form of their own.
 * CheckInputs makes sure that no illegal characters are included, while procedure LongKey makes a key value equal in length to the message.
 * CharaToMod36 converts each string character, of both message and key, to its set-position number. Another procedure, Mod36ToChara converts these numbers back again prior to display.
 * AddMod36 performs modulo-36 addition, and subtracts 36 from numbers larger than 35 to keep the result within the set. The procedure SubMod36 performs subtraction, and adds 36 to any negative results, again, to keep the numbers within range.
 * There is some latitude for improvement of the code.  For example, the set could be further extended, and the key could be tested to avoid some of the flaws that are characteristic of this cipher.   At present, the user must interpret the position for spaces in the decrypted results; this helps to better conceal the use of the frequently used space character. So, extend the set only at the risk of worsened performance. As mentioned before, a user form could be made to replace direct entry.
 * Because repeat patterns can develop, some care is needed in coding. Clearly, a key that consists only of one repeated character would not be very secure, especially if it were the letter A. (Try it!).  A good mixture of characters makes for the best key, and if the key completely covers the message without repetition, so much the better.   This latter situation helps to avoid patterns that might make for easier cracking. In fact, if instead of a repeated key, a hash of the key were used, many of these pattern weaknesses might be avoided.  Those who have an interest in such modifications will find hash procedures elsewhere in this series; (use base64 output). That said, care should be taken to include only alpha characters and integers from any such hash, or errors will result. (B64 strings from hash algorithms typically have three additional symbol characters to avoid, =, +, and /. )

A Larger Vigenere Table
If all else fails, and for those who prefer manual working anyway, the table in the above drop-box may be found useful. It lists both capitals and integers. Notice that although both tables have a passing similarity, their content is quite different in places, so they are not fully interchangeable.

A Worked Example
The following panel shows how the calculation works for the coded version. It is analogous to the adding and subtraction of character distances within a closed set. Other implementations of the manual method have included the sliding of one set of characters against another for the required distances, sometimes using concentric discs. Figure 2 can be interpreted as a listing of every possible combination of messages and keys. THE CHARACTER SET AND ITS VALUES A   B    C    D    E    F    G    H    I    J    K    L    M         0    1    2    3    4    5    6    7    8    9   10   11   12 N   O    P    Q    R    S    T    U    V    W    X    Y    Z        13   14   15   16   17   18   19   20   21   22   23   24   25

0   1    2    3    4    5    6    7    8    9        26   27   28   29   30   31   32   33   34   35        ENCRYPTION WORKING S   E    N    D    H    E    L    P      message               (1) B   U    L    G    E                     key B   U    L    G    E    B    U    L      extended key          (2) 18   4   13    3    7    4   11   15      message values        (3) 1  20   11    6    4    1   20   11      key values            (4) 19  24   24    9   11    5   31   26      (3)+(4)               (5)         T    Y    Y    J    L    F    5    0      cipher text (Note 1)  (7)

Note 1:  Subtract 36 from any numbers here that might exceed 35. Notice that each instance of "E" results in different cipher text. DECRYPTION WORKING T   Y    Y    J    L    F    5    0      cipher text           (8) B   U    L    G    E                     key B   U    L    G    E    B    U    L      extended key          (9) 19  24   24    9   11    5   31   26      cipher text values   (10) 1  20   11    6    4    1   20   11      key values           (11) 18   4   13    3    7    4   11   15      (10) minus (11)      (12) S   E    N    D    H    E    L    P      plain text (Note 2)  (15)

Note 2:  Add 36 to any numbers here that might become negative.

The VBA Code Module
Copy this entire code listing into an Excel standard module, save the file as a xlsm type, then run the top procedure. No user form code has been provided, so the user should enter his message (sTxt) and key (sKey) strings directly into the section identified in the top procedure. Be sure to identify whether encryption or decryption is intended with the setting of the variable bEncrypt.

Corrections: 6 April 2020; corrected a note in SubMod36; does not affect operation.