-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.py
72 lines (60 loc) · 2.35 KB
/
caesar.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
import argparse
import pyperclip
import string
def caesarCipher(message, key, mode):
LETTERS = string.ascii_uppercase
translated = ''
message = message.upper()
for letter in message:
if letter in LETTERS:
num = LETTERS.find(letter)
if mode == 'encrypt':
num += key
elif mode == 'decrypt':
num -= key
if num >= len(LETTERS):
num -= len(LETTERS)
elif num < 0:
num += len(LETTERS)
translated += LETTERS[num]
else:
translated += letter
return translated
def brute_force_caesar_cipher(text):
for key in range(26):
decrypted_message = caesarCipher(text, key, 'decrypt')
print(f"Key {key}: {decrypted_message}")
def main():
parser = argparse.ArgumentParser(description="Caesar Cipher Encryptor/Decryptor")
parser.add_argument("--brute", action="store_true", help="Perform brute-force decryption")
parser.add_argument("-e", "--encrypt", action="store_true", help="Encrypt the message")
parser.add_argument("-d", "--decrypt", action="store_true", help="Decrypt the message")
parser.add_argument("-k", "--key", type=int, help="Encryption/Decryption key")
parser.add_argument("-m", "--message", type=str, help="Message to encrypt/decrypt")
args = parser.parse_args()
if args.brute:
if args.message:
print("Brute-force decryption results:")
brute_force_caesar_cipher(args.message)
else:
print("Please provide a message using the -m/--message option.")
elif args.encrypt:
if args.key is None or args.message is None:
print("Both key and message are required for encryption.")
return
result = caesarCipher(args.message, args.key, 'encrypt')
print("Encrypted Message:")
print(result)
pyperclip.copy(result)
elif args.decrypt:
if args.key is None or args.message is None:
print("Both key and message are required for decryption.")
return
result = caesarCipher(args.message, args.key, 'decrypt')
print("Decrypted Message:")
print(result)
pyperclip.copy(result)
else:
print("Please provide either -e for encryption or -d for decryption.")
if __name__ == "__main__":
main()