Python/ciphers/onepad_cipher.py

31 lines
849 B
Python
Raw Normal View History

import random
2018-07-02 08:07:25 +00:00
class Onepad:
def encrypt(self, text: str) -> ([str], [int]):
"""Function to encrypt text using pseudo-random numbers"""
2018-07-02 08:07:25 +00:00
plain = [ord(i) for i in text]
key = []
cipher = []
for i in plain:
k = random.randint(1, 300)
2019-10-05 05:14:13 +00:00
c = (i + k) * k
cipher.append(c)
key.append(k)
return cipher, key
def decrypt(self, cipher: [str], key: [int]) -> str:
"""Function to decrypt text using pseudo-random numbers."""
plain = []
for i in range(len(key)):
2019-10-05 05:14:13 +00:00
p = int((cipher[i] - (key[i]) ** 2) / key[i])
plain.append(chr(p))
2019-10-05 05:14:13 +00:00
plain = "".join([i for i in plain])
return plain
2018-07-02 08:07:25 +00:00
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
c, k = Onepad().encrypt("Hello")
2018-07-02 08:07:25 +00:00
print(c, k)
print(Onepad().decrypt(c, k))