-2

ive been stuck on this for hours now. I've been trying to encrypt a preset message with a preset vigenere key. so that the name lets say 'bob' shifts by the key 'abc' it shifts the word bob by the ascii values 'abc'.

I have this code right now but it isn't printing anything whatsoever and i'm incredibly confused on how to do this.

def vigenere(key, letter):

    keyVal = ord(key)
    letterVal = ord(letter)

    keyVal = keyVal - 97

    letterVal = keyVal + letterVal

    if letterVal >= ord("z"):
        letterVal = letterVal - 26

        print letterVal
SepZ
  • 11
  • 6
  • How are you calling this function? Or is the problem that you aren't? – tripleee Feb 25 '18 at 18:48
  • @tripleee i thought i was calling it by assigning them to the letterval / keyval variables. – SepZ Feb 25 '18 at 18:58
  • You're not calling `vignere(key, letter)` anywhere in your example... do you just need to call it with `key` and `letter` params? – match Feb 25 '18 at 19:07

1 Answers1

1

You have to do that in loop for a string, like I did below:

def vigenere(key, letter):

    letterChar = ""
    for i in range(len(letter)):
        keyVal = ord(key)
        letterVal = ord(letter[i])
        keyVal = keyVal - 97
        letterChar += chr(keyVal + letterVal)
        if letterVal >= ord('z'):
            letterVal = letterVal - 26

    print letterChar


vigenere('c', "pop")

It returns:

rqr
Muhammad Noman
  • 1,344
  • 1
  • 14
  • 28
  • how would i print a phrase out with a key? Like ascii key to shift the individual characters within the phrase for example if the phrase was cat and the key was abc. The c would become d, the a would become c and the t would become w. Making cat into dcw. – SepZ Feb 25 '18 at 19:44
  • you mean decryption? – Muhammad Noman Feb 25 '18 at 19:45
  • not decryption im a complete noob when it comes to python all it does is create a encrypted phrase with an ascii shift on the individual letters thank you for the help so far – SepZ Feb 25 '18 at 19:48
  • @SepZ check the edited answer, Now it is showed that "pop" becomes "rqr" with key: "c". Here c = 2, because you are subtracting 97 (i.e 'a') from c – Muhammad Noman Feb 25 '18 at 20:22
  • I didn't use 'a' here, it wont make any effect, because 'a'-'a' is 0, so the 'pop' will remain same as 'pop' – Muhammad Noman Feb 25 '18 at 20:29
  • Accept the answer if it helps you – Muhammad Noman Feb 25 '18 at 20:29