blob: 2100aeb960ebf12f3ab140659573f0b4c727b1e4 (
plain)
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
|
def chaocipher_encrypt(message: str) -> str:
"""
Encrypts a message using the Chaocipher algorithm.
Chaocipher is a symmetric encryption algorithm that uses two mixed alphabets to
perform a double substitution on each letter of the plaintext. The two alphabets
are predetermined and fixed.
Args:
message: The message to be encrypted.
Returns:
The encrypted message.
"""
# Define the Chaocipher alphabets
left_alphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
right_alphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
ciphertext = ""
left_index = 0
right_index = 0
# Loop through each character in the message
for char in message.upper():
if not char.isalpha():
# Ignore non-alphabetic characters
ciphertext += char
continue
# Find the index of the character in the left alphabet
left_char_index = left_alphabet.index(char)
# Swap the left and right indices
left_index, right_index = right_index, left_index
# Find the corresponding character in the right alphabet
right_char_index = (left_char_index + right_index) % 26
right_char = right_alphabet[right_char_index]
# Append the encrypted character to the ciphertext
ciphertext += right_char
return ciphertext
def chaocipher_decrypt(ciphertext: str) -> str:
"""
Decrypts a message that has been encrypted using the Chaocipher algorithm.
Args:
ciphertext: The message to be decrypted.
Returns:
The decrypted message.
"""
# Define the Chaocipher alphabets
left_alphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
right_alphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
plaintext = ""
left_index = 0
right_index = 0
# Loop through each character in the ciphertext
for char in ciphertext.upper():
if not char.isalpha():
# Ignore non-alphabetic characters
plaintext += char
continue
# Find the index of the character in the right alphabet
right_char_index = right_alphabet.index(char)
# Swap the left and right indices
left_index, right_index = right_index, left_index
# Find the corresponding character in the left alphabet
left_char_index = (right_char_index - right_index) % 26
left_char = left_alphabet[left_char_index]
# Append the decrypted character to the plaintext
plaintext += left_char
return plaintext
# Example usage
message = "Hello World!"
ciphertext = chaocipher_encrypt(message)
print(f"Ciphertext: {ciphertext}")
plaintext = chaocipher_decrypt(ciphertext)
print(f"Plaintext: {plaintext}")
|