@@ -9,7 +9,9 @@ def encrypt(text: str) -> tuple[list[int], list[int]]:
9
9
>>> Onepad().encrypt("")
10
10
([], [])
11
11
>>> Onepad().encrypt([])
12
- ([], [])
12
+ Traceback (most recent call last):
13
+ ...
14
+ TypeError: Input must be a string
13
15
>>> random.seed(1)
14
16
>>> Onepad().encrypt(" ")
15
17
([6969], [69])
@@ -19,12 +21,27 @@ def encrypt(text: str) -> tuple[list[int], list[int]]:
19
21
>>> Onepad().encrypt(1)
20
22
Traceback (most recent call last):
21
23
...
22
- TypeError: 'int' object is not iterable
24
+ TypeError: Input must be a string
23
25
>>> Onepad().encrypt(1.1)
24
26
Traceback (most recent call last):
25
27
...
26
- TypeError: 'float' object is not iterable
28
+ TypeError: Input must be a string
27
29
"""
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
+
28
45
plain = [ord (i ) for i in text ]
29
46
key = []
30
47
cipher = []
@@ -51,14 +68,28 @@ def decrypt(cipher: list[int], key: list[int]) -> str:
51
68
>>> Onepad().decrypt([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
52
69
'Hello'
53
70
"""
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
+
54
82
plain = []
55
83
for i in range (len (key )):
56
84
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" )
57
88
plain .append (chr (p ))
58
89
return "" .join (plain )
59
90
60
91
61
92
if __name__ == "__main__" :
62
93
c , k = Onepad ().encrypt ("Hello" )
63
94
print (c , k )
64
- print (Onepad ().decrypt (c , k ))
95
+ print (Onepad ().decrypt (c , k ))
0 commit comments