-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvignere.py
52 lines (44 loc) · 1.61 KB
/
vignere.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
def vigenere_encrypt(plaintext, key):
ciphertext = ""
key_index = 0
key = key.upper()
for char in plaintext:
if char.isalpha():
shift = ord(key[key_index]) - ord('A')
if char.isupper():
ciphertext += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
else:
ciphertext += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
key_index = (key_index + 1) % len(key)
else:
ciphertext += char
return ciphertext
def vigenere_decrypt(ciphertext, key):
plaintext = ""
key_index = 0
key = key.upper()
for char in ciphertext:
if char.isalpha():
shift = ord(key[key_index]) - ord('A')
if char.isupper():
plaintext += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
else:
plaintext += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))
key_index = (key_index + 1) % len(key)
else:
plaintext += char
return plaintext
if __name__ == "__main__":
print("Vigenère Cipher")
choice = input("Do you want to (E)ncrypt or (D)ecrypt? ").strip().upper()
if choice in ['E', 'D']:
text = input("Enter the text: ").strip()
key = input("Enter the key: ").strip()
if choice == 'E':
result = vigenere_encrypt(text, key)
print("Encrypted Text:", result)
else:
result = vigenere_decrypt(text, key)
print("Decrypted Text:", result)
else:
print("Invalid choice. Please select either 'E' or 'D'.")