Quick actions

cmd+k|ctrl+k

Navigation

Languages

Vigenere Cipher

Snippet info

Language

Python

Visibility

public

Author

elaine.fawne

Created

2020-05-06T08:00:27Z

Updated

2020-05-06T08:00:52Z

def generateKey(string, key): 
    key = list(key) 
    if len(string) == len(key): 
        return(key) 
    else: 
        for i in range(len(string) - len(key)): 
            key.append(key[i % len(key)]) 
    return("" . join(key)) 
      
def cipherText(string, key): 
    cipher_text = [] 
    string= string.replace(" ", "")
    for i in range(len(string)): 
        x = (ord(string[i]) +  ord(key[i])) % 26
        x += ord('A') 
        cipher_text.append(chr(x)) 
    return("" . join(cipher_text)) 
      
def originalText(cipher_text, key): 
    orig_text = [] 
    for i in range(len(cipher_text)): 
        x = (ord(cipher_text[i]) -  ord(key[i]) + 26) % 26
        x += ord('A') 
        orig_text.append(chr(x)) 
    return("" . join(orig_text)) 
      
message = input("Input your message here: ")
keyword = input("\nInput your key here: ")
message= message.upper()
keyword= keyword.upper()
key = generateKey(message, keyword) 
cipher_text = cipherText(message,key) 
print("\nCiphertext :", cipher_text) 
print("Original/Decrypted Text :", originalText(cipher_text, key)) 
INFO