# encoding:utf-8

# Form implementation generated from reading ui file 'CHAR_DECODE.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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 PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys
import re
import base64
import urllib.parse
import codecs


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(879, 530)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(900, 530))
        MainWindow.setMaximumSize(QtCore.QSize(900, 530))
        MainWindow.setBaseSize(QtCore.QSize(900, 530))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QtCore.QRect(10, 10, 871, 471))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.layoutWidget.sizePolicy().hasHeightForWidth())
        self.layoutWidget.setSizePolicy(sizePolicy)
        self.layoutWidget.setObjectName("layoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
        self.gridLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
        self.gridLayout.setContentsMargins(0, 0, 0, 1)
        self.gridLayout.setHorizontalSpacing(0)
        self.gridLayout.setVerticalSpacing(6)
        self.gridLayout.setObjectName("gridLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.label_before = QtWidgets.QLabel(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_before.sizePolicy().hasHeightForWidth())
        self.label_before.setSizePolicy(sizePolicy)
        self.label_before.setObjectName("label_before")
        self.verticalLayout.addWidget(self.label_before)
        self.before_decrypt = QtWidgets.QPlainTextEdit(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.before_decrypt.sizePolicy().hasHeightForWidth())
        self.before_decrypt.setSizePolicy(sizePolicy)
        self.before_decrypt.setObjectName("before_decrypt")
        self.verticalLayout.addWidget(self.before_decrypt)
        self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.label_end = QtWidgets.QLabel(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_end.sizePolicy().hasHeightForWidth())
        self.label_end.setSizePolicy(sizePolicy)
        self.label_end.setObjectName("label_end")
        self.verticalLayout_2.addWidget(self.label_end)
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.plainTextEdit.sizePolicy().hasHeightForWidth())
        self.plainTextEdit.setSizePolicy(sizePolicy)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.verticalLayout_2.addWidget(self.plainTextEdit)
        self.gridLayout.addLayout(self.verticalLayout_2, 0, 2, 1, 1)
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.char_decrypt = QtWidgets.QPushButton(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.char_decrypt.sizePolicy().hasHeightForWidth())
        self.char_decrypt.setSizePolicy(sizePolicy)
        self.char_decrypt.setObjectName("char_decrypt")
        self.verticalLayout_3.addWidget(self.char_decrypt, 0, QtCore.Qt.AlignHCenter)
        self.hex_decrypt = QtWidgets.QPushButton(self.layoutWidget)
        self.hex_decrypt.setObjectName("hex_decrypt")
        self.verticalLayout_3.addWidget(self.hex_decrypt)
        self.url_decrypt = QtWidgets.QPushButton(self.layoutWidget)
        self.url_decrypt.setObjectName("url_decrypt")
        self.verticalLayout_3.addWidget(self.url_decrypt)
        self.base64_decrypt = QtWidgets.QPushButton(self.layoutWidget)
        self.base64_decrypt.setObjectName("base64_decrypt")
        self.verticalLayout_3.addWidget(self.base64_decrypt)
        self.clear = QtWidgets.QPushButton(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.clear.sizePolicy().hasHeightForWidth())
        self.clear.setSizePolicy(sizePolicy)
        self.clear.setObjectName("clear")
        self.verticalLayout_3.addWidget(self.clear, 0, QtCore.Qt.AlignHCenter)
        self.gridLayout.addLayout(self.verticalLayout_3, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 879, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "DECODE_TOOLS BY hyj"))
        self.label_before.setText(_translate("MainWindow", "解密前："))
        self.label_end.setText(_translate("MainWindow", "解密后："))
        self.char_decrypt.setText(_translate("MainWindow", "CHAR解密"))
        self.hex_decrypt.setText(_translate("MainWindow", "HEX解密"))
        self.url_decrypt.setText(_translate("MainWindow", "URL解密"))
        self.base64_decrypt.setText(_translate("MainWindow", "Base64解密"))
        self.clear.setText(_translate("MainWindow", "清空"))
        self.char_decrypt.clicked.connect(self.char_fun)
        self.hex_decrypt.clicked.connect(self.hex_fun)
        self.url_decrypt.clicked.connect(self.url_fun)
        self.base64_decrypt.clicked.connect(self.base64_fun)
        self.clear.clicked.connect(self.clear_fun)

    def char_fun(self):
        try:
            str_list = []
            before_decrypt = self.before_decrypt.toPlainText()
            before_decrypt = before_decrypt.replace(' ', '')
            str_re = re.findall(r'[:=)(]?(\d+)', before_decrypt)
            for i in str_re:
                t = chr(int(i))
                str_list.append(t)
            char_decode = ''.join(str_list)
            self.plainTextEdit.setPlainText(char_decode)
        except Exception as e:
            self.fail_result = "请检查格式！" + '\n' + str(e)
            self.plainTextEdit.setPlainText(self.fail_result)

    def hex_fun(self):
        try:
            str_list = []
            decode_str = ""
            before_decrypt = self.before_decrypt.toPlainText()
            before_decrypt = before_decrypt.replace(' ', '')
            str_re = re.findall(r'[\\\|0]?x?([0-9a-zA-Z]{2})', before_decrypt)
            for i in str_re:
                try:
                    new_str = codecs.decode(i, "hex").decode('utf-8')
                except:
                    new_str = '?'
                str_list.append(new_str)
                decode_str = ''.join(str_list)
            self.plainTextEdit.setPlainText(decode_str)
        except Exception as e:
            self.fail_result = "请检查格式！" + '\n' + str(e)
            self.plainTextEdit.setPlainText(self.fail_result)

    def base64_fun(self):
        try:
            before_decrypt = self.before_decrypt.toPlainText()
            decode_str = str(base64.b64decode(before_decrypt), "utf-8", "ignore")
            self.plainTextEdit.setPlainText(decode_str)
        except Exception as e:
            self.fail_result = "请检查格式！" + '\n' + str(e)
            self.plainTextEdit.setPlainText(self.fail_result)

    def url_fun(self):
        try:
            before_decrypt = self.before_decrypt.toPlainText()
            before_decrypt = before_decrypt.encode('utf-8').decode('unicode_escape')
            decode_str = urllib.parse.unquote(before_decrypt)
            self.plainTextEdit.setPlainText(decode_str)
        except Exception as e:
            self.fail_result = "请检查格式！" + '\n' + str(e)
            self.plainTextEdit.setPlainText(self.fail_result)

    def clear_fun(self):
        self.before_decrypt.setPlainText("")
        self.plainTextEdit.setPlainText("")


class MyApp(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.setupUi(self)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())
