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

# Form implementation generated from reading ui file 'rsa.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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.

import decimal
from PyQt5 import QtCore, QtGui, QtWidgets

inp_val_p = 0
inp_val_q = 0
inp_val_e = 0
n = 0
k = 0
Фn = 0
d = 0
clousText = ""
clousTextList = []
clousTextCharList = []
openText = ""
openTextList = []
openTextCharList = []


class Ui_RSA(object):
    def setupUi(self, RSA):
        RSA.setObjectName("RSA")
        RSA.resize(750, 478)
        RSA.setMinimumSize(QtCore.QSize(750, 478))
        RSA.setMaximumSize(QtCore.QSize(750, 478))
        RSA.setStyleSheet("background: #323450;")
        self.centralwidget = QtWidgets.QWidget(RSA)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(0, 458, 750, 20))
        self.frame.setStyleSheet("background-color: rgb(212, 80, 54);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.consolLable = QtWidgets.QLabel(self.frame)
        self.consolLable.setGeometry(QtCore.QRect(9, 0, 450, 20))
        self.consolLable.setStyleSheet("border: 0;\n"
                                       "color: rgb(52, 52, 52);")
        self.consolLable.setObjectName("consolLable")

        self.val_p = QtWidgets.QLabel(self.centralwidget)
        self.val_p.setGeometry(QtCore.QRect(42, 60, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.val_p.setFont(font)
        self.val_p.setStyleSheet("color: rgb(153, 153, 153);\n"
                                 "background-color: rgb(33, 34, 53);")
        self.val_p.setObjectName("val_p")
        self.inp_val_p = QtWidgets.QLineEdit(self.centralwidget)
        self.inp_val_p.setGeometry(QtCore.QRect(71, 59, 61, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.inp_val_p.setFont(font)
        self.inp_val_p.setStyleSheet("color: rgb(36, 36, 36);\n"
                                     "background: #fff;")
        self.inp_val_p.setObjectName("inp_val_p")
        self.inp_val_q = QtWidgets.QLineEdit(self.centralwidget)
        self.inp_val_q.setGeometry(QtCore.QRect(72, 100, 61, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.inp_val_q.setFont(font)
        self.inp_val_q.setStyleSheet("color: rgb(36, 36, 36);\n"
                                     "background: #fff;")
        self.inp_val_q.setObjectName("inp_val_q")
        self.val_q = QtWidgets.QLabel(self.centralwidget)
        self.val_q.setGeometry(QtCore.QRect(42, 100, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.val_q.setFont(font)
        self.val_q.setStyleSheet("color: rgb(153, 153, 153);\n"
                                 "background-color: rgb(33, 34, 53);")
        self.val_q.setObjectName("val_q")
        self.frame_2 = QtWidgets.QFrame(self.centralwidget)
        self.frame_2.setGeometry(QtCore.QRect(20, 40, 181, 331))
        self.frame_2.setStyleSheet("background-color: rgb(33, 34, 53);")
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.openText = QtWidgets.QTextEdit(self.centralwidget)
        self.openText.setGeometry(QtCore.QRect(215, 40, 241, 331))
        self.openText.setStyleSheet("background-color: rgb(33, 34, 53);\n"
                                    "color: rgb(153, 153, 153);")
        self.openText.setObjectName("openText")
        self.parametors_t = QtWidgets.QLabel(self.centralwidget)
        self.parametors_t.setGeometry(QtCore.QRect(20, 16, 81, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.parametors_t.setFont(font)
        self.parametors_t.setStyleSheet("color: rgb(153, 153, 153);")
        self.parametors_t.setObjectName("parametors_t")
        self.open_t = QtWidgets.QLabel(self.centralwidget)
        self.open_t.setGeometry(QtCore.QRect(215, 16, 101, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.open_t.setFont(font)
        self.open_t.setStyleSheet("color: rgb(153, 153, 153);")
        self.open_t.setObjectName("open_t")
        self.val_fn = QtWidgets.QLabel(self.centralwidget)
        self.val_fn.setGeometry(QtCore.QRect(42, 150, 41, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.val_fn.setFont(font)
        self.val_fn.setStyleSheet("color: rgb(153, 153, 153);\n"
                                  "background-color: rgb(33, 34, 53);")
        self.val_fn.setObjectName("val_fn")
        self.val_fn_number = QtWidgets.QLabel(self.centralwidget)
        self.val_fn_number.setGeometry(QtCore.QRect(90, 150, 71, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.val_fn_number.setFont(font)
        self.val_fn_number.setStyleSheet("color: rgb(153, 153, 153);\n"
                                         "background-color: rgb(33, 34, 53);")
        self.val_fn_number.setObjectName("val_fn_number")
        self.pnq_button = QtWidgets.QPushButton(self.centralwidget)
        self.pnq_button.setGeometry(QtCore.QRect(140, 340, 51, 23))
        self.pnq_button.setStyleSheet("color: rgb(153, 153, 153);")
        self.pnq_button.setObjectName("pnq_button")
        self.pnq_button.clicked.connect(self.cliclConsoleRSA)
        self.val_n = QtWidgets.QLabel(self.centralwidget)
        self.val_n.setGeometry(QtCore.QRect(42, 180, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.val_n.setFont(font)
        self.val_n.setStyleSheet("color: rgb(153, 153, 153);\n"
                                 "background-color: rgb(33, 34, 53);")
        self.val_n.setObjectName("val_n")
        self.val_n_number = QtWidgets.QLabel(self.centralwidget)
        self.val_n_number.setGeometry(QtCore.QRect(68, 180, 71, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.val_n_number.setFont(font)
        self.val_n_number.setStyleSheet("color: rgb(153, 153, 153);\n"
                                        "background-color: rgb(33, 34, 53);")
        self.val_n_number.setObjectName("val_n_number")
        self.val_e = QtWidgets.QLabel(self.centralwidget)
        self.val_e.setGeometry(QtCore.QRect(42, 240, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.val_e.setFont(font)
        self.val_e.setStyleSheet("color: rgb(153, 153, 153);\n"
                                 "background-color: rgb(33, 34, 53);")
        self.val_e.setObjectName("val_e")
        self.inp_val_e = QtWidgets.QLineEdit(self.centralwidget)
        self.inp_val_e.setGeometry(QtCore.QRect(72, 238, 61, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.inp_val_e.setFont(font)
        self.inp_val_e.setStyleSheet("color: rgb(36, 36, 36);\n"
                                     "background: #fff;")
        self.inp_val_e.setObjectName("inp_val_e")
        self.np_e = QtWidgets.QLabel(self.centralwidget)
        self.np_e.setGeometry(QtCore.QRect(40, 270, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.np_e.setFont(font)
        self.np_e.setStyleSheet("color: rgb(153, 153, 153);\n"
                                "background-color: rgb(33, 34, 53);")
        self.np_e.setObjectName("np_e")
        self.np_e_number = QtWidgets.QLabel(self.centralwidget)
        self.np_e_number.setGeometry(QtCore.QRect(70, 270, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.np_e_number.setFont(font)
        self.np_e_number.setStyleSheet("color: rgb(153, 153, 153);\n"
                                       "background-color: rgb(33, 34, 53);")
        self.np_e_number.setObjectName("np_e_number")
        self.np_k = QtWidgets.QLabel(self.centralwidget)
        self.np_k.setGeometry(QtCore.QRect(40, 300, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.np_k.setFont(font)
        self.np_k.setStyleSheet("color: rgb(153, 153, 153);\n"
                                "background-color: rgb(33, 34, 53);")
        self.np_k.setObjectName("np_k")
        self.np_k_number = QtWidgets.QLabel(self.centralwidget)
        self.np_k_number.setGeometry(QtCore.QRect(70, 300, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.np_k_number.setFont(font)
        self.np_k_number.setStyleSheet("color: rgb(153, 153, 153);\n"
                                       "background-color: rgb(33, 34, 53);")
        self.np_k_number.setObjectName("np_k_number")
        self.np_d = QtWidgets.QLabel(self.centralwidget)
        self.np_d.setGeometry(QtCore.QRect(40, 330, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.np_d.setFont(font)
        self.np_d.setStyleSheet("color: rgb(153, 153, 153);\n"
                                "background-color: rgb(33, 34, 53);")
        self.np_d.setObjectName("np_d")
        self.np_d_number = QtWidgets.QLabel(self.centralwidget)
        self.np_d_number.setGeometry(QtCore.QRect(70, 330, 21, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.np_d_number.setFont(font)
        self.np_d_number.setStyleSheet("color: rgb(153, 153, 153);\n"
                                       "background-color: rgb(33, 34, 53);")
        self.np_d_number.setObjectName("np_d_number")
        self.clousText = QtWidgets.QTextEdit(self.centralwidget)
        self.clousText.setGeometry(QtCore.QRect(470, 40, 241, 331))
        self.clousText.setStyleSheet("background-color: rgb(33, 34, 53);\n"
                                     "color: rgb(153, 153, 153);")
        self.clousText.setObjectName("clousText")
        self.clous_t = QtWidgets.QLabel(self.centralwidget)
        self.clous_t.setGeometry(QtCore.QRect(470, 16, 111, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.clous_t.setFont(font)
        self.clous_t.setStyleSheet("color: rgb(153, 153, 153);")
        self.clous_t.setObjectName("clous_t")
        self.shifrButton = QtWidgets.QPushButton(self.centralwidget)
        self.shifrButton.setGeometry(QtCore.QRect(364, 380, 91, 23))
        self.shifrButton.setStyleSheet("color: rgb(153, 153, 153);\n"
                                       "background-color: rgb(33, 34, 53);")
        self.shifrButton.setObjectName("shifrButton")
        self.shifrButton.clicked.connect(self.shifr)
        self.deshifrButton = QtWidgets.QPushButton(self.centralwidget)
        self.deshifrButton.setGeometry(QtCore.QRect(620, 380, 91, 23))
        self.deshifrButton.setStyleSheet("color: rgb(153, 153, 153);\n"
                                         "background-color: rgb(33, 34, 53);")
        self.deshifrButton.setObjectName("deshifrButton")
        self.deshifrButton.clicked.connect(self.deshifr)
        self.frame_2.raise_()
        self.frame.raise_()
        self.val_p.raise_()
        self.inp_val_q.raise_()
        self.val_q.raise_()
        self.openText.raise_()
        self.parametors_t.raise_()
        self.open_t.raise_()
        self.val_fn.raise_()
        self.val_fn_number.raise_()
        self.pnq_button.raise_()
        self.inp_val_p.raise_()
        self.val_n.raise_()
        self.val_n_number.raise_()
        self.val_e.raise_()
        self.inp_val_e.raise_()
        self.np_e.raise_()
        self.np_e_number.raise_()
        self.np_k.raise_()
        self.np_k_number.raise_()
        self.np_d.raise_()
        self.np_d_number.raise_()
        self.clousText.raise_()
        self.clous_t.raise_()
        self.shifrButton.raise_()
        self.deshifrButton.raise_()
        RSA.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, RSA):
        _translate = QtCore.QCoreApplication.translate
        RSA.setWindowTitle(_translate("RSA", "RSA"))
        self.consolLable.setText(_translate("RSA", "RSA"))
        self.val_p.setText(_translate("RSA", "p = "))
        self.val_q.setText(_translate("RSA", "q = "))
        self.parametors_t.setText(_translate("RSA", "Параметры"))
        self.open_t.setText(_translate("RSA", "Открытый Текст"))
        self.val_fn.setText(_translate("RSA", "Ф(n) = "))
        self.val_fn_number.setText(_translate("RSA", "0"))
        self.pnq_button.setText(_translate("RSA", "OK"))
        self.val_n.setText(_translate("RSA", "n = "))
        self.val_n_number.setText(_translate("RSA", "0"))
        self.val_e.setText(_translate("RSA", "e = "))
        self.np_e.setText(_translate("RSA", ""))
        self.np_e_number.setText(_translate("RSA", ""))
        self.np_k.setText(_translate("RSA", "k ="))
        self.np_k_number.setText(_translate("RSA", "0"))
        self.np_d.setText(_translate("RSA", "d ="))
        self.np_d_number.setText(_translate("RSA", "0"))
        self.clous_t.setText(_translate("RSA", "Закрытый Текст"))
        self.shifrButton.setText(_translate("RSA", "Шифровать"))
        self.deshifrButton.setText(_translate("RSA", "Дешифровать"))

    def cliclChek(self):
        result = False
        if self.inp_val_p.text().isdigit():
            result = True
            if self.inp_val_q.text().isdigit():
                result = True
                if self.inp_val_e.text().isdigit():
                    result = True
                else:
                    result = False
            else:
                self.consolLable.setText(f"Значение q = {self.inp_val_q.text()} не является целым числом")
                result = False
        else:
            self.consolLable.setText(f"Значение p = {self.inp_val_p.text()} не является целым числом")
            result = False
        return result

    def cliclConsoleRSA(self):
        self.consolLable.setText(f"Проверка")
        if self.cliclChek():
            global inp_val_p
            global inp_val_q
            global inp_val_e
            global n
            global k
            global Фn
            global d
            inp_val_p = int(self.inp_val_p.text())
            inp_val_q = int(self.inp_val_q.text())
            inp_val_e = int(self.inp_val_e.text())
            n = inp_val_p * inp_val_q
            k = 0
            Фn = (inp_val_p - 1) * (inp_val_q - 1)
            self.val_n_number.setText(str(n))
            self.val_fn_number.setText(str(Фn))
            for i in range(1, 1000):
                d = (i * Фn + 1) / inp_val_e
                if d % 1 == 0:
                    self.consolLable.setText(f"Успешно d = {int(d)} Можете продолжать")
                    k = i
                    self.np_k_number.setText(str(int(k)))
                    self.np_d_number.setText(str(int(d)))
                    break
                elif i == 999:
                    self.consolLable.setText(f"Значение k Не было найдено поменяйте p, q, e")

    def shifr(self):
        self.clousText.setText("")
        global clousText
        global openText
        global openTextList
        global clousTextList
        global inp_val_e
        global n
        global clousTextCharList
        clousText = ""
        openText = self.openText.toPlainText()
        for i in openText:
            openTextList.append(ord(i))
            print("openTextList ", openTextList)
        for j in openTextList:
            clousTextList.append(str(((int(j) ** inp_val_e) % n)))
            print("clousTextList ", clousTextList)
        for w in clousTextList:
            clousTextCharList.append(chr(w))
            print("clousTextCharList ", clousTextCharList)

        clousTextList.append(str(((int(openText) ** inp_val_e) % n)))
        # clousText = "".join(clousTextCharList)
        clousText = "".join(clousTextList)
        self.clousText.setText(str(clousText))
        clousText = ""
        openText = ""
        openTextList = []
        clousTextList = []
        clousTextCharList = []

    def deshifr(self):
        pass
        self.openText.setText("")
        global clousText
        global openText
        global openTextList
        global clousTextList
        global inp_val_e
        global n
        global clousTextCharList
        global openTextCharList
        global d
        openText = ""
        clousText = self.clousText.toPlainText()
        for c in clousText:
            clousTextList.append(str(ord(c)))
            print("clousTextList ", clousTextList)
        for y in clousTextList:
            openTextList.append((int(y) ** d) % n)
            print("openTextList ", openTextList)
        for o in openTextList:
            openTextCharList.append(chr(int(o)))
            print("openTextCharList ", openTextCharList)
        print(clousText)
        openTextList.append((int(clousText) ** d) % n)
        clousText = "".join(openTextCharList)
        clousText = "".join(openTextList)
        test = (decimal.Decimal(int(clousText)) ** decimal.Decimal(d)) % decimal.Decimal(n)
        self.openText.setText(str(test))
        clousText = ""
        openText = ""
        openTextList = []
        clousTextList = []
        clousTextCharList = []
