Modified Vigenere Cipher In Python - Alphabet
This is what I have to do: Write a script in Python that is an implementation of a version of the Vigenere cipher for English text. Your script should distinguish lowercase and up
Solution 1:
You code currently has the following problems (I am assuming that var = 0, encr = [], encr1 = [], key1 = list(key) and text1 = list(text) happen before the code you have posted):
- Your
whileloop will never start if the key is longer than the plaintext, and never end otherwise, as you never shortentext1(and doing so would break your indexing); - You don't need to manually increment the counter in a
forloop (and if you wanted to,i + 1without assignment effectively does nothing, you needi += 1); - When using mod (
%), you don't need to check if e.g.num3 < 25; and - If you get around to including them, note that the extra characters you list aren't the same as those specified (
"/" != "-").
Rather than using ord and chr, I would build my own alphabet to cycle over, e.g.
from string import ascii_uppercase
alphabet = list(ascii_uppercase) + [",", ".", "-", "_"]
You might find this implementation of the "standard" Vigenère helpful:
from itertools import cycle
def vigenere(plain, key, alphabet):
for p, k in zip(plain, cycle(key)):
index = alphabet.index(p) + alphabet.index(k)
yield alphabet[index % len(alphabet)]
Here is some pseudo code for your modified implementation:
convert all text (plain, key) to upper case
create a list to hold output
split plain into blocks of length len(key)
for each block in plain:
if it's the first block, use key to encode
otherwise, use the last block of encoded text in output
Post a Comment for "Modified Vigenere Cipher In Python - Alphabet"