Over the last year I have been programming on my own, mostly in Python. I haven't had any formal education, though I have followed some online instructions, and have been making things that interest me.

One of those things is my Vigenère encrypter. I've made a few versions of it, and I would appreciate feedback on my latest version. Such as, does it follow somewhat of a good design pattern for these things? How would it perform under heavy load?

If I can provide any additional information, please ask and I won't hesitate to do so.

This is my code:

from os import system

#characters to numbers table
dictDown = {'|':0,'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,
          ')':49,' ':50,'?':51,'\n':52,'@':53,'#':54,'/':55,'%':56,

#numbers to characters table
dictUp = {0:'|',1:'a',2:'b',3:'c',4:'d',5:'e',6:'f',7:'g',8:'h',
       49:')',50:' ',51:'?',52:'\n',53:'@',54:'#',55:'/',56:'%',

#encrypting function
def crypt(key,clearText):
    cryptText = []
    numKey = []
    keyCount = 0

    #translate the characters in the key to numbers
    for keyClearChar in key:
        keyNum = dictDown[keyClearChar]

    for clearChar in clearText:
        #loop characters in key
        if keyCount == len(key):
            keyCount = 0 
        charNum = dictDown[clearChar]
        keyIndex = numKey[keyCount]
        cryptChar = dictUp[charNum+keyIndex%len(dictUp)]
        keyCount += 1

    return ''.join(cryptText)

#inverted encrypting function
def decrypt(key,cryptText):
    clearText = []
    numKey = []
    keyCount = 0

    for keyClearChar in key:
        keyNum = dictDown[keyClearChar]

    for cryptChar in cryptText:
        if keyCount == len(key):
            keyCount = 0
        cryptNum = dictDown[cryptChar]
        keyIndex = numKey[keyCount]
        clearChar = dictUp[cryptNum-keyIndex%len(dictUp)]
        keyCount += 1

    return ''.join(clearText)

while True:
    choice = raw_input("Enter 'e' to encrypt,\nEnter 'd' to decrypt.\n\n")

    if choice.lower() == 'e':

        key = list(raw_input("Enter an encryption key here:\n"))
        clearText = list(raw_input("\nEnter the text to be encrypted here:\n"))

        cryptText = crypt(key,clearText)
        cryptDir = open("C:\Users\Yorick\Desktop\cryptText.txt",'w')
        print("Encryption finished.\nResult saved to desktop.\nPress enter to close the program.")

    elif choice.lower() == 'd':
        key = list(raw_input("Enter a decryption key here:\n"))

        #try opening encrypted txt file
            cryptDir = open("C:\Users\Yorick\Desktop\cryptText.txt",'r')
            cryptText = list(cryptDir.read())
        except IOError:
            print("No cryptText.txt found on desktop.")

        clearText = decrypt(key,cryptText)
        _=raw_input("\n\n\n\nPress enter to close.")

2 Answers 2


This is a good place for a list comprehension:

numKey = []
for keyClearChar in key:
        keyNum = dictDown[keyClearChar]

Can be succinctly written as

numKey = [dictDown[char] for char in key]

Python has the nice feature that the for loop does not care what it iterates over, as long as it is iterable. This means, you don't have to convert your keys and texts to lists, for char in "AB CD" works perfectly fine.

Your encrypt and decrypt function differ only by the parameter names and the sign used in the actual shift. You could combine them into one function.

from itertools import cycle

def vigenere(key, text, decrypt=False):
    sign = -1 if decrypt else 1

    numKey = [dictDown[char] for char in key]
    out = (dictUp[(dictDown[char] + sign*keyIndex) % len(dictUp)]
           for keyIndex, char in zip(cycle(numKey), text))
    return ''.join(out)

I used itertools.cycle (EDIT: itertools.zip_longest fills the shorter with a given value, we want to repeat the key, therefore cycle), which makes manually keeping track of the keyCount obsolete. I also inlined the whole calculation, though it can of course be written in a more verbose for loop. It is first build as a generator object, which is passed to join. This avoids building the list (which has the same length as the text, which is potentially big), saving memory.

EDIT: The index calculation was wrong because of missing parenthesis before the modulus (Also in your code).

You can now, if you want to, define convenience functions, as well:

def crypt(key, text):
    return vigenere(key, text)

def decrypt(key, text):
    return vigenere(key, text, True)

You should try to avoid polluting the namespace too much. Prefer

import os

(This is a bit of a preference, I did not adhere to it myself in the above function. However, it makes it obvious where a function comes from, at the price of additional characters.)

You should make the path to the files a constant at the beginning of the code, helping implementing command line arguments for that at a later time.

Move all your code not in a function into a main function. Then at the end of the code use:

if __name__ == "__main__":

where TEXT_FILE is the path to the file, obviously. This way you can import this script and use its functions at a later time (e.g. if you want to use the crypt function separately in another program).

If you want to make the path the first command-line argument you can now just do:

if __name__ == "__main__":
    text_file = sys.argv[1] if len(sys.argv) > 1 else TEXT_FILE

The built-in module string has some nice character classes. Your dictUp is basically just this:

import string
dictUp = dict(enumerate(string.printable[:95]))

where the :95 is there to exclude the last few (tab, newline, carriage return, etc). The order might be slightly different, so your code will not be downward compatible anymore if you make this change.

string.printable == '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

dictDown is then just the inverted dictionary of that:

def invert(d):
    return {v: k for k, v in d.iteritems()}

dictDown = invert(dictUp)

Or use itertools.count and make dictUp a simple string (dictUp[3] is not different for a list, a string or a dict, with all keys in range(95) defined).

from itertools import count

# technically not a dict anymore, but sufficient for what you use it for:
dictUp = string.printable[:95]
dictDown = dict(zip(dictUp, count()))

Note here that most python implementations cache single-letter strings, so dictUp[35] should be O(1) both for a dict and a string.

  • \$\begingroup\$ @TomaszJakubRup Thanks for spotting the missing closing ". Your edit was rejected because I amended the answer afterwards, but I fixed it myself. :) \$\endgroup\$
    – Graipher
    Aug 20, 2016 at 21:03
  • \$\begingroup\$ Woah, the list comprehension is very nice. I'm going to remember that. As for the other things, I'm going to read them thoroughly and do some practice with them. Thank you for your help! :) \$\endgroup\$
    – Yorick
    Aug 21, 2016 at 9:58
  • \$\begingroup\$ @Yorick Yes, they are nice :) Regarding the function vigenere, I just spotted a bug there, you want to use itertools.cycle, not itertools.zip_longest. \$\endgroup\$
    – Graipher
    Aug 21, 2016 at 10:14
  • \$\begingroup\$ Instead of a dict you could have a boolean encrypt parameter in the Vigenerè encryption, seems simpler. \$\endgroup\$
    – Caridorc
    Aug 22, 2016 at 11:34
  • 1
    \$\begingroup\$ @Caridorc you are right, a boolean is probably clearer here. Edited. \$\endgroup\$
    – Graipher
    Aug 22, 2016 at 11:36

Here are some things that may help you improve your code.

Don't use system("cls")

There are two reasons not to use system("cls") or system("pause"). The first is that it is not portable to other operating systems which you may or may not care about now. The second is that it's a security hole, which you absolutely must care about. Specifically, if some program is defined and named cls or pause, your program will execute that program instead of what you intend, and that other program could be anything. I'd recommend instead using ANSI escape sequences. For example:

def cls():

Don't hardcode filenames

Hardcoding filenames is not a good idea. Better would be to either fetch the file name from the user (as with a command line argument) or to simply write the output to the console.

Write to allow for reuse

The way it's currently written, there is not a way to easily reuse the cipher in another program. One way to allow that would be to wrap the main part of the program in a function like this:

if __name__ == '__main__':

This allows you to run the program from the command line if you decide to do so, but also to use the encrypt() and decrypt() routines from other programs without modification of the file.

Use docstrings

Using docstrings would help document your program and make it simpler for others using the code (including yourself at some future date!) to understand what the various routines are doing and how.

Consider a different construction

The essential feature of both encryption and decryption is the alphabet you're using. Right now, there is no linkage between the dictUp and dictDown structures except that (you hope!) they both have all of the same letters in the same order. If you decide to continue to use dictionaries, consider constructing both structures from a single string. Alternatively, consider using a single structure for both. One mechanism would be to simply use a static string and rely on indexing.

Fix the bug

If I attempt to encrypt the word "CIPHERING" with the key "BUTTERFLY", I get the following error:

Traceback (most recent call last):
  File "encipher.py", line 86, in <module>
    cryptText = crypt(key,clearText)
  File "encipher.py", line 46, in crypt
    cryptChar = dictUp[charNum+keyIndex%len(dictUp)]
KeyError: 127

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.