<gh_stars>0
#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 6.2
#  in conjunction with Tcl version 8.6
#    Dec 11, 2021 10:47:35 AM PKT  platform: Windows NT

from os import name
import hashlib
import sys
from PIL import Image
from tkinter import Checkbutton, Scrollbar, StringVar, Text, filedialog, messagebox
from PIL import Image, ImageTk
from tkinter.constants import ANCHOR, END, NSEW, NW, RIGHT, S, TOP
import aes
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True


def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root, returnMsg, top, enableAES
    root = tk.Tk()
    enableAES = tk.IntVar()
    top = Toplevel1 (root)
    root.mainloop()
    temp = returnMsg
    returnMsg = None
    return temp

w = None

def destroy_Toplevel1():
    global w
    w.destroy()
    w = None

class Toplevel1:
    def __init__(self, top=None):
        global enableAES
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

        top.geometry("600x240+468+138")
        top.minsize(120, 1)
        top.maxsize(1540, 845)
        top.resizable(0,  0)
        top.title("Text In Image (Decode)")
        top.configure(background="#d9d9d9")

        self.menubar = tk.Menu(top,font="TkMenuFont",bg=_bgcolor,fg=_fgcolor)
        top.configure(menu = self.menubar)

        self.import_button_label = ttk.Button(top, command=openFile)
        self.import_button_label.place(relx=0.110, rely=0.022, height=35
                                       , width=510)
        self.import_button_label.configure(takefocus="")
        self.import_button_label.configure(text='''Import''')
        self.import_button_label.configure(cursor="hand2")

        self.back_button_label = ttk.Button(top, command=lambda: destroy("back"))
        self.back_button_label.place(relx=0.033, rely=0.022, height=35
                                       , width=40)
        self.back_button_label.configure(takefocus="")
        self.back_button_label.configure(text='''Back''')
        self.back_button_label.configure(cursor="hand2")

        self.decode_button = ttk.Button(top, command=DecodeAndDisplay)
        self.decode_button.place(x=290, y=200, height=35, width=200)
        self.decode_button.configure(takefocus="")
        self.decode_button.configure(text='''Decode''')
        self.decode_button.configure(cursor="hand2")

        self.image_label = ttk.Label(top)
        self.image_label.place(x=10, y=45, height=19, width=45)
        self.image_label.configure(background="#d9d9d9")
        self.image_label.configure(foreground="#000000")
        self.image_label.configure(font="TkDefaultFont")
        self.image_label.configure(relief="flat")
        self.image_label.configure(anchor='w')
        self.image_label.configure(justify='left')
        self.image_label.configure(text='''Image:''')

        self.Label1 = tk.Label(top)
        self.Label1.place(x=10, y=65, height=181, width=264)
        self.Label1.configure(background="#e9e9e9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(relief="groove")

        self.style.map('TCheckbutton',background=
            [('selected', _bgcolor), ('active', _ana2color)])
        self.enable_AES_button = ttk.Checkbutton(top, variable=enableAES, offvalue=0, onvalue=1, command=enableAESF)
        self.enable_AES_button.place(x=290, y=65, relwidth=0.15
                , relheight=0.0, height=21)
        self.enable_AES_button.configure(takefocus="")
        self.enable_AES_button.configure(text='''Enable AES''')

        self.key_label = ttk.Label(top)
        self.key_label.place(x=290, y=95, height=19, width=35)
        self.key_label.configure(background="#d9d9d9")
        self.key_label.configure(foreground="#000000")
        self.key_label.configure(font="TkDefaultFont")
        self.key_label.configure(relief="flat")
        self.key_label.configure(anchor='w')
        self.key_label.configure(justify='left')
        self.key_label.configure(text='''Key:''')

        self.TEntry1 = ttk.Entry(top, state="disabled")
        self.TEntry1.place(x=330, y=93, relheight=0.089, relwidth=0.433)
        self.TEntry1.configure(takefocus="")
        self.TEntry1.configure(cursor="ibeam")

        self.encoded_text_label = ttk.Label(top)
        self.encoded_text_label.place(x=290, y=155, height=19
                , width=60)
        self.encoded_text_label.configure(background="#d9d9d9")
        self.encoded_text_label.configure(foreground="#000000")
        self.encoded_text_label.configure(font="TkDefaultFont")
        self.encoded_text_label.configure(relief="flat")
        self.encoded_text_label.configure(anchor='w')
        self.encoded_text_label.configure(justify='left')
        self.encoded_text_label.configure(text='''Password:''')

        self.input_password = ttk.Entry(top)
        self.input_password.place(x=360, y=150, relheight=0.089, relwidth=0.378)
        self.input_password.configure(takefocus="")
        self.input_password.configure(cursor="ibeam")


class DataDisplay:
    def __init__(self, top=None, str=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

        top.geometry("300x250+468+138")
        top.minsize(120, 1)
        top.maxsize(1540, 845)
        top.resizable(0,  0)
        top.title("Encoded Text")
        top.configure(background="#d9d9d9")

        self.menubar = tk.Menu(top,font="TkMenuFont",bg=_bgcolor,fg=_fgcolor)
        top.configure(menu = self.menubar)

        self.Label2 = tk.Label(top)
        self.Label2.grid(row=0, column=0)

        self.text_label = Text(self.Label2)
        self.text_label.grid(row=0, column=1)
        self.scrollbar = Scrollbar(self.Label2, command=self.text_label.yview)
        self.text_label.config(yscrollcommand=self.scrollbar.set)
        self.scrollbar.grid(row=0,column=0, sticky=NSEW)
        self.text_label.insert(END, str)


def openFile():
    global filepath, top
    filepath = filedialog.askopenfilename()
    global photo
    photo = returnImage(filepath)
    top.Label1.configure(image=photo)


def returnImage(file):
    img = Image.open(file)
    if len(file) < 4:
        return 0
    img = img.resize((265, 220), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(img)
    return img


def decode(deli):
    img = Image.open(filepath)
    height, width = img.size
    temp = ""
    text=""
    
    count = 0
    flag = False

    for i in range(height):
        for j in range(width):
            r, g, b = img.getpixel((i, j))
            temp += str(r % 2)
            temp += str(g % 2)
            temp += str(b % 2)
            if(len(temp)>8):
                temp1 = temp[:8]
                temp = temp[8:]
                text+=chr(int(temp1, 2))
                a = text.find(deli)
                if(a !=-1):# and a == len(text)-len(deli) ):
                    return text

    return text


def enableAESF():
    global enableAES, top
    if(enableAES.get()):
        top.TEntry1.configure(state="enabled")
    else:
        top.TEntry1.configure(state="disabled")


def DecodeAndDisplay():
    global dtext, top, photo, enableAES
    if(photo == None):
        messagebox.showerror("Error", "Please Import Image First")
        return 0
    if len(top.input_password.get()) == 0:
        messagebox.showerror("Error", "Password is not set.")
        return 0
    elif len(top.input_password.get()) < 5:
        messagebox.showerror("Error", "Password length should be greater than 4")
        return 0
    passtemp = hashlib.sha256(top.input_password.get().encode()).hexdigest()
    dtext = decode(passtemp)[:-64]
    if enableAES.get():
        if len(top.TEntry1.get()) == 0:
            messagebox.showerror("Error", "Encryption key is not set.")
            return 0
        elif len(top.TEntry1.get()) < 5:
            messagebox.showwarning("WARNING", "Encryption key should be greater than 5 letters")

        aestemp = aes.AESCipher(top.TEntry1.get())
        dtext = str(aestemp.decrypt(dtext))
    if(hashlib.sha256(dtext[64:].encode()).hexdigest() != dtext[:64]):
        messagebox.showerror("Error", "Data isn't correct")
        return 0
    messagebox.showinfo("Data", dtext[64:])



def destroy(option):
    global root, returnMsg
    returnMsg = option
    root.destroy()

top = None
filepath = None
returnMsg = None
photo = None
dtext = None
enableAES = None

if __name__=="__main__":
    vp_start_gui()
