-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
45 lines (37 loc) · 1.26 KB
/
modules.py
File metadata and controls
45 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import random
class Modules():
def readTextFromTxt(filepath: str):
with open(filepath,'r',encoding="utf-8") as f:
return f.read().strip()
def writeTxtInDir(directory: str, text: str):
with open(directory + "/result.txt",'w',encoding="utf-8") as f:
f.write(text)
import random
def shuffleCrypto(s:str,key:int) -> str:
random.seed(key)
ln = len(s)
keys = random.sample(range(ln),ln)
out = ''
for i in keys: out += s[i]
return out
def shuffleDecrypt(s:str,key:int) -> str:
random.seed(key)
ln = len(s)
keys = random.sample(range(ln),ln)
out = [' ' for _ in range(ln)]
for i,j in zip(keys,s):
out[i] = j
return ''.join(out)
def caesarCrypto(s:str,shift:int) -> str:
m = []
for i in s:
if i.isalpha():
if i.isupper():
m.append(chr((ord(i) + shift - 65) % 26 + 65))
else:
m.append(chr((ord(i) + shift - 97) % 26 + 97))
else:
m.append(i)
return ''.join(m)
def caesarDecrypt(s:str,shift:int) -> str:
return Modules.caesarCrypto(s, 26 - shift)