File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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 ))
You can’t perform that action at this time.
0 commit comments