mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-25 12:40:14 +00:00
0fdd2d369e
In one pad algorithm length of key and length of message are equal which results in endless possibilities of false messages on bruteforce.
29 lines
816 B
Python
29 lines
816 B
Python
class Onepad:
|
|
def encrypt(self, text):
|
|
'''Function to encrypt text using psedo-random numbers'''
|
|
plain = []
|
|
key = []
|
|
cipher = []
|
|
for i in text:
|
|
plain.append(ord(i))
|
|
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 = (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)
|