-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
54 lines (44 loc) · 1.71 KB
/
caesar.py
File metadata and controls
54 lines (44 loc) · 1.71 KB
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
#!/usr/bin/env python
import string
import argparse
# alphabet of lowercase letters
letters = string.ascii_lowercase
def encrypt(plaintext, key):
encrypted = ""
for c in plaintext:
# if character is in alphabet
if c.lower() in letters:
# find letter which is key letters forward in alphabet, using mod 26 to wrap round
c_encrypted = letters[(letters.index(c.lower())+key)%26]
# uppercase encrypted character is plaintext was uppercase
if c.isupper():
c_encrypted = c_encrypted.upper()
# if character is not in alphabet
else:
c_encrypted = c
encrypted += c_encrypted
return encrypted
def decrypt(cipher, key):
# decryption is same as encryption with negative key
return encrypt(cipher, -key)
def bruteforce(cipher):
# decrypt with all possible keys
for i in range(1, 26):
print(str(i) + " : " + decrypt(cipher, i) + "\n")
def main():
parser = argparse.ArgumentParser(description = "A simple caesar cipher program")
parser.add_argument("command", help = "encrypt, decrypt, or brute (try all possible keys)")
parser.add_argument("-k", "--key", help = "numeric caesar cipher key for encryption or decryption", required = False)
parser.add_argument("-t", "--text", help = "text to be encrypted or decrypted")
args = parser.parse_args()
if args.command == "encrypt":
print(encrypt(args.text, int(args.key)))
elif args.command == "decrypt":
print(decrypt(args.text, int(args.key)))
elif args.command == "brute":
bruteforce(args.text)
else:
print("Error: unrecognized command")
exit(1)
if __name__ == "__main__":
main()