-1

Currently doing a Vigenere cipher in Python, and me and many people in my class are stuck on one aspect.

After we translated the keyword to ordinals, we need to add those number to a message to encrypt it. This is my code so far.

Input = input('Enter your message: ')

key = input('Enter the one word key: ')

times = len(Input)//len(key)+1

encryptedKey = (times*key)[:len(Input)]

output = []
for character in Input:
    number = ord(character) - 96
    output.append(number)

outputKey = []
for character in encryptedKey:
      numberKey = ord(character) - 96
      outputKey.append(numberKey)

print(encryptedKey)

print(outputKey)
print(Input)
print(output)

So if the Input is 'hello', and the key is 'bye', the keyword would become 'byeby' [2,25,5,2,25], and 'hello' would be [8,5,12,12,15]. I can't figure out a way to add the first 2 with 8, the 25 with 5, and so on.

I tried print(sum(output + outputKey)) but of course that just adds all the numbers together, meaning the answer is 111.

I also need them to turn back into letters, so that it ends up with the encrypted message.

Thanks!

2 Answers2

0

You could also try something like this (pseudocode):

int j = 0
for int i = 0; i < output.len(); i++
    print output[i] + outputKey[j]
    j++
    if j > outputKey.len()
        j = 0

That way, rather than extend your array from [b, y, e] to [b, y, e, b, y] you calculate the key once and use its index to loop over additional values as needed.

Kat Russo
  • 469
  • 4
  • 8
0

You're off to the right start. You've gotten your message and key translated into numbers.

keyphrase = [2,25,5,2,25]
message = [8,5,12,12,15]

Now you need to add them and modulo 26 so your answers are still a-z.

encrypted = [(keyphrase[i] + message[i])%26 for i in range(len(message))]
>>> encrypted
[10, 4, 17, 14, 14]

Now you need to turn those back into letters:

''.join(chr(c + 96) for c in encrypted)
'jdqnn'

And then you can recover the message by going the other way:

message = [(encrypted[i] - keyphrase[i])%26 for i in range(len(encrypted))]
>>> message
[8, 5, 12, 12, 15]
>>> ''.join(chr(c + 96) for c in message)
'hello'

A bit of an FYI, for computer cryptography, especially with a language like Python or C, it's usually standard to start counting at 0. So 'a' is 0, 'b' is 1, etc. You're starting at 1, which is okay, just be aware of it.

BlivetWidget
  • 10,543
  • 1
  • 14
  • 23
  • Thanks, super helpful. Now I just have to figure out how to make sure symbols do not encrypt! Still getting used to using .isalpha! – AnimeDeamon Nov 11 '15 at 15:20
  • @AnimeDeamon `.isalpha()` can certainly get you where you want to go. If you want my advice, I would not only 'not encrypt' symbols, I would wipe them from the message before you encrypt it. In any real cryptosystem, you want to encrypt anything you send, which means either encrypting punctuation or (in the case of a Vigenere cipher, which only operates on letters) leaving it out. Leaving them in but unencrypted gives away a lot of information. – BlivetWidget Nov 11 '15 at 17:44
  • My teacher said, although I understand it, that I have to make it simpler so that it is easier for her to understand - by putting it into more than one line. I tried but It kind of messed up the rest of my coding. – AnimeDeamon Nov 17 '15 at 10:15
  • @AnimeDeamon since comments can't show line formatting, it would be easier to read if you add what you have now to your question, or perhaps start a new question. – BlivetWidget Nov 17 '15 at 16:32
  • It doesn't matter anymore - my teacher told me as long as I understand it's okay - I just have to explain it better! :) – AnimeDeamon Nov 18 '15 at 14:57
  • @AnimeDeamon okay, glad to hear you've gotten the assignment figured out. – BlivetWidget Nov 18 '15 at 15:46
  • can I just ask one thing? What is the point of the [i] after the variables, I understand what you do apart from the use of [i]. – AnimeDeamon Nov 24 '15 at 10:28
  • @AnimeDeamon I'm using a list comprehension, they're an efficient alternative to loops. – BlivetWidget Nov 24 '15 at 15:58