Skip to content

Commit fa712df

Browse files
committed
add one time pad algorithm
1 parent 7b7e6a8 commit fa712df

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

ciphers/one_time_pad.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
text = 'dori'
3+
ciphers, keys = encrypt(text)
4+
decrypt(ciphers, keys)
5+
complexity: O(n)
6+
"""
7+
from random import randint
8+
from typing import List, Tuple
9+
10+
11+
def encrypt(text: str) -> Tuple[List[int]]:
12+
plain = [ord(letter) for letter in text]
13+
keys = []
14+
ciphers = []
15+
for letter_code in plain:
16+
random_key = randint(1, 300)
17+
cipher = (letter_code + random_key) * random_key
18+
keys.append(random_key)
19+
ciphers.append(cipher)
20+
return ciphers, keys
21+
22+
23+
def decrypt(ciphers: List[int], keys: List[int]) -> str:
24+
if len(ciphers) != len(keys):
25+
return None
26+
text = ""
27+
for cipher, key in zip(ciphers, keys):
28+
letter_code = int(cipher / key) - key
29+
text += chr(letter_code)
30+
return text
31+
32+
33+
if __name__ == "__main__":
34+
text = 'dori'
35+
ciphers, keys = encrypt(text)
36+
print(ciphers)
37+
print(keys)
38+
print(decrypt(ciphers, keys))

0 commit comments

Comments
 (0)