-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplefileencryption.py
More file actions
275 lines (237 loc) · 8.45 KB
/
simplefileencryption.py
File metadata and controls
275 lines (237 loc) · 8.45 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import rsa
from fernet import Fernet
import string
import tkinter as tk
import tkinter.messagebox
import base64
import os
from tkinter import filedialog
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.fernet import Fernet
from tkinter import *
import tkinter.filedialog
def set_textenSimetrik(text):
e3.delete(0, END)
e3.insert(0, text)
return
def set_textdeSimetrik(text):
e3.insert(0, text)
return
def asimetrikRSAanahtarOlustur():
key = Fernet.generate_key()
# ◘write the simetrik key to a file
k = open('symmetric.key', 'wb')
k.write(key)
k.close()
# private ve public key oluştur
# burda new keys de bir e.get şeysi yap.
b = int(e1.get())
(pubkey, privkey) = rsa.newkeys(b)
print("%s boyutlu bir anahtar oluşturulacaktır" % b)
# bu sayiyi değiştirerek yapabiliriz sanki gibi.
# public keyi dosyaya yazma
pukey = open('publickey.key', 'wb')
pukey.write(pubkey.save_pkcs1('PEM'))
pukey.close()
# özel keyi dosyaya yazma
prkey = open('privkey.key', 'wb')
prkey.write(privkey.save_pkcs1('PEM'))
prkey.close()
print(privkey)
print(pubkey)
message = b'ornekolsundiyeyazdim'
crypto = rsa.encrypt(message, pubkey)
print(crypto)
decrypt = rsa.decrypt(crypto, privkey)
print(decrypt.decode())
def asimetrikRSAsifrele():
dosya = filedialog.askopenfile(parent=master, mode='rb', title='Şifrelenecek dosyayı seçiniz')
if dosya: # file seçilmişse
data = dosya.read()
dosya.close()
a = os.path.basename(dosya.name)
##buraya kadar dosyayı seçme işlemleri.
print(dosya.name)
#mesela C:/Users/Emre/PycharmProjects/pythonProject/testvideo.mkv dır bu.
print(a)
#bu ise testvideo.mkv dir.
# unpacking the tuple
# simetrik key dosyasını aç
skey = open('symmetric.key', 'rb')
key = skey.read()
print("Şifrelenecek dosya ismi: %s" % (a))
# cipher oluştur
cipher = Fernet(key)
# open file for şifreleme
myfile = open(dosya.name, 'rb')
myfiledata = myfile.read()
# veriyi şifrele
encrypted_data = cipher.encrypt(myfiledata)
dosya_uzantisi = os.path.splitext(a)
print(dosya_uzantisi[0])
b = dosya_uzantisi[0]+"şifrelenmiş"+dosya_uzantisi[1]
print(b)
edata = open(b, 'wb')
edata.write(encrypted_data)
# open the public key file
pkey = open('publickey.key', 'rb')
pkdata = pkey.read()
# load the file
pubkey = rsa.PublicKey.load_pkcs1(pkdata)
# encrpy the simetrik key file with the public key
encrypted_key = rsa.encrypt(key, pubkey)
# write the encrypted simetrik key to a file
ekey = open('encrypted_key', 'wb')
ekey.write(encrypted_key)
def asimetrikRSAsifreCoz():
dosya = filedialog.askopenfile(parent=master, mode='rb', title='Choose a file')
if dosya: # file seçilmişse
data = dosya.read()
dosya.close()
a = os.path.basename(dosya.name)
print(a + " dosyasının şifresi çözülecektir")
prkey = open('privkey.key', 'rb')
pkey = prkey.read()
private_key = rsa.PrivateKey.load_pkcs1(pkey)
e = open('encrypted_key', 'rb')
ekey = e.read()
dpubkey = rsa.decrypt(ekey, private_key)
cipher = Fernet(dpubkey)
encrypted_data = open(dosya.name, 'rb')
edata = encrypted_data.read()
decrypted_data = cipher.decrypt(edata)
dosya_uzantisi = os.path.splitext(a)
print(dosya_uzantisi[0])
b = dosya_uzantisi[0] + "kırılmış" + dosya_uzantisi[1]
edata = open(b, 'wb')
edata.write(decrypted_data)
def simetrikSHA256anahtarOlustur():
password_provided = "password"
password = password_provided.encode()
b = int(e3.get())
a = (os.urandom(b))
print(a)
salt = a
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
file = open('key.key', 'wb')
key = base64.urlsafe_b64encode(kdf.derive(password))
file.write(key)
file.close()
def simetrikSHA256Sifrele():
dosya = filedialog.askopenfile(parent=master, mode='rb', title='Choose a file')
if dosya: # file seçilmişse
data = dosya.read()
dosya.close()
a = os.path.basename(dosya.name)
##buraya kadar dosyayı seçme işlemleri.
print(dosya.name)
print(a)
file = open('key.key', 'rb')
key = file.read()
file.close()
print("Şifrelenecek dosya ismi: %s" % (a))
str1 = str(a)
print(str1)
str2 = '.en'
str3 = str1 + str2
#bu üstteki 3 satır da kritik olmayan kodlar.
print("Şifrelenmiş dosya ismi %s olacaktır" % (str3))
# şifrelenecek dosyayı aç
with open(dosya.name, 'rb') as f:
data = f.read()
fernet = Fernet(key)
encrypted = fernet.encrypt(data)
dosya_uzantisi = os.path.splitext(a)
print(dosya_uzantisi[0])
b = dosya_uzantisi[0] + "şifrelenmiş" + dosya_uzantisi[1]
with open(b, 'wb') as f:
f.write(encrypted)
#bu kod kısmen önemsiz. İhmal edilebilir.
set_textenSimetrik(b)
# Burada, bu alana, test.txt.en yazdırması gerekir.
def simetrikSHA256sifreCoz():
dosya = filedialog.askopenfile(parent=master, mode='rb', title='Choose a file')
if dosya: # file seçilmişse
data = dosya.read()
dosya.close()
a = os.path.basename(dosya.name)
print(a + " dosyasının şifresi çözülecektir")
file = open('key.key', 'rb')
key = file.read()
file.close()
str1 = a
str4 = '.de'
str5 = str1 + str4
# şifrelenecek dosyayı aç
with open(dosya.name, 'rb') as f:
data = f.read()
fernet = Fernet(key)
encrypted = fernet.decrypt(data)
# Write the encrypted file
dosya_uzantisi = os.path.splitext(a)
print(dosya_uzantisi[0])
b = dosya_uzantisi[0] + "kırılmış" + dosya_uzantisi[1]
set_textenSimetrik(b)
with open(b, 'wb') as f:
f.write(encrypted)
master = tk.Tk()
master.maxsize(3000, 3000)
tk.Label(master,
text="Asimetrik RSA anahtar boyutu").grid(row=5, column=0)
tk.Label(master,
text="Yeni dosya ismi").grid(row=6,column=0)
# tk.Text(master, "Burası bir text alanıdıııııııııııııııııııır"),
e1 = tk.Entry(master, width=10)
e2 = tk.Entry(master, width=10)
e3 = tk.Entry(master,width=10)
e4 = tk.Entry(master,width=10)
e1.grid(row=5, column=1)
e2.grid(row=6, column=1)
e3.grid(row=5, column=74)
e4.grid(row=6, column=74)
tk.Label(master,
text="Burası asimetrik şifreleme alanı").grid(row=0, column=0)
tk.Button(master,
text='Asimetrik RSA Key oluştur',
command=asimetrikRSAanahtarOlustur).grid(row=5,
column=2,
sticky=tk.W,
pady=4)
tk.Button(master,
text='Şifrele:dosya seç', command=asimetrikRSAsifrele).grid(row=8,
column=1,
sticky=tk.W,
pady=4)
tk.Button(master,
text='Şifre çöz:dosya seç', command=asimetrikRSAsifreCoz).grid(row=8,
column=2,
sticky=tk.W,
pady=4,)
tk.Label(master,
text="Burası simetrik şifreleme alanı").grid(row=0, column=74)
tk.Button(master,
text='Simetrik Key oluştur',
command=simetrikSHA256anahtarOlustur).grid(row=5,
column=75,
pady=4)
tk.Button(master,
text='Şifrele:dosya seç', command=simetrikSHA256Sifrele).grid(row=8,
column=74,
pady=4)
tk.Button(master,
text='Şifre çöz:dosya seç', command=simetrikSHA256sifreCoz).grid(row=8,
column=75,
pady=4,)
tk.Label(master,
text="Simetrik Anahtar boyutu").grid(row=5, column=73)
tk.Label(master,
text="Yeni dosyanın ismi").grid(row=6,column=73)
tk.mainloop()