|
| 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