Skip to content

Commit e995186

Browse files
authored
Add files via upload
1 parent e6536d3 commit e995186

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

data_file.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am plain text file

fd.der

970 Bytes
Binary file not shown.

fd.p12

2.51 KB
Binary file not shown.

gui_dec.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from tkinter import *
2+
import tkinter as tk
3+
from tkinter import filedialog
4+
from tkinter import messagebox
5+
import os, random
6+
from Crypto.Cipher import AES
7+
from Crypto.Hash import SHA256
8+
from OpenSSL import crypto
9+
from Crypto.PublicKey import RSA
10+
from Crypto.Cipher import PKCS1_v1_5
11+
from Crypto.Cipher import PKCS1_OAEP
12+
import zlib
13+
import base64
14+
15+
global icert
16+
global ifile
17+
18+
ifile=""
19+
icert=""
20+
21+
def select_file():
22+
global ifile,T1
23+
ifile = filedialog.askopenfilename()
24+
print("file is '{0}'".format(ifile))
25+
T1.insert(tk.END, ifile)
26+
messagebox.showinfo("File selected", "File selected is '{0}' ".format(ifile))
27+
28+
def select_cert():
29+
global icert,T2
30+
icert = filedialog.askopenfilename()
31+
T2.insert(tk.END, icert)
32+
messagebox.showinfo("File selected", "File selected is '{0}' ".format(icert))
33+
34+
def decrypt():
35+
36+
print("In encrypt method file is '{0}'".format(ifile))
37+
print("In encrypt method cert is '{0}'".format(icert))
38+
39+
# extract pri key to decrypt AES key
40+
p12 = crypto.load_pkcs12(file(icert,"rb").read(),"user1")
41+
pri_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, p12.get_privatekey())
42+
with open("enc_key_file.txt","rb") as ekf:
43+
ef = ekf.read()
44+
ekf.close()
45+
print("\nenc_key_stream is \n")
46+
print(ef)
47+
48+
rsa_pri_key = RSA.importKey(pri_key)
49+
rsa_pri_key = PKCS1_OAEP.new(rsa_pri_key)
50+
dec_key = rsa_pri_key.decrypt(ef)
51+
print("\nAES key is \n")
52+
print(dec_key) # Dec key
53+
# AES key extracted
54+
55+
chunksize = 64*1024
56+
57+
sp = ifile.split("/")
58+
ln = len(sp)
59+
print("len is '{0}'".format(ln))
60+
print(sp[ln-1])
61+
print(sp)
62+
outputFile = "final_"+ sp[ln-1]
63+
64+
# File decryption starts
65+
with open(ifile, 'rb') as infile:
66+
filesize = long(infile.read(16))
67+
IV = infile.read(16)
68+
decryptor = AES.new(dec_key, AES.MODE_CBC, IV)
69+
70+
with open(outputFile, 'wb') as outfile:
71+
while True:
72+
chunk = infile.read(chunksize)
73+
if len(chunk) == 0:
74+
break
75+
outfile.write(decryptor.decrypt(chunk))
76+
outfile.truncate(filesize)
77+
# File decrypted
78+
79+
80+
root = Tk()
81+
root.title("File Decryption")
82+
def close_root_window():
83+
root.destroy()
84+
root.geometry("550x600")
85+
86+
ll = tk.Label(root, text='Input file')
87+
ll.place(x=20,y=30)
88+
l2 = tk.Label(root, text='Input certificate')
89+
l2.place(x=20,y=70)
90+
#global T
91+
92+
#T = tk.Text(root, height=2, width=30)
93+
#T.pack()
94+
#T.insert(tk.END, ifile)
95+
96+
T1 = Entry(root, width=30)
97+
T1.place(x=130,y=30)
98+
#T1.insert(tk.END, ifile)
99+
100+
T2 = Entry(root, width=30)
101+
T2.place(x=130,y=70)
102+
#T2.insert(tk.END, icert)
103+
104+
b1 = Button(root, text="SELECT FILE",fg="Red",font="Times", command=select_file)
105+
b1.place(x=420,y=23)
106+
107+
b1 = Button(root, text="SELECT KEY",fg="Red",font="Times", command=select_cert)
108+
b1.place(x=420,y=63)
109+
110+
b2 = Button(root, text="Decrypt",fg="green",font="Verdana 15 bold", command= decrypt)
111+
b2.place(x=100,y=100)
112+
113+
b3 = Button(root, text="Reset",fg="green",font="Verdana 15 bold", command= "")
114+
b3.place(x=220,y=100)
115+
116+
b4 = Button(root, text="Close",fg="green",font="Helvetica 15 bold", command=close_root_window)
117+
b4.place(x=320,y=100)
118+
119+
root.mainloop()
120+

