-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmexican_army_cipher.py
81 lines (69 loc) · 3.16 KB
/
mexican_army_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
import random
class MexicanArmyCipher:
def __init__(self, start_letter, ring_offsets, encode_to_text=False):
self.start_letter = start_letter.lower()
self.ring_offsets = ring_offsets
self.alphabets = "abcdefghijklmnopqrstuvwxyz"
self.rotated_alphabets = "abcdefghijklmnopqrstuvwxyz"
self.rings = self.rotate_list(ring_offsets)
self.encode_to_text = encode_to_text
def rotate_list(self, rings_offset):
start_ind = self.alphabets.index(self.start_letter)
rotated_chars_list = list(self.alphabets)
self.rotated_alphabets = ''.join(rotated_chars_list[start_ind:] + rotated_chars_list[:start_ind])
rings = []
for i in range(4):
ring = [j + (26 * i) for j in range(1, 26 + 1)]
if len(rings_offset) > i:
ring_offset = rings_offset[i] - 1
ring_offset = ring_offset % len(self.alphabets)
ring = ring[ring_offset:] + ring[:ring_offset]
rings.append(ring)
return rings
def encrypt(self, plaintext):
encrypted_text = []
for char in plaintext:
if char.isalpha():
encoded_char = random.choice(self.rings)[self.rotated_alphabets.index(char.lower())]
if encoded_char > 100:
encoded_char = random.choice(self.rings[:-1])[self.rotated_alphabets.index(char.lower())]
if self.encode_to_text:
encoded_char = str(int(encoded_char / 26)) + self.alphabets[(encoded_char % 26) - 1]
else:
encoded_char = str(encoded_char)
encrypted_text.append(encoded_char)
else:
encrypted_text.append(char)
return "-".join(encrypted_text)
def decrypt(self, ciphertext):
decrypted_text = ""
cipher_list = list(ciphertext.split('-'))
for char in cipher_list:
if char.isalnum():
if self.encode_to_text:
ring_number = int(char[0])
encoded_char = (len(self.alphabets) * ring_number) + self.alphabets.index(char[1:]) + 1
else:
encoded_char = int(char)
ring_number = int(encoded_char / 26)
ring = self.rings[ring_number]
decrypted_text += self.rotated_alphabets[ring.index((encoded_char))]
else:
decrypted_text += char
return decrypted_text
# Example usage: with encode to text false
cipher = MexicanArmyCipher('M', [10, 28, 74, 95], False)
plaintext = "Network Security"
print("Normal Text:", plaintext, "\nWith encode to text false")
encrypted_text = cipher.encrypt(plaintext)
print("Encrypted:", encrypted_text)
decrypted_text = cipher.decrypt(encrypted_text)
print("Decrypted:", decrypted_text)
# Example usage: with encode to text true
cipher = MexicanArmyCipher('M', [10, 28, 74, 95], True)
plaintext = "Network Security"
print("Normal Text:", plaintext, "\nWith encode to text true")
encrypted_text = cipher.encrypt(plaintext)
print("Encrypted:", encrypted_text)
decrypted_text = cipher.decrypt(encrypted_text)
print("Decrypted:", decrypted_text)