Encrypting a Single Character

FUNCTION – transformChar(character, selectedWheel, pointer):

The algorithm clearly states that the wheel is incremented (moved 1 position clockwise) after each message character is encoded. In simple terms this ensures that a repeated plaintext character in the message does not get encoded to the same ciphertext character.

Getting the wheel to turn is not so difficult, thanks to modulo 26 maths. Although, technically this function does not perform the turn (the message encryption/decryption routines do that), it does take the turning into consideration.

Pertinent bits of code

#Convert the ASCII character to alphabetical position of the character.
char_num = ord(character) - 65
#Choose the offset for wheel one or two. Then use the pointer value.
if (selectedWheel == 1):
    offset = WHEEL1[(char_num - pointer)%26] #Use mod 26 to stay within circle.
else:
    offset = WHEEL2[(char_num - pointer)%26] #Use mod 26 to stay within circle.
#Convert alphabetical position of the character back to an ASCII character.
character = chr(65 + (char_num + offset)%26) #Use mod 26 to stay within circle.

For Example:

If we are using Wheel I and the pointer is at “C”, then the pointer value would be 2. Next, if the character to be encoded was “F”, then the character number would be 5, and the calculated offset would be WHEEL1[5 - 2] = 7.

Thus, the encoded character is 5 + 7 = 12 => “M”.