gui_enc.py

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
2+
from tkinter import *
3+
import tkinter as tk
4+
from tkinter import filedialog
5+
from tkinter import messagebox
6+
import os, random
7+
from Crypto.Cipher import AES
8+
from Crypto.Hash import SHA256
9+
from OpenSSL import crypto
10+
from Crypto.PublicKey import RSA
11+
from Crypto.Cipher import PKCS1_v1_5
12+
from Crypto.Cipher import PKCS1_OAEP
13+
import zlib
14+
import base64
15+
16+
global icert
17+
global ifile
18+
19+
ifile=""
20+
icert=""
21+
22+
def select_file():
23+
global ifile,T1
24+
ifile = filedialog.askopenfilename()
25+
print("file is '{0}'".format(ifile))
26+
T1.insert(tk.END, ifile)
27+
messagebox.showinfo("File selected", "File selected is '{0}' ".format(ifile))
28+
29+
def select_cert():
30+
global icert,T2
31+
icert = filedialog.askopenfilename()
32+
T2.insert(tk.END, icert)
33+
messagebox.showinfo("File selected", "File selected is '{0}' ".format(icert))
34+
35+
def getKey(password):
36+
hasher = SHA256.new(password)
37+
return hasher.digest()
38+
39+
def encrypt():
40+
41+
print("In encrypt method file is '{0}'".format(ifile))
42+
print("In encrypt method cert is '{0}'".format(icert))
43+
44+
filename = ifile
45+
password = os.urandom(32)
46+
47+
# store plain AES key(SHA_256 hash of randomly generated 256 bit no) in key_file
48+
49+
with open("key_file.txt","w") as kf:
50+
password = str(password)
51+
kf.write(password)
52+
kf.close()
53+
54+
key = getKey(password)
55+
56+
chunksize = 64*1024
57+
sp = ifile.split("/")
58+
ln = len(sp)
59+
print("len is '{0}'".format(ln))
60+
print(sp[ln-1])
61+
print(sp)
62+
outputFile = "enc_"+ sp[ln-1]
63+
filesize = str(os.path.getsize(filename)).zfill(16)
64+
IV = ''
65+
66+
for i in range(16):
67+
68+
IV += chr(random.randint(0, 0xFF))
69+
encryptor = AES.new(key, AES.MODE_CBC, IV)
70+
71+
with open(filename, 'rb') as infile:
72+
with open(outputFile, 'wb') as outfile:
73+
outfile.write(filesize)
74+
outfile.write(IV)
75+
while True:
76+
chunk = infile.read(chunksize)
77+
if len(chunk) == 0:
78+
break
79+
elif len(chunk) % 16 != 0:
80+
chunk += ' ' * (16 - (len(chunk) % 16))
81+
outfile.write(encryptor.encrypt(chunk))
82+
83+
# Key text encryption using RSA pub key and store in text file
84+
cert = crypto.load_certificate(crypto.FILETYPE_ASN1, open(icert,"rb").read())
85+
print(crypto.dump_publickey(crypto.FILETYPE_PEM,cert.get_pubkey()))
86+
pk = crypto.dump_publickey(crypto.FILETYPE_PEM,cert.get_pubkey())
87+
88+
# RSA 2048 bit public key extracted from .der
89+
pub_key = RSA.importKey(pk)
90+
pub_key = PKCS1_OAEP.new(pub_key)
91+
enc_text = pub_key.encrypt(key)
92+
print("enc text of key stream is\n")
93+
print(enc_text)
94+
95+
# enc key in enc_key_file
96+
97+
with open("enc_key_file.txt","w") as ekf:
98+
ekf.write(enc_text)
99+
ekf.close()
100+
101+
def reset():
102+
global ifile,T
103+
#var = T.get('1.0',tk.END).replace(ifile,' ')
104+
#T.replace('1.0',tk.END,var)
105+
106+
root = Tk()
107+
root.title("File Encryption")
108+
def close_root_window():
109+
root.destroy()
110+
root.geometry("550x600")
111+
112+
ll = tk.Label(root, text='Input file')
113+
ll.place(x=20,y=30)
114+
l2 = tk.Label(root, text='Input certificate')
115+
l2.place(x=20,y=70)
116+
#global T
117+
118+
#T = tk.Text(root, height=2, width=30)
119+
#T.pack()
120+
#T.insert(tk.END, ifile)
121+
122+
T1 = Entry(root, width=30)
123+
T1.place(x=130,y=30)
124+
T1.insert(tk.END, ifile)
125+
126+
T2 = Entry(root, width=30)
127+
T2.place(x=130,y=70)
128+
T2.insert(tk.END, icert)
129+
130+
b1 = Button(root, text="SELECT FILE",fg="Red",font="Times", command=select_file)
131+
b1.place(x=420,y=23)
132+
133+
b1 = Button(root, text="SELECT KEY",fg="Red",font="Times", command=select_cert)
134+
b1.place(x=420,y=63)
135+
136+
b2 = Button(root, text="Encrypt",fg="green",font="Verdana 15 bold", command= encrypt)
137+
b2.place(x=100,y=100)
138+
139+
b3 = Button(root, text="Reset",fg="green",font="Verdana 15 bold", command= reset)
140+
b3.place(x=220,y=100)
141+
142+
b4 = Button(root, text="Close",fg="green",font="Helvetica 15 bold", command=close_root_window)
143+
b4.place(x=320,y=100)
144+
145+
root.mainloop()
146+

0 commit comments

Comments
 (0)