# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'enc_dec.ui'
#
# Created by: PyQt5 UI code generator 5.15.5
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
from tkinter import Tk     # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askopenfilename

from PyQt5 import QtCore, QtGui, QtWidgets

import random
import getpass


class Enc():
    def __init__(self,data):
        try:
            print("----------------Stating Encription--------------\n")
            self.data = data
            self.username = getpass.getuser()
            self.key()
            self.encryption()
            self.saving_encryption()
        except Exception as e:
            print(e)
        
    def key(self):
        self.data_len = len(self.data)
        self.keys=""

        for i in range(self.data_len):
            x = random.randint(0,4)
            self.keys = self.keys+str(x)

        #print(self.keys)

    def encryption(self):
        self.enc_data = ""
        for i in range(self.data_len):
            ascii_code = ord(self.data[i])
            ascii_code = ascii_code+int(self.keys[i])
            self.enc_data = self.enc_data+chr(ascii_code)

        print(self.enc_data)

    def saving_encryption(self):
        
        #file = open("encrypted data.txt","w")
        file = open(f"C:\\Users\\{self.username}\\Desktop\\encrypted data.txt","w")
        file.write(self.enc_data)
        file.close

        #file_2 = open("key_1.txt","w")
        file_2 = open(f"C:\\Users\\{self.username}\\Desktop\\key_1.txt","w")
        file_2.write(str(self.keys))
        print("\n-------file encrypted and SAVED------")

"""------------------------------------------------------------------------------"""

class Dec:
    def decmain(self,Encripted_file_path,key_file_path):
        try:
            print("\n------strating decription------\n\n")
            self.Encripted_file_path = Encripted_file_path
            self.key_file_path = key_file_path
            self.reading_data()
            y = self.decription()
            return y
        except Exception as e:
            print("xxxxxx-------ERROR-------xxxxxxx\nMaybe key and data dosenot match or\n",e)

    def reading_data(self):
        file = open(self.Encripted_file_path,"r")
        self.enc_data = file.read()
        file.close()

        file_2 = open(self.key_file_path,"r")
        self.keys = file_2.read()
        file_2.close()
        

        #print(self.enc_data,self.keys)

    def decription(self):
        enc_data_len = len(self.enc_data)
        key_len = len(self.keys)
        self.dec_data = ""

        for i in range(enc_data_len):
            ascii_code = ord(self.enc_data[i])
            ascii_code = ascii_code-int(self.keys[i])
            self.dec_data = self.dec_data+chr(ascii_code)
            #print(self.dec_data)
        
        
        print(self.dec_data)
        x = self.dec_data
        return  x

"""-----------------------------------------------------------------"""


#username = getpass.getuser()
#print(username)

"""---------------ENCRIPTION-------------------------"""

#data="This work has been selected by scholars as being culturally important, and is part of the knowledge base of civilization as we know it. This work was reproduced from the original artifact, and remains as true to the original work as possible."
#Enc(data,username)


"""---------------DECRIPTON--------------------------"""

#Encripted_file_path = "encrypted data.txt"
#Encripted_file_path = f"C:\\Users\\{username}\\Desktop\\encrypted data.txt"
#key_file_path = "key_1.txt"
#key_file_path = f"C:\\Users\\{username}\\Desktop\\key_1.txt"


#Dec(Encripted_file_path,key_file_path)

