0

Spacing in a single letter viginere cipher

Trying to account for spacing/whitespaces in a viginere cipher in python. this is a single letter encryption. Please note that i'm very new to python! def encrypt(message, key): '''Vigenere encryption of message using key.''' # Converted to uppercase. # Non-alpha characters stripped out. message = filter(str.isalpha, message.upper()) def enc(c, k): '''Single letter encryption.''' return chr(((ord(k) + ord(c) - 2 * ord('A')) % 26) + ord('A')) return ''.join(starmap(enc, zip(message, cycle(key)))) def decrypt(message, key): '''Vigenere decryption of message using key.''' def dec(c, k): '''Single letter decryption.''' return chr(((ord(c) - ord(k) - 2 * ord('A')) % 26) + ord('A')) return ''.join(starmap(dec, zip(message, cycle(key)))) Have already tried this but it does not work def enc(c, k): '''Single letter encryption.''' if ord(c) == 32: return chr(ord(c)) else: return chr(((ord(k) + ord(c) - 2 * ord('A')) % 26) + ord('A')) This is what i get with my current code: "Hello World" --> "JKJLJYUPLY" key = "wasup" I want: "Hello world" --> "JKJLJ YUPLY" key "wasup"

9th Oct 2019, 8:16 PM
Hooman Amiri
Hooman Amiri - avatar
1 Odpowiedź
0
from itertools import starmap, cycle def func(s): return s.isalpha() or s == " " def enc(c, k): return chr(((ord(k) + ord(c) - 2 * ord('A')) % 26) + ord('A')) def encrypt(message, key): message = "".join(filter(func, message.upper())).split() return ' '.join(''.join(starmap(enc, zip(word, cycle(key)))) for word in message) print(encrypt("Hello world", "wasup")) # JKJLJ YUPLY
10th Oct 2019, 4:48 AM
Diego
Diego - avatar