- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaifer.py
More file actions
Latest commit
131 lines (97 loc) · 3.49 KB
/
Copy pathsaifer.py
File metadata and controls
131 lines (97 loc) · 3.49 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
importrandom
importstring
importsys
importos
importtkinterastk
fromtkinterimportfiledialog
#globals
chars=string.punctuation+string.digits+string.ascii_letters
chars=list(chars)
defmakeKey(key_name: str) ->str:
'''Generates a new encryption key
Args:
key_name (str): A name for the new key.
Returns:
str: Path it was saved to.
'''
# Define the folder path
folder_path="Keys"
# Create the folder if it doesn't exist
ifnotos.path.exists(folder_path):
os.makedirs(folder_path)
file_path=os.path.join(folder_path, key_name+".saiferkey")
#make a new key based on the chars list and shuffle it
key=chars.copy()
random.shuffle(key)
keyFileptr=open(file_path, "w")
keystr=', '.join(key)
keyFileptr.write(keystr)
keyFileptr.close()
returnfile_path
defencrypt(key_path: str, message_path: str, encrypted_name: str) ->str:
'''Encrypts a text file with a key
Args:
key_path (str): Path to the saiferkey file used in the encryption.
message_path (str): Path to the txt file to encrypt.
encrypted_name (str): A name for the encrypted file.
Returns:
str: Path where the saifertxt file can be found.
'''
keyFileptr=open(f"{key_path}", "r")
key=keyFileptr.read()
key=key.split(', ')
keyFileptr.close()
messageFileptr=open(f"{message_path}", "r")
message=messageFileptr.read()
messageFileptr.close()
encryptedMessage=""
forletterinmessage:
#things like â wont be encrypted, weird letters
if(chars.__contains__(letter)):
i=chars.index(letter)
encryptedMessage+=key[i]
else:
encryptedMessage+=letter
# Define the folder path
folder_path="SaiferTexts"
# Create the folder if it doesn't exist
ifnotos.path.exists(folder_path):
os.makedirs(folder_path)
# Define the file path
file_path=os.path.join(folder_path, encrypted_name+".saifertxt")
newFileptr=open(file_path, "w")
newFileptr.write(encryptedMessage)
newFileptr.close()
returnfile_path
defdecrypt(key_path: str, encrypted_path: str, decrypted_name: str) ->str:
'''Encrypts a text file with a key
Args:
key_path (str): Path to the saiferkey file used in the decryption.
encrypted_path (str): Path to the saifertxt file to decrypt.
decrypted_name (str): A name for the decrypted file.
Returns:
str: The path the decrypted message was saved to.
'''
keyFileptr=open(f"{key_path}", "r")
key=keyFileptr.read()
key=key.split(', ')
keyFileptr.close()
messageFileptr=open(f"{encrypted_path}", "r")
message=messageFileptr.read()
messageFileptr.close()
decryptedMessage=""
forletterinmessage:
#things like â wont be considered, weird letters
if(key.__contains__(letter)):
i=key.index(letter)
decryptedMessage+=chars[i]
else:
decryptedMessage+=letter
folder_path="DeSaiferedTexts"
ifnotos.path.exists(folder_path):
os.makedirs(folder_path)
file_path=os.path.join(folder_path, decrypted_name+".txt")
newFileptr=open(file_path, "w")
newFileptr.write(decryptedMessage)
newFileptr.close()
returnfolder_path