-4
print ("")
print ("Welcome to Vigenere cipher!")
print ("")
print ("This program will encrypt then offer you a decryption of your message.")
print ("")
print ("Start this off with inputting your message you would like to encrypt.")
m = input("")
print ("")
print ("Now enter your keyword you would like to use. (it must be (" +str(len(m)),"or less letters.))")
k = input("")

if len(k) > len(m):
    print ("")
    print ("sorry this is an invaild keyword")
    print ("Please re-enter your keyword.")
    k = input("")
else:
    print ("Your keyword is great. Lets keep going")

print ("")

print ("Is this correct?")
print ("")

print("-------\n"
  "Message:  ",m,"\n"
  "Key:      ",k,
  "\n-------")
print ("")

print ("yes or no")
correct = input ("")
if 'yes'.startswith(correct.lower()):
print("Great! Lets keep going.")
else:
    print ("You previously put: " +str(m), " as your message.")
    print ("Please re-enter the keyword you wish to use. (it must be (" +str(len(m)),"or less letters.))")
    k = input("")
encrypted = ("")
print ("")

for a in m:
    print ("The message letters are")
    encrypt2 = ord(a)

    for b in k:
        print ("The keyword letters are")
        encrypt = ord(b)
        encrypt3 = (encrypt) + (encrypt2)
        encrpyted = chr(encrypt3)
        print (chr(encrypt3))

I need it to encrypt with the keyword, any help? p.s it needs to be basic like the code above. it is for GCSE (UK) if it needs to have some commands then can you please tell me what they do :) so i can explain it in the task :)

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Some code might be missing. but this should all be it –  May 20 '15 at 19:46
  • 2
    We aren't here to help you cheat on your coursework, @GeorgeTaylder. Please read http://stackoverflow.com/help/how-to-ask – jonrsharpe May 20 '15 at 19:55
  • what problem is this code having? Other then the fact that you want to re-look at your `if 'yes'.startswith` statement (print needs indented and that code is not best logic for checking for a "yes"). Or do you just need help understanding the logic of that encryption method? – LinkBerest May 20 '15 at 20:32
  • For the second part (vigenere ciphers) a very simple example of this problem (with explanation) can be found in [chapter 19 of Invent with Python](http://inventwithpython.com/hacking/chapter19.html) – LinkBerest May 20 '15 at 20:51
  • its just the encryption, all the rest i want to make it look a bit better, if you get what i mean. –  May 21 '15 at 15:34
  • im not asking to cheat. i was just asking for some examples. i haven't copied anything. i just dont know how i can solve it so it will actually encrypt and decrypt. –  May 21 '15 at 19:53

1 Answers1

1

first, failures in your question:

all the code up for a in m: is unnecessary, all that is not part of your problem, your real problem is to encrypt a word using the vigenere cipher's algorithm, this can be replaced by two lines:

message = "ATTACKATDAWN"
key_adj = "LEMONLEMONLE"

or if you prefer

message = "ATTACKATDAWN"
key = "LEMON"
#key_adj and message equal length
key_adj = key + key*((len(message)/len(key))-1) + key[:len(message)%len(key)]
#key_adj is LEMONLEMONLE

Note: i rename the variables so that the people who help you, better understand,

as the input isn't a problem now, i can solve the problem

second, failures in your algorithm:

The most visible fault is using nested for, more isn't better, this complicates your solution, for this problem you need only one for

for a in m:
    for b in k:
        ....

second failure, is the cipher equation .... for example, first letters:

letter_menssage = "A" -> ord("A") = 65
letter_key = "L"      -> ord("L") = 76
                                  +141 -> chr(141) = "ì"

with all this explanation , now I can show a solution:

#i use zip function, match letter message with letter key
# (A,L) (T,E) (T,M) (A,O) (C,N) (K,L) etc.
encrypt = ""
for letter_msg, letter_key in zip(message, key_adj):
    code_letter_msg = ord(letter_msg)-ord('A')
    code_letter_key = ord(letter_key)-ord('A')
    encrypt += chr((code_letter_msg+code_letter_key) % 26 + ord('A'))

print (encrypt)

you get:

LXFOPVEFRNHR

explanation equation:

letter_menssage = "A" -> ord("A")-65 = 0
letter_key      = "L" -> ord("L")-65 = 11
                                    ((0+11)%26 + 65)-> chr(76) = "L"

letter_menssage = "T" -> ord("A")-65 = 19
letter_key      = "E" -> ord("E")-65 = 4
                                    ((19+4)%26 + 65)-> chr(88) = "X"
Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62
  • no no no. i want it so the user can input the message and the key and then it will give out the encrypted message through the keyword. i dont want to edit any from k = input ("") –  May 21 '15 at 15:36
  • @GeorgeTaylder Yes, I understand .... but the input isn't your problem, it is one of the multiple reasons, you get the negative points in question – Jose Ricardo Bustos M. May 21 '15 at 15:39