-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ciphers.py
More file actions
168 lines (123 loc) · 4.39 KB
/
test_ciphers.py
File metadata and controls
168 lines (123 loc) · 4.39 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
#!/usr/bin/env python3
"""
Test script for the Ciphers module to verify all functions work correctly.
"""
import numpy as np
import Ciphers as cp
def test_caesar():
"""Test Caesar cipher."""
print("Testing Caesar Cipher...")
message = "HELLO WORLD"
key = 3
encrypted = cp.caesar_encrypt(message, key)
decrypted = cp.caesar_decrypt(encrypted, key)
print(f"Original: {message}")
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")
assert decrypted == message, "Caesar cipher test failed"
print("✓ Caesar cipher test passed\n")
def test_vigenere():
"""Test Vigenère cipher."""
print("Testing Vigenère Cipher...")
message = "HELLO WORLD"
key = "KEY"
encrypted = cp.vigenere_encrypt(message, key)
decrypted = cp.vigenere_decrypt(encrypted, key)
print(f"Original: {message}")
print(f"Key: {key}")
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")
assert decrypted == message, "Vigenère cipher test failed"
print("✓ Vigenère cipher test passed\n")
def test_affine():
"""Test Affine cipher."""
print("Testing Affine Cipher...")
message = "HELLO"
a, b = 5, 8
encrypted = cp.affine_encrypt(message, a, b)
decrypted = cp.affine_decrypt(encrypted, a, b)
print(f"Original: {message}")
print(f"Keys: a={a}, b={b}")
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")
assert decrypted == message, "Affine cipher test failed"
print("✓ Affine cipher test passed\n")
def test_substitution():
"""Test Substitution cipher."""
print("Testing Substitution Cipher...")
message = "HELLO"
key = {"h": "m", "e": "n", "l": "o", "o": "p"}
encrypted = cp.substitution_encrypt(message, key)
decrypted = cp.substitution_decrypt(encrypted, key)
print(f"Original: {message}")
print(f"Key: {key}")
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")
assert decrypted == message, "Substitution cipher test failed"
print("✓ Substitution cipher test passed\n")
def test_hill():
"""Test Hill cipher."""
print("Testing Hill Cipher...")
message = "HELLO"
# Use a matrix that has an inverse modulo 26
key_matrix = np.array([[3, 2], [5, 7]])
encrypted = cp.hill_encrypt(message, key_matrix)
decrypted = cp.hill_decrypt(encrypted, key_matrix)
print(f"Original: {message}")
print(f"Key matrix:\n{key_matrix}")
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")
# Remove padding for comparison
decrypted_clean = decrypted.rstrip("x").upper()
message_clean = message.upper()
assert (
decrypted_clean == message_clean
), f"Hill cipher test failed: {decrypted_clean} != {message_clean}"
print("✓ Hill cipher test passed\n")
def test_otp():
"""Test One-Time Pad cipher."""
print("Testing OTP Cipher...")
message = "HELLO"
key = "MYSECRETKEY"
encrypted = cp.otp_encrypt(message, key)
decrypted = cp.otp_decrypt(encrypted, key)
print(f"Original: {message}")
print(f"Key: {key}")
print(f"Encrypted: {repr(encrypted)}") # Use repr to show special characters
print(f"Decrypted: {decrypted}")
assert decrypted == message, "OTP cipher test failed"
print("✓ OTP cipher test passed\n")
def test_safe_parsing():
"""Test safe parsing functions."""
print("Testing Safe Parsing Functions...")
# Test substitution key parsing
key_str = "{'a': 'b', 'c': 'd'}"
parsed_key = cp.safe_parse_substitution_key(key_str)
expected = {"a": "b", "c": "d"}
assert (
parsed_key == expected
), f"Substitution key parsing failed: {parsed_key} != {expected}"
# Test matrix parsing
matrix_str = "[[1, 2], [3, 4]]"
parsed_matrix = cp.safe_parse_matrix(matrix_str)
expected_matrix = np.array([[1, 2], [3, 4]])
assert np.array_equal(parsed_matrix, expected_matrix), "Matrix parsing failed"
print("✓ Safe parsing tests passed\n")
def main():
"""Run all tests."""
print("Running Cipher Tests...\n")
try:
test_caesar()
test_vigenere()
test_affine()
test_substitution()
test_hill()
test_otp()
test_safe_parsing()
print("🎉 All tests passed successfully!")
except Exception as e:
print(f"❌ Test failed: {e}")
return False
return True
if __name__ == "__main__":
main()