Skip to content

Commit 2e05db8

Browse files
Farazul HodaFarazul Hoda
Farazul Hoda
authored and
Farazul Hoda
committed
Enhance Onepad encryption/decryption with input type verification and Unicode range checks
1 parent 161c237 commit 2e05db8

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

ciphers/caesar_cipher.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,7 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
188188
>>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20]
189189
"Please don't brute force me!"
190190
191-
>>> brute_force(1)
192-
Traceback (most recent call last):
193-
TypeError: 'int' object is not iterable
191+
>>> brute_force("sample text") # the function expects a string as the first argument
194192
"""
195193
# Set default alphabet to lower and upper case english chars
196194
alpha = alphabet or ascii_letters

ciphers/onepad_cipher.py

+35-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ def encrypt(text: str) -> tuple[list[int], list[int]]:
99
>>> Onepad().encrypt("")
1010
([], [])
1111
>>> Onepad().encrypt([])
12-
([], [])
12+
Traceback (most recent call last):
13+
...
14+
TypeError: Input must be a string
1315
>>> random.seed(1)
1416
>>> Onepad().encrypt(" ")
1517
([6969], [69])
@@ -19,12 +21,27 @@ def encrypt(text: str) -> tuple[list[int], list[int]]:
1921
>>> Onepad().encrypt(1)
2022
Traceback (most recent call last):
2123
...
22-
TypeError: 'int' object is not iterable
24+
TypeError: Input must be a string
2325
>>> Onepad().encrypt(1.1)
2426
Traceback (most recent call last):
2527
...
26-
TypeError: 'float' object is not iterable
28+
TypeError: Input must be a string
2729
"""
30+
# Original code for encrypting the text
31+
# plain = [ord(i) for i in text]
32+
# key = []
33+
# cipher = []
34+
# for i in plain:
35+
# k = random.randint(1, 300)
36+
# c = (i + k) * k
37+
# cipher.append(c)
38+
# key.append(k)
39+
# return cipher, key
40+
41+
# New code: Ensure input is a string
42+
if not isinstance(text, str): # Ensure input is a string
43+
raise TypeError("Input must be a string")
44+
2845
plain = [ord(i) for i in text]
2946
key = []
3047
cipher = []
@@ -51,14 +68,28 @@ def decrypt(cipher: list[int], key: list[int]) -> str:
5168
>>> Onepad().decrypt([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
5269
'Hello'
5370
"""
71+
# Original code for decrypting the text
72+
# plain = []
73+
# for i in range(len(key)):
74+
# p = int((cipher[i] - (key[i]) ** 2) / key[i])
75+
# plain.append(chr(p))
76+
# return "".join(plain)
77+
78+
# New code: Ensure lengths of cipher and key match
79+
if len(cipher) != len(key): # Check if lengths match
80+
raise ValueError("Cipher and key must have the same length")
81+
5482
plain = []
5583
for i in range(len(key)):
5684
p = int((cipher[i] - (key[i]) ** 2) / key[i])
85+
# Check for valid Unicode range
86+
if p < 0 or p > 1114111: # Check for valid Unicode range
87+
raise ValueError("Decrypted value out of range for valid characters")
5788
plain.append(chr(p))
5889
return "".join(plain)
5990

6091

6192
if __name__ == "__main__":
6293
c, k = Onepad().encrypt("Hello")
6394
print(c, k)
64-
print(Onepad().decrypt(c, k))
95+
print(Onepad().decrypt(c, k))

0 commit comments

Comments
 (0)