Skip to content
37 changes: 34 additions & 3 deletions ciphers/onepad_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def encrypt(text: str) -> tuple[list[int], list[int]]:
>>> Onepad().encrypt("")
([], [])
>>> Onepad().encrypt([])
([], [])
Traceback (most recent call last):
...
TypeError: Input must be a string
>>> random.seed(1)
>>> Onepad().encrypt(" ")
([6969], [69])
Expand All @@ -19,12 +21,27 @@ def encrypt(text: str) -> tuple[list[int], list[int]]:
>>> Onepad().encrypt(1)
Traceback (most recent call last):
...
TypeError: 'int' object is not iterable
TypeError: Input must be a string
>>> Onepad().encrypt(1.1)
Traceback (most recent call last):
...
TypeError: 'float' object is not iterable
TypeError: Input must be a string
"""
# Original code for encrypting the text
# plain = [ord(i) for i in text]
# key = []
# cipher = []
# for i in plain:
# k = random.randint(1, 300)
# c = (i + k) * k
# cipher.append(c)
# key.append(k)
# return cipher, key

# New code: Ensure input is a string
if not isinstance(text, str): # Ensure input is a string
raise TypeError("Input must be a string")

plain = [ord(i) for i in text]
key = []
cipher = []
Expand All @@ -51,9 +68,23 @@ def decrypt(cipher: list[int], key: list[int]) -> str:
>>> Onepad().decrypt([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
'Hello'
"""
# Original code for decrypting the text
# plain = []
# for i in range(len(key)):
# p = int((cipher[i] - (key[i]) ** 2) / key[i])
# plain.append(chr(p))
# return "".join(plain)

# New code: Ensure lengths of cipher and key match
if len(cipher) != len(key): # Check if lengths match
raise ValueError("Cipher and key must have the same length")

plain = []
for i in range(len(key)):
p = int((cipher[i] - (key[i]) ** 2) / key[i])
# Check for valid Unicode range
if p < 0 or p > 1114111: # Check for valid Unicode range
raise ValueError("Decrypted value out of range for valid characters")
plain.append(chr(p))
return "".join(plain)

Expand Down
Loading