-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharistocrats_K3.py
More file actions
69 lines (52 loc) · 2.13 KB
/
aristocrats_K3.py
File metadata and controls
69 lines (52 loc) · 2.13 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from string import ascii_uppercase as letters
# Returns a string with duplicates removed.
def remove_duplicates(key):
true_key = ''
for c in key:
if c not in true_key: true_key += c
return true_key
# Returns an alphabet constructed from the given key and offset
def create_alphabet(key, offset):
remainder = ''.join(c for c in letters if c not in key)
prefix = remainder[-offset:] if offset > 0 else ''
suffix = remainder[:-offset] if offset > 0 else remainder
return prefix + key + suffix
def validate_input(prompt, fn):
valid = False
response = input(prompt)
while not valid:
valid = fn(response)
if not valid:
print('Invalid response.')
response = input(prompt)
return response
# Main program
# Prompt user for key
key = validate_input('Enter an alphabetic key: ', lambda x: x.isalpha())
key = remove_duplicates(key.upper())
# Prompt user for offsets
max_offset = 26 - len(key)
offset_pt = validate_input(f'Enter a plaintext offset (0-{max_offset}): ', lambda x: x.isdigit() and int(x) < max_offset)
offset_pt = int(offset_pt)
offset_ct = validate_input(f'Enter a ciphertext offset (0-{max_offset}): ', lambda x: x.isdigit() and int(x) < max_offset)
offset_ct = int(offset_ct)
# Alphabets
pt_alphabet = create_alphabet(key, offset_pt)
ct_alphabet = create_alphabet(key, offset_ct)
# Cipher maps
forward_cipher = { p:c for p,c in zip(pt_alphabet, ct_alphabet) }
reverse_cipher = { c:p for p,c in zip(pt_alphabet, ct_alphabet) }
# Display the cipher
print('This creates the following cipher:\n')
print(f'PT | { ' '.join(pt_alphabet) } |')
print(f'CT | { ' '.join(ct_alphabet) } |\n')
print(f'PT | { ' '.join(letters) } |')
print(f'CT | { ' '.join(forward_cipher[c] for c in letters) } |\n')
print(f'CT | { ' '.join(letters) } |')
print(f'PT | { ' '.join(reverse_cipher[c] for c in letters) } |\n')
# Prompt user for message
message = input('Enter the message to encrypt (or nothing to stop): ').upper()
# Encrypt and print message
encrypted = ''.join(forward_cipher[c] if c.isalpha() else c for c in message)
print('The encrypted message is:')
print(encrypted)