mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
a652905b60
* ci(pre-commit): Add ``flake8-comprehensions`` to ``pre-commit`` (#7233) * refactor: Fix ``flake8-comprehensions`` errors * fix: Replace `map` with generator (#7233) * fix: Cast `range` objects to `list`
32 lines
859 B
Python
32 lines
859 B
Python
import random
|
|
|
|
|
|
class Onepad:
|
|
@staticmethod
|
|
def encrypt(text: str) -> tuple[list[int], list[int]]:
|
|
"""Function to encrypt text using pseudo-random numbers"""
|
|
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
|
|
|
|
@staticmethod
|
|
def decrypt(cipher: list[int], key: list[int]) -> str:
|
|
"""Function to decrypt text using pseudo-random numbers."""
|
|
plain = []
|
|
for i in range(len(key)):
|
|
p = int((cipher[i] - (key[i]) ** 2) / key[i])
|
|
plain.append(chr(p))
|
|
return "".join(plain)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
c, k = Onepad().encrypt("Hello")
|
|
print(c, k)
|
|
print(Onepad().decrypt(c, k))
|