SouthParkMe

Hi.

This site tends to hold geeky things.

Vigenère in Python

Vigenère in Python

I previously posted about the Vigenére cipher. Here is some code in Python 3. It is rather inelegant, but it does the job. If you don’t want to have an input file, remove the first ‘if’ down to the first ‘else’ and remove one level of indentation from ‘finished=False’ down to ‘lines+=ask’

# options
caponly=True #False preserves upper and lowercase, True is upper only
alphaonly=True # False preserves punctuation, True gives a text block

# Read the input, and put each line in a list of lines!
# There should be a file called inputtext.txt
if input('Read sample text? (y/n)')=='y':
    filetimetable = open("inputtext.txt","r")
    lines=filetimetable.readlines()
    filetimetable.close()
    print()
else:
    print()
    finished=False
    lines=""
    while not finished:
        print('Just = to finish')
        ask=input("Type your text: ")
        finished=(ask=='=')
        if not finished:
            lines+=ask
        
# Now to get the key
key=input('Please type a key to use: ')
key=key.upper()
keylen=len(key)


# Which way are we going?
enc=False
choice=input('(E)ncrypt or (D)ecrypt)')
if (choice=="E") or (choice=="e"):
    enc=True

# This is a counter, What position in the key am I?
i=0

# keep a count of letters used for groups
# only used if alphaonly is True
gpletter=0
gpcount=0

# Variable to hold the output
output=""


# Let's step through the data
for line in lines:
    newline=""

    # we have a line, let's go through character at a time
    for char in line:

        # let's check we have a letter
        asc=ord(char)
        if (64 < asc < 91) or ( 96 < asc < 123):

            # Now whether lower or upper case, let's
            # turn it into a number from 0 to 25.
            if (asc > 96):
                base=97
            else:
                base=65
            lett=asc-base
            # I now have a number from 0 to 25

            # I use 'base' to preserve the number
            # offset for the ascii ranges for lower and upper
            # however, over-ride this if the output is caps only
            if caponly:
                base=65

            # We will need a key letter
            # this gets the next key letter, looping the key
            keylett=ord(key[i])-65
            i=(i+1)%keylen

            # encrypting and decrypting are different operations
            #
            if enc:
                lett=(lett+keylett)%26+base
            else:
                lett=(lett-keylett)%26+base

            # turn the modified letter back into text
            char=chr(lett)

            # If not outputting punctuation, put things in groups
            if alphaonly:
                gpletter=(gpletter+1)%5
                if gpletter==0:
                    gpcount=(gpcount+1)%10
                    if gpcount==0:
                        char+='\n'
                    else:
                        char+=' '
        else:
            # suppress any non letter if option set
            if alphaonly:
                char=""
                
        # put all the characters together to a new line
        newline+=char
        
    # add a line to output
    output+=newline
    
# print the output
print(output)

Other posts on this topic

More Cryptography

Orbital Mechanics - Part 3

Orbital Mechanics - Part 3

Le Tour 2020

Le Tour 2020