Python/ciphers/Onepad_Cipher.py
Jonathan Gómez c0f7df7e22 Fixed error on chr function when decrypt (#359)
On line 23 when make the operations returns a float and chr function doesn't permit float values as parameters.
2018-10-19 15:42:19 +02:00

33 lines
836 B
Python

from __future__ import print_function
import random
class Onepad:
def encrypt(self, text):
'''Function to encrypt text using psedo-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
def decrypt(self, cipher, key):
'''Function to decrypt text using psedo-random numbers.'''
plain = []
for i in range(len(key)):
p = int((cipher[i]-(key[i])**2)/key[i])
plain.append(chr(p))
plain = ''.join([i for i in plain])
return plain
if __name__ == '__main__':
c, k = Onepad().encrypt('Hello')
print(c, k)
print(Onepad().decrypt(c, k))