Skip to content

Commit a2b540f

Browse files
authored
Merge pull request TheAlgorithms#285 from yesIamHasi/patch-2
Create Onepad_Cipher.py
2 parents 31e1913 + 0fdd2d3 commit a2b540f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ciphers/Onepad_Cipher.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Onepad:
2+
def encrypt(self, text):
3+
'''Function to encrypt text using psedo-random numbers'''
4+
plain = []
5+
key = []
6+
cipher = []
7+
for i in text:
8+
plain.append(ord(i))
9+
for i in plain:
10+
k = random.randint(1, 300)
11+
c = (i+k)*k
12+
cipher.append(c)
13+
key.append(k)
14+
return cipher, key
15+
16+
def decrypt(self, cipher, key):
17+
'''Function to decrypt text using psedo-random numbers.'''
18+
plain = []
19+
for i in range(len(key)):
20+
p = (cipher[i]-(key[i])**2)/key[i]
21+
plain.append(chr(p))
22+
plain = ''.join([i for i in plain])
23+
return plain
24+
25+
if __name__ == '__main__':
26+
c,k = Onepad().encrypt('Hello')
27+
print c, k
28+
print Onepad().decrypt(c, k)

0 commit comments

Comments
 (0)