"""-----------------------------------------------------------------------------------------"""
"""-----------------------------------------------------------------------------------------"""
"""-----------------------------------------------------------------------------------------"""
"""-----------------------------------------------------------------------------------------"""
"""-----------------------------------------------------------------------------------------"""
"""-----------------------------------------------------------------------------------------"""
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(917, 649)
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet("")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(30, 10, 421, 581))
        self.frame.setStyleSheet("background-color: rgb(255, 243, 237);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")


        self.encrypt_button = QtWidgets.QPushButton(self.frame,clicked = lambda:self.enc_click())
        self.encrypt_button.setGeometry(QtCore.QRect(120, 430, 171, 41))
        self.encrypt_button.setStyleSheet("background-color: rgb(217, 221, 242);")
        self.encrypt_button.setObjectName("encrypt_button")


        self.enc_data_in = QtWidgets.QLineEdit(self.frame)
        self.enc_data_in.setGeometry(QtCore.QRect(40, 140, 331, 151))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.enc_data_in.setFont(font)
        self.enc_data_in.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.enc_data_in.setObjectName("enc_data_in")
        self.enc_data_in.setText("")



        self.frame_2 = QtWidgets.QFrame(self.centralwidget)
        self.frame_2.setGeometry(QtCore.QRect(470, 10, 411, 581))
        self.frame_2.setStyleSheet("background-color: rgb(229, 233, 255);")
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")

        self.file_path_button = QtWidgets.QPushButton(self.frame_2,clicked=lambda: self.filepath_click())
        self.file_path_button.setGeometry(QtCore.QRect(10, 120, 93, 31))
        self.file_path_button.setStyleSheet("background-color: rgb(255, 243, 237);")
        self.file_path_button.setObjectName("file_path_button")



        self.key_path_button = QtWidgets.QPushButton(self.frame_2,clicked = lambda: self.keypath_click())
        self.key_path_button.setGeometry(QtCore.QRect(10, 180, 93, 31))
        self.key_path_button.setStyleSheet("background-color: rgb(255, 243, 237);")
        self.key_path_button.setObjectName("key_path_button")



        self.decrypt_button = QtWidgets.QPushButton(self.frame_2,clicked = lambda: self.dec_click())
        self.decrypt_button.setGeometry(QtCore.QRect(90, 250, 211, 41))
        self.decrypt_button.setStyleSheet("background-color: rgb(255, 243, 237);")
        self.decrypt_button.setObjectName("decrypt_button")



        self.filepath_in = QtWidgets.QLineEdit(self.frame_2)
        self.filepath_in.setGeometry(QtCore.QRect(100, 120, 291, 31))
        self.filepath_in.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.filepath_in.setObjectName("filepath_in")



        self.keypath_in = QtWidgets.QLineEdit(self.frame_2)
        self.keypath_in.setGeometry(QtCore.QRect(100, 180, 291, 31))
        self.keypath_in.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.keypath_in.setObjectName("keypath_in")



        self.textEdit = QtWidgets.QTextEdit(self.frame_2)
        self.textEdit.setGeometry(QtCore.QRect(20, 310, 361, 251))
        self.textEdit.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.textEdit.setObjectName("textEdit")



        self.line = QtWidgets.QFrame(self.centralwidget)
        self.line.setGeometry(QtCore.QRect(450, -20, 20, 681))
        self.line.setFrameShape(QtWidgets.QFrame.VLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(290, 0, 171, 72))
        font = QtGui.QFont()
        font.setPointSize(36)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setAutoFillBackground(True)
        self.label.setStyleSheet("color: rgb(209, 212, 233);\n"
"color: rgb(0, 0, 0);\n"
"")
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(460, 0, 201, 72))
        font = QtGui.QFont()
        font.setPointSize(36)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label_2.setAutoFillBackground(False)
        self.label_2.setStyleSheet("color: rgb(0, 0, 0);\n"
"\n"
"\n"
"")
        self.label_2.setObjectName("label_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def enc_click(self):
        enc_data = self.enc_data_in.text()
        Enc(enc_data)
        self.enc_data_in.setText("data&key saved on your desktop")

    def filepath_click(self):
        Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
        self.filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
        print(self.filename)
        self.filepath_in.setText(self.filename)
        

    def keypath_click(self):
        Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
        self.keypath = askopenfilename() # show an "Open" dialog box and return the path to the selected file
        print(self.keypath)
        self.keypath_in.setText(self.keypath)
        

    def dec_click(self):
        d = Dec()
        x = d.decmain(self.filename,self.keypath)
        #print(x)
        self.textEdit.setText(x)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Encryption & Decryption"))
        self.encrypt_button.setText(_translate("MainWindow", "ENCRYPT DATA"))
        self.file_path_button.setText(_translate("MainWindow", "File Path"))
        self.key_path_button.setText(_translate("MainWindow", "Key Path"))
        self.decrypt_button.setText(_translate("MainWindow", "DECRYPT DATA"))
        self.label.setText(_translate("MainWindow", "PASS"))
        self.label_2.setText(_translate("MainWindow", "^@&#"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
