-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere-cipher.py
62 lines (52 loc) · 1.72 KB
/
vigenere-cipher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Function to set key based on the text
def setKey(text, key):
text = text.replace(" ", "")
key = key.replace(" ", "")
if (len(text) > len(key)):
length = len(text) - len(key)
for character in range(length):
key += key[character]
return key
elif (len(text) < len(key)):
length = len(key) - len(text)
return key[:-length]
else:
return key
# Function to get character based on calculation from 2 character, text and key
def getCharacter(charText, charKey, options):
alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
charText = charText.upper()
charKey = charKey.upper()
if (charText in alphabet and charKey in alphabet):
if (options == "encrypt"):
index = (alphabet.index(charText) + alphabet.index(charKey)) % 26
elif (options == "decrypt"):
index = (alphabet.index(charText) - alphabet.index(charKey)) % 26
else:
return " "
return alphabet[index]
else:
return " "
# Function to encrypt plaintext
def encrypt(plaintext, key):
plaintext = plaintext.replace(" ", "")
ciphertext = ""
key = setKey(plaintext, key)
for character in range(len(plaintext)):
ciphertext += getCharacter(plaintext[character], key[character], "encrypt")
return ciphertext
# Function to decrypt ciphertext
def decrypt(ciphertext, key):
ciphertext = ciphertext.replace(" ", "")
plaintext = ""
key = setKey(ciphertext, key)
for character in range(len(ciphertext)):
plaintext += getCharacter(ciphertext[character], key[character], "decrypt")
return plaintext
# Main program
plaintext = "this is plaintext"
key = "it is key"
ciphertext = encrypt(plaintext, key)
print(ciphertext)
plaintext = decrypt(ciphertext, key)
print(plaintext)