-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_playfair_cipher.py
More file actions
253 lines (212 loc) · 11 KB
/
test_playfair_cipher.py
File metadata and controls
253 lines (212 loc) · 11 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import unittest
import sys
import os
# Assuming playfair_cipher.py is in the same directory, import its functions.
# For a real environment, you might need a more complex path setup.
# In this environment, we assume direct import is possible.
try:
from playfair_cipher import (create_grid, prepare_message,
find_coordinates, encrypt_pair,
playfair_encrypt)
except ImportError:
# If running locally and the module is not found
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__))))
from playfair_cipher import (create_grid, prepare_message,
find_coordinates, encrypt_pair,
playfair_encrypt)
class TestPlayfairCipher(unittest.TestCase):
# Define a standard grid for testing encryption logic
# Keyword: MONARCHY
# M O N A R
# C H Y B D
# E F G I K
# L P Q S T
# U V W X Z
def setUp(self):
self.grid_monarchy = [['M', 'O', 'N', 'A', 'R'],
['C', 'H', 'Y', 'B', 'D'],
['E', 'F', 'G', 'I', 'K'],
['L', 'P', 'Q', 'S', 'T'],
['U', 'V', 'W', 'X', 'Z']]
# --- Test 1: Grid Creation (create_grid) ---
def test_create_grid_basic(self):
# Test basic grid generation and 'J' replacement
keyword = "MONARCHY"
expected_grid = self.grid_monarchy
self.assertEqual(create_grid(keyword), expected_grid)
def test_create_grid_duplicates(self):
# Test that duplicate letters are ignored
keyword = "BALLOON"
# B A L O N C D E F G H I K M P Q R S T U V W X Y Z
expected_grid = [
['B', 'A', 'L', 'O', 'N'],
['C', 'D', 'E', 'F', 'G'],
['H', 'I', 'K', 'M', 'P'],
['Q', 'R', 'S', 'T', 'U'],
[
'V', 'W', 'X', 'Y', 'Z'
] # A should wrap around or be a blank. Playfair is 25 letters, so only 25 unique characters can exist. The remaining letter should be 'Y'
]
grid = create_grid(keyword)
self.assertEqual(grid, expected_grid)
def test_create_grid_j_handling(self):
# Test that 'J' is replaced by 'I'
keyword = "JAZZ"
# J -> I, Unique: I A Z
# Remaining: B C D E F G H K L M N O P Q R S T U V W X Y
expected_grid = [['I', 'A', 'Z', 'B', 'C'], ['D', 'E', 'F', 'G', 'H'],
['K', 'L', 'M', 'N', 'O'], ['P', 'Q', 'R', 'S', 'T'],
['U', 'V', 'W', 'X', 'Y']]
self.assertEqual(create_grid(keyword), expected_grid)
def test_create_grid_non_alphabetic(self):
# Test that non-alphabetic characters are ignored
keyword = "SECRET 123 KEY."
# S E C R T K Y A B D F G H I L M N O P Q U V W X Z
expected_grid = [['S', 'E', 'C', 'R', 'T'], ['K', 'Y', 'A', 'B', 'D'],
['F', 'G', 'H', 'I', 'L'], ['M', 'N', 'O', 'P', 'Q'],
['U', 'V', 'W', 'X', 'Z']]
self.assertEqual(create_grid(keyword), expected_grid)
def test_create_grid_empty_keyword(self):
# Test that an empty keyword raises an error
with self.assertRaises(ValueError):
create_grid("")
with self.assertRaises(ValueError):
create_grid("123!")
# --- Test 2: Message Preparation (prepare_message) ---
def test_prepare_message_j_removal_and_cleaning(self):
# Test 'J' replacement and non-alphabetic removal
message = "HeLlO JOhN! 123"
expected_digraphs = ['HE', 'LX', 'LO', 'IO',
'HN'] # J -> I, O J -> O I -> OI
self.assertEqual(prepare_message(message), expected_digraphs)
#
def test_prepare_message_odd_length_padding(self):
# Message: ATTACK. Correctly results in odd length after repeat separation, requiring padding.
message = "ATTACK"
self.assertEqual(prepare_message(message), ['AT', 'TX', 'AC', 'KX'])
# Test odd length padding with 'Z' (when padding char 'X' is the last letter)
message = "ATTACKX"
self.assertEqual(
prepare_message(message),
['AT', 'TX', 'AC', 'KX']) # The X in ATTACKX is paired with K
message = "HELLO"
self.assertEqual(prepare_message(message), ['HE', 'LX', 'OX'])
def test_prepare_message_consecutive_repeats(self):
# Test standard repeat separation with 'X'
message = "BALLOON"
# Final: B A /L X/ L O / O N -> BA LX LO ON
self.assertEqual(prepare_message(message), ['BA', 'LX', 'LO', 'ON'])
def test_prepare_message_x_repeats(self):
# Test 'X' repeat separation with 'Z'
message = "AXENAX"
# Cleaned: AXENAX
# 1. A X -> AX
# 2. E N -> EN
# 3. A X -> AX
# Prepared: AXENAX. Even length. No change.
self.assertEqual(prepare_message(message), ['AX', 'EN', 'AX'])
message = "AXXA"
# Cleaned: AXXA
# 1. A X -> AX
# 2. X A (The second X is checked against A) -> XA
# Prepared: A X X A. Length 4.
self.assertEqual(prepare_message(message), ['AX', 'XA'])
# --- Test 3: Encryption Logic (find_coordinates & encrypt_pair) ---
def test_find_coordinates(self):
# Test coordinates for M O Y Z
self.assertEqual(find_coordinates(self.grid_monarchy, 'M'), (0, 0))
self.assertEqual(find_coordinates(self.grid_monarchy, 'O'), (0, 1))
self.assertEqual(find_coordinates(self.grid_monarchy, 'Y'), (1, 2))
self.assertEqual(find_coordinates(self.grid_monarchy, 'Z'), (4, 4))
with self.assertRaises(ValueError):
find_coordinates(self.grid_monarchy, 'J') # 'J' should not exist
def test_encrypt_pair_same_row(self):
# Digraph: AR (Row 0) -> Shift Right -> RM
self.assertEqual(encrypt_pair(self.grid_monarchy, 'AR'), 'RM')
# Wrap-around: RM (Row 0) -> Shift Right -> MC
self.assertEqual(encrypt_pair(self.grid_monarchy, 'RA'),
'MR') # R A -> M R
def test_encrypt_pair_same_column(self):
# Digraph: MT (Column 0, Row 0, Row 3) -> Shift Down -> LC
self.assertEqual(encrypt_pair(self.grid_monarchy, 'ML'), 'CU')
# Wrap-around: UL (Column 0, Row 4, Row 3) -> Shift Down -> M U
self.assertEqual(encrypt_pair(self.grid_monarchy, 'UL'), 'MU')
def test_encrypt_pair_rectangle(self):
# Digraph: HE -> Opposite Corners -> BP
# H(1,1), E(2,0) -> H(1,0)=C, E(2,1)=F (Error in textbook rule, let's use the code's rule)
# H(1,1), E(2,0) -> H(1,0)=C, E(2,1)=F -> The rule is (r1, c1), (r2, c2) -> (r1, c2), (r2, c1)
# H(1,1) -> (1, 0) -> C
# E(2,0) -> (2, 1) -> F
# Expected: CF
self.assertEqual(encrypt_pair(self.grid_monarchy, 'HE'), 'CF')
# Digraph: GA
# G(2,2), A(0,3) -> G(2,3)=I, A(0,2)=N
# Expected: IN
self.assertEqual(encrypt_pair(self.grid_monarchy, 'GA'), 'IN')
# --- Test 4: Decryption Logic (decrypt_pair) ---
# --- Test 5: End-to-End Tests ---
def test_full_cipher_case_1(self):
# Example: Keyword MONARCHY, Message HIDE THE GOLD
# Preparation: H I D E T H E G O L D
# H I D E T H E G O L D -> H I D E T H E G O L D X (padding is done after repeats)
# H I D E T H E G O L D X -> H I D E T H E G O L D X -> HI DE TH EG OL DX
# Final Digraphs: ['HI', 'DE', 'TH', 'EG', 'OL', 'DX']
# Ciphertext (from standard examples): B P M X R H E L M T C U
# NOTE: My preparation logic for 'hide the gold' is simpler than expected.
# H I D E T H E G O L D -> HI DE TH EG OL DX (length 11 padded to 12)
# Grid M O N A R, C H Y B D, E F G I K, L P Q S T, U V W X Z
# HI -> H(1,1), I(2,3) -> Rectangle -> H(1,3)=B, I(2,1)=F (Error on standard example, let's trust the code logic)
# H(1,1), I(2,3) -> (1,3)=B, (2,1)=F. Expected: BF (not BP)
# Let's check a standard textbook example for MONARCHY HIDE THE GOLD: BP MX RH EL MT CU
# HI -> BP: H(1,1), I(2,3). B(1,3), P(3,1). Incorrect according to my encryption pair logic.
# Assuming the standard textbook rule for HIDE THE GOLD is the goal:
# H(1,1) I(2,3) -> B(1,3) P(3,1)
# D(1,4) E(2,0) -> J(2,4) V(3,0) -> KB (My grid has K at (2,4) and L at (3,0)) -> K E (Wait, this is wrong)
# D(1,4) E(2,0) -> D(1,0)=C, E(2,4)=K -> CK (Not MX)
# Let's use a simpler, known test case: "WE ARE NOT SAFE" with keyword "PLAYFAIR"
# Keyword: PLAYFIR (J->I), Remaining: B C D E G H K M N O Q S T U V W X Z
# Grid P L A Y F, I R B C D, E G H K M, N O Q S T, U V W X Z
grid = create_grid("PLAYFAIR")
# P(0,0), L(0,1), A(0,2), Y(0,3), F(0,4)
# I(1,0), R(1,1), B(1,2), C(1,3), D(1,4)
# E(2,0), G(2,1), H(2,2), K(2,3), M(2,4)
# N(3,0), O(3,1), Q(3,2), S(3,3), T(3,4)
# U(4,0), V(4,1), W(4,2), X(4,3), Z(4,4)
# Message: WE ARE NOT SAFE
# Prepare: WE AR EN OT SA FE
# WE -> W(4,2), E(2,0) -> W(4,0)=U, E(2,2)=H -> UH
# AR -> A(0,2), R(1,1) -> A(0,1)=L, R(1,2)=B -> LB
# EN -> E(2,0), N(3,0) -> Same Col -> Shift Down -> N(3,0), O(3,1) -> N(3,0)=N, O(3,1)=O (Shift N(3,0) down to U(4,0), Shift O(3,1) down to V(4,1)) -> UV
# OT -> O(3,1), T(3,4) -> Same Row -> Shift Right -> Q S -> PQ (O->Q, T->P (Wrap)) -> Q P (O->Q, T->P (Wrap)) -> QP
# SA -> S(3,3), A(0,2) -> S(3,2)=Q, A(0,3)=Y -> QY
# FE -> F(0,4), E(2,0) -> F(0,0)=P, E(2,4)=M -> PM
# Expected Ciphertext (using code logic): UHLBUVQPQYPM
plaintext = "WE ARE NOT SAFE"
ciphertext = playfair_encrypt("PLAYFAIR", plaintext)
self.assertEqual(ciphertext, "UBLRUVQPQYPM")
def test_full_cipher_case_2_with_padding_and_repeats(self):
# Keyword: EXAMPLE, Message: COMMUNICATION
# Keyword: E X A M P L I (J->I), Remaining: B C D F G H K N O Q R S T U V W Y Z
# Grid: E X A M P, L I B C D, F G H K N, O Q R S T, U V W Y Z
# Message: COMMUNICATION
# Cleaned: COMMUNICATION
# C O M M -> C O M X M (i advances to U)
# U N I C A T I O N
# Prepared: C O M X M U N I C A T I O N. Length 14. Even.
# Digraphs: ['CO', 'MX', 'MU', 'NI', 'CA', 'TI', 'ON']
# CO -> C(1,3), O(3,0) -> C(1,0)=L, O(3,3)=S -> LS
# MX -> M(0,3), X(0,1) -> M(0,1)=X, X(0,3)=M -> XM
# MU -> M(0,3), U(4,0) -> M(0,0)=E, U(4,3)=Y -> EY
# NI -> N(2,4), I(1,1) -> N(2,1)=G, I(1,4)=D -> GD
# CA -> C(1,3), A(0,2) -> C(1,2)=B, A(0,3)=M -> BM
# TI -> T(3,4), I(1,1) -> T(3,1)=Q, I(1,4)=D -> QD
# ON -> O(3,0), N(2,4) -> O(3,4)=T, N(2,0)=F -> TF
# Expected Ciphertext: LSXNEYGD BMQDTF
plaintext = "COMMUNICATION"
keyword = "EXAMPLE"
ciphertext = playfair_encrypt(keyword, plaintext)
self.assertEqual(ciphertext, "LSXNEYGDBMQDTF")
if __name__ == '__main__':
print("Running Playfair Cipher Unit Tests...")
# This runs the tests and reports results
unittest.main(argv=['first-arg-is-ignored'], exit=False)