MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / Onepad

Class Onepad

ciphers/onepad_cipher.py:4–58  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2
3
4class Onepad:
5 @staticmethod
6 def encrypt(text: str) -> tuple[list[int], list[int]]:
7 """
8 Function to encrypt text using pseudo-random numbers
9 >>> Onepad().encrypt("")
10 ([], [])
11 >>> Onepad().encrypt([])
12 ([], [])
13 >>> random.seed(1)
14 >>> Onepad().encrypt(" ")
15 ([6969], [69])
16 >>> random.seed(1)
17 >>> Onepad().encrypt("Hello")
18 ([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
19 >>> Onepad().encrypt(1)
20 Traceback (most recent call last):
21 ...
22 TypeError: 'int' object is not iterable
23 >>> Onepad().encrypt(1.1)
24 Traceback (most recent call last):
25 ...
26 TypeError: 'float' object is not iterable
27 """
28 plain = [ord(i) for i in text]
29 key = []
30 cipher = []
31 for i in plain:
32 k = random.randint(1, 300)
33 c = (i + k) * k
34 cipher.append(c)
35 key.append(k)
36 return cipher, key
37
38 @staticmethod
39 def decrypt(cipher: list[int], key: list[int]) -> str:
40 """
41 Function to decrypt text using pseudo-random numbers.
42 >>> Onepad().decrypt([], [])
43 ''
44 >>> Onepad().decrypt([35], [])
45 ''
46 >>> Onepad().decrypt([], [35])
47 Traceback (most recent call last):
48 ...
49 IndexError: list index out of range
50 >>> random.seed(1)
51 >>> Onepad().decrypt([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
52 'Hello'
53 """
54 plain = []
55 for i in range(len(key)):
56 p = int((cipher[i] - (key[i]) ** 2) / key[i])
57 plain.append(chr(p))
58 return "".join(plain)
59
60
61if __name__ == "__main__":

Callers 1

onepad_cipher.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected