Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python function that uses a word as the encryption key, rather than an integer
#1
I am confused on what to add as my encryption function via the Vigenere cipher... I am more familiar with the Caesar method so I am drawing a complete blank here. Please help with any suggestions.

import alphabet_positon

letter = input("Enter a letter: ")

def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3,
                    'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6,
                    'H':7, 'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10,
                    'k':10, 'L':11, 'l':11, 'M':12, 'm':12, 'N': 13,
                    'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16,
                    'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19,
                    't':19, 'U':20, 'u':20, 'V':21, 'v':21, 'W':22,
                    'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25 }
    pos = alphabet_pos[letter]
    return pos 

def rotate(letter, rot):
shift = 97 if letter.islower() else 65
return chr((ord(letter) + rot - shift) % 26 + shift)

letter = input('Enter a letter: ')
rot = int(input('Enter a number: '))
print(rotate(letter, rot))

# add encrypt function 
Quote
#2
The Vigenere Cipher is just a repeated Caesar Cipher. It takes any string of letters as a key, such as 'python'. You would then encrypt the first letter of the message using the Caesar Cipher where A -> P, the second letter of the message with the Caesar Cipher where A -> Y, and so. When you ran out of letters in the key (on the seventh letter of the message), you would start over again with P.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3

import alphabet_positon
 
letter = input("Enter a letter: ")
 
def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3,
                    'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6,
                    'H':7, 'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10,
                    'k':10, 'L':11, 'l':11, 'M':12, 'm':12, 'N': 13,
                    'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16,
                    'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19,
                    't':19, 'U':20, 'u':20, 'V':21, 'v':21, 'W':22,
                    'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25 }
    pos = alphabet_pos[letter]
    return pos 
 
def rotate(letter, rot):
shift = 97 if letter.islower() else 65
return chr((ord(letter) + rot - shift) % 26 + shift)
 
letter = input('Enter a letter: ')
rot = int(input('Enter a number: '))
print(rotate(letter, rot))
 
def vigenere_cipher(plaintext, key):
    """Encrypt plaintext using a key."""
    return ''.join([
        encrypt_letter(letter, key)
        for letter, key in cipher_list(plaintext, key)]) 

When I try to run the code, nothing returns
Quote
#4
I'm supposed to create a program that uses a word as the encryption key that behaves like this in the end

$ python vigenere.py
Type a message:
The crow flies at midnight!
Encryption key:
boom
Uvs osck rmwse bh auebwsih!
My main issue is with the encrypting aspect of it.

def vigenere_cipher(plaintext, key):
encrypted_string = ''
for i in range(len(key)):
# loop over plaintext by index
    plaintextVal = ord(plaintext)
    # if character in plaintext is alpha do the following:
    keyVal = ord(key[i])
    keyVal = keyVal - 97
    # get alphabet position for character in key

    plaintextChar += chr(keyVal + plaintextVal)
    if plaintextVal >= ord('z')
    #get alphabet position for character in plaintext
        plaintextVal = plaintextVal - 26
        # rotate character from plaintext with a character from the key 
        # convert new position back to a character if you haven't done it already
        # concatenate new character to an encrypted string             


        print PlaintextVal

return encrypted_string
I'm getting a bunch of invalid syntax error throughout and I'm confused on how to go about fixing the code.

Thanks in advanced!
Quote
#5
Reading this code I get little bit confused.

- why you import alphabet_position and then define function with same name?
- why you ask two times for letter?
- is it typo or functions rotate is not indented?

Instead of writing dictionary manually one can let Python create it:

alphabet_pos = {}

for i, v in enumerate(zip(range(65, 91), range(97, 123))):
    alphabet_pos.update({**dict.fromkeys([chr(v[0]), chr(v[1])], i)})
It is also not clear what function vigenere_cipher should accomplish.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  write an integer to file using a function xvkxvo 4 253 Dec-10-2019, 11:27 PM
Last Post: xvkxvo
  File encryption itzik 5 198 Nov-05-2019, 12:29 PM
Last Post: Gribouillis
  print a word after specific word search evilcode1 8 289 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  A function for correctly using "a/an" before a word Exsul1 2 191 Oct-07-2019, 07:51 PM
Last Post: newbieAuggie2019
  Vernam encryption method for files JohnCTX 1 195 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  python cache for small integer Uchikago 1 485 Jun-27-2019, 05:32 PM
Last Post: ichabod801
  Regarding encryption and decryption naressh1994 1 437 Jan-25-2019, 07:26 AM
Last Post: buran
  AES encryption - does not match between arduino and python 3 crypto guillaume55 0 1,273 Sep-23-2018, 11:14 AM
Last Post: guillaume55
  How to sum up the elements of an integer using python split/join? mohanraj1986 5 757 Aug-27-2018, 09:13 AM
Last Post: perfringo
  lab experiment / encryption pythan 0 741 Jun-09-2018, 07:19 PM
Last Post: pythan

Forum Jump:


Users browsing this thread: 1 Guest(s)