2018-07-02 08:07:25 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2018-09-28 04:17:28 +00:00
|
|
|
import random
|
|
|
|
|
2018-07-02 08:07:25 +00:00
|
|
|
|
2018-04-13 15:49:38 +00:00
|
|
|
class Onepad:
|
|
|
|
def encrypt(self, text):
|
|
|
|
'''Function to encrypt text using psedo-random numbers'''
|
2018-07-02 08:07:25 +00:00
|
|
|
plain = [ord(i) for i in text]
|
2018-04-13 15:49:38 +00:00
|
|
|
key = []
|
|
|
|
cipher = []
|
|
|
|
for i in plain:
|
|
|
|
k = random.randint(1, 300)
|
|
|
|
c = (i+k)*k
|
|
|
|
cipher.append(c)
|
|
|
|
key.append(k)
|
|
|
|
return cipher, key
|
|
|
|
|
|
|
|
def decrypt(self, cipher, key):
|
|
|
|
'''Function to decrypt text using psedo-random numbers.'''
|
|
|
|
plain = []
|
|
|
|
for i in range(len(key)):
|
2018-10-19 13:42:19 +00:00
|
|
|
p = int((cipher[i]-(key[i])**2)/key[i])
|
2018-04-13 15:49:38 +00:00
|
|
|
plain.append(chr(p))
|
|
|
|
plain = ''.join([i for i in plain])
|
|
|
|
return plain
|
2018-07-02 08:07:25 +00:00
|
|
|
|
|
|
|
|
2018-04-13 15:49:38 +00:00
|
|
|
if __name__ == '__main__':
|
2018-07-02 08:07:25 +00:00
|
|
|
c, k = Onepad().encrypt('Hello')
|
|
|
|
print(c, k)
|
|
|
|
print(Onepad().decrypt(c, k))
|