1

So, I'm trying to make a program that can encrypt and decrypt vigenere ciphers in python 3. I'm doing this for practice, but I'm really struggling with the creation of a cipher matrix. If you're unfamiliar with vigenere ciphers, here's a helpful photo of what I want.

The function I have to do the switching of the first term to the last is shift, and it works well. I just need to create the lists with every value of the alphabet shifted over. My code is below.

import string

alpha = list(string.ascii_lowercase)


def shift(inList):  #Shifts the first item in the list to the end
    first = inList[0]
    inList.pop(0)
    inList.append(first)
    return inList

lastList = alpha
cipher = alpha
for item in alpha:
    print(alpha.index(item))
    cipher = cipher[].append([shift(lastList)])
    #global lastList = cipher[-1]
    print(lastList)
    print(cipher)

My problem is the creation of the 2D array that holds the vigenere cipher. I can't seem to make it work. Above is the furthest I've gotten making it, and this solution won't compile.

GarrukApex
  • 153
  • 2
  • 10

2 Answers2

1

If all you want is creating the table, you can achieve it in one go like this:

for i in range(26):
     left = string.ascii_uppercase[i:]
     right = string.ascii_uppercase[:i]
     print('{}{}'.format(left, right))   
Mehdi Sadeghi
  • 4,104
  • 1
  • 24
  • 34
  • I edited the question in case it was unclear: I'm trying to generate a 2d array, which is what I'm stuggling with. I'm passing eveything through .lower(), so case shouldn't be an issue. – GarrukApex Mar 04 '18 at 01:36
0

I'm working on building one myself, so here's what I have for creating the table:

count = 0
ext_alph = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
alph_table = []
for y in range(26):
    new_row = []
    for x in range(26):
        new_row.append(ext_alph[x+count])
    count += 1
    alph_table.append(new_row)
for r in alph_table:
    print(r)

I figured out you can just shift over what you put into each new row. Because I didn't want to move directly from the end of the alphabet string to the start to continue creating the later rows (which start with x, y, z, a, b... for example), I just added another alphabet to the end. The last two lines are just for displaying the table, BTW.

Aidunlin
  • 1
  • 1