MCPcopy Create free account
hub / github.com/geekcomputers/Python / vigenere

Function vigenere

vigenere_cipher.py:5–25  ·  view source on GitHub ↗
(message, key, direction=1)

Source from the content-addressed store, hash-verified

3
4
5def vigenere(message, key, direction=1):
6 key_index = 0
7 alphabet = "abcdefghijklmnopqrstuvwxyz"
8 final_message = ""
9
10 for char in message.lower():
11 # Append any non-letter character to the message
12 if not char.isalpha():
13 final_message += char
14 else:
15 # Find the right key character to encode/decode
16 key_char = key[key_index % len(key)]
17 key_index += 1
18
19 # Define the offset and the encrypted/decrypted letter
20 offset = alphabet.index(key_char)
21 index = alphabet.find(char)
22 new_index = (index + offset * direction) % len(alphabet)
23 final_message += alphabet[new_index]
24
25 return final_message
26
27
28def encrypt(message, key):

Callers 2

encryptFunction · 0.85
decryptFunction · 0.85

Calls 1

indexMethod · 0.45

Tested by

no test coverage detected