Home > Mobile >  How to start again at the beginning of the word?
How to start again at the beginning of the word?

Time:06-22

To apply a Vigenere coding, we have to shift the letters but not all by the same number. The key is this time a keyword which each letter gives us the shift to be done (taking A for a shift of 0, B for a shift of 1 ...).

Let's take an example to explain the method: Let's imagine that the keyword is "MATHS" and the word to code is "PYTHON". To code P, I shift the number corresponding to M, i.e. 12 (because we start at 0 with A) which gives me B as coding for P. Let's move on to Y: I shift it by the number corresponding to A, i.e. 0, so Y is the coding for Y here. Let's go to T which is shifted by the number corresponding to T, i.e. 19, so T becomes M once shifted And so on.

import string

def vigenere_cipher(msg, shift):
    encrypted = ''
    for i,j in zip(msg,shift):
        new_index = ( string.ascii_uppercase.index(i)   string.ascii_uppercase.index(j) ) % 26
        encrypted  = string.ascii_uppercase[new_index]
    return encrypted
print(vigenere_cipher('PYTHON', 'MATH'))

If our keyword is too short we start again at the beginning of the word, i.e. N will be shifted by the number corresponding to M.

My problem right here is actually with the last part, How I can simply say that if the keyword is too short we start again at the beginning of the word ?

Because only "PYTH" part is encrypted to "BYMO" with MATH as a key but not the "ON"

CodePudding user response:

Just add the line shift = shift * (len(msg) // len(shift) 1) at the start of the function so shift is repeated until it's longer than msg (e.g. this line turns MATH into MATHMATH)

import string

def vigenere_cipher(msg, shift):
    shift = shift * (len(msg) // len(shift)   1)
    encrypted = ''
    for i,j in zip(msg,shift):
        new_index = (string.ascii_uppercase.index(i)   string.ascii_uppercase.index(j)) % 26
        encrypted  = string.ascii_uppercase[new_index]
    return encrypted

    
print(vigenere_cipher('PYTHON', 'MATH'))

Output: BYMOAN

CodePudding user response:

I think the main issue here is that you're zipping both msg and shift together, when you don't actually need to do so. You already understand the concept of using % to guarantee that you stay on a number smaller than your max number, so I'll modify your function to also use % to select which character from shift you want to use

import string

def vigenere_cipher(msg, shift):
    encrypted = ''
    shift_length = len(shift)
    for i, char in enumerate(msg):
        new_index = ( string.ascii_uppercase.index(char)   string.ascii_uppercase.index(shift[i % shift_length]) ) % 26
        encrypted  = string.ascii_uppercase[new_index]
    return encrypted

print(vigenere_cipher('PYTHON', 'MATH'))
  • Related