-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.py
100 lines (91 loc) · 3.07 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import string
MIN = 32
MAX = 126
KEY_RANGE = MAX - MIN + 1
def vigenere_encrypt(plaintext, key):
ciphertext = ""
key_len = len(key)
for i, char in enumerate(plaintext):
if not (MIN <= ord(char) <= MAX) or not (MIN <= ord(key[i % key_len]) <= MAX):
raise ValueError("Invalid key or plaintext")
shift = ord(key[i % key_len]) % KEY_RANGE
possible_shift = ord(char) + shift
if possible_shift > MAX:
possible_shift = (MIN - 1) + (possible_shift - MAX)
ciphertext += chr(possible_shift)
return ciphertext
def vigenere_decrypt(ciphertext, key):
plaintext = ""
key_len = len(key)
for i, char in enumerate(ciphertext):
if not (MIN <= ord(char) <= MAX) or not (MIN <= ord(key[i % key_len]) <= MAX):
raise ValueError("Invalid key or ciphertext")
shift = ord(key[i % key_len]) % KEY_RANGE
possible_shift = ord(char) - shift
if possible_shift < MIN:
possible_shift = MAX + 1 - (MIN - possible_shift)
plaintext += chr(possible_shift)
return plaintext
def caesar_encrypt(plaintext, key):
if not isinstance(key, int):
raise ValueError("Invalid key: Not an integer")
shift = key % KEY_RANGE
original = ''.join(chr(i) for i in range(MIN, MAX + 1))
shifted = original[shift:] + original[:shift]
cipher = str.maketrans(original, shifted)
return plaintext.translate(cipher)
def caesar_decrypt(ciphertext, key):
if not isinstance(key, int):
raise ValueError("Invalid key: Not an integer")
shift = key % KEY_RANGE
original = ''.join(chr(i) for i in range(MIN, MAX + 1))
shifted = original[-shift:] + original[:-shift]
cipher = str.maketrans(original, shifted)
return ciphertext.translate(cipher)
def encrypt(cipher_type):
plaintext = input("Enter the plaintext: ")
key = input("Enter the key (integer for Caesar): ")
try:
if cipher_type == "v":
ciphertext = vigenere_encrypt(plaintext, key)
elif cipher_type == "c":
ciphertext = caesar_encrypt(plaintext, int(key))
print(f"Encrypted message:\n{ciphertext}")
except ValueError as e:
print(f"Error: {e}")
def decrypt(cipher_type):
ciphertext = input("Enter the ciphertext: ")
key = input("Enter the key (integer for Caesar): ")
try:
if cipher_type == "v":
plaintext = vigenere_decrypt(ciphertext, key)
elif cipher_type == "c":
plaintext = caesar_decrypt(ciphertext, int(key))
print(f"Decrypted message:\n{plaintext}")
except ValueError as e:
print(f"Error: {e}")
def main():
print("Welcome to Nick's Ciphermaker!")
while True:
mode = input("Choose mode: (e)ncrypt, (d)ecrypt, or (q)uit: ").lower()
if mode == "q":
print("Goodbye!")
break
elif mode in ["e", "d"]:
cipher = input("Choose cipher: (v)Vigenère or (c)Caesar: ").lower()
if cipher == "v":
if mode == "e":
encrypt("v")
else:
decrypt("v")
elif cipher == "c":
if mode == "e":
encrypt("c")
else:
decrypt("c")
else:
print("Invalid cipher choice!")
else:
print("Invalid mode choice!")
if __name__ == "__main__":
main()