加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
encryptionpage.cpp 10.32 KB
一键复制 编辑 原始数据 按行查看 历史
聆听随风 提交于 2021-08-25 09:33 . 设置图片只适应Dlabel
#include "encryptionpage.h"
#include "base64.h"
#include <QGridLayout>
#include <QLabel>
#include <DLabel>
#include <DDialog>
Encryptionpage::Encryptionpage(QWidget *parent)
: QFrame(parent),m_lineEdit(new DLineEdit()),
m_toast(new DToast(this)),
m_encryptedEdit(new DTextEdit),
m_decryptedEdit(new DTextEdit),
m_hexEdit(new DTextEdit),
m_rsaPublicKeyEdit(new DTextEdit),
m_rsaPrivateEdit(new DTextEdit),
m_btnMD5(new DPushButton("MD5")),
m_btnSHA(new DPushButton("SHA256")),
m_btnEncryptDES(new DPushButton("DES加密")),
m_btnDecryptDes(new DPushButton("DES解密")),
m_btnRSAPruKey(new DPushButton("生成RSA公私钥")),
m_btnEncryptRSA(new DPushButton("RSA加密")),
m_btnDecryptRSA(new DPushButton("RSA解密")),
m_btnClean(new DPushButton("清空数据"))
{
initUI();
initConnections();
}
void Encryptionpage::showEvent(QShowEvent *event)
{
// 移动DToast的位置到窗口中下方的位置并显示
m_toast->move((width() - m_toast->width()) / 2.0,
height() - m_toast->height());
m_toast->pop();
QWidget::showEvent(event);
}
void Encryptionpage::paintEvent(QPaintEvent *event)
{
// QPainter painter(this);
// if (m_bg.isNull()) {
// QPixmap pix(":/images/effects-bg.jpg");
// m_bg = pix.scaled(rect().size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
// }
// painter.drawPixmap(0, 0, m_bg);
// painter.end();
QFrame::paintEvent(event);
}
void Encryptionpage::initUI()
{
// 设置DToast的提示信息内容
m_toast->setText("Welcome To Use DtkEncrypt");
// 构建界面内容布局
QGridLayout *layout = new QGridLayout;
layout->setSpacing(10);
layout->addWidget(new DLabel("原始明文"), 0, 0, 1, 1, Qt::AlignRight);
layout->addWidget(new DLabel("加密密文/摘要"), 1, 0, 1, 1, Qt::AlignRight);
layout->addWidget(new DLabel("解密明文"), 2, 0, 1, 1, Qt::AlignRight);
layout->addWidget(new DLabel("Hex摘要串"), 3, 0, 1, 1, Qt::AlignRight);
layout->addWidget(new DLabel("RSA公钥"), 4, 0, 1, 1, Qt::AlignRight);
layout->addWidget(new DLabel("RSA私钥"), 5, 0, 1, 1, Qt::AlignRight);
m_lineEdit->setFixedWidth(640);
m_lineEdit->lineEdit()->setPlaceholderText("请输入原始明文");
m_lineEdit->lineEdit()->setClearButtonEnabled(true);
layout->addWidget(m_lineEdit, 0, 2, 1, 1, Qt::AlignLeft);
m_encryptedEdit->setFixedSize(640, 100);
layout->addWidget(m_encryptedEdit, 1, 2, 1, 1, Qt::AlignLeft);
m_decryptedEdit->setFixedSize(640, 100);
layout->addWidget(m_decryptedEdit, 2, 2, 1, 1, Qt::AlignLeft);
m_hexEdit->setFixedSize(640, 100);
layout->addWidget(m_hexEdit, 3, 2, 1, 1, Qt::AlignLeft);
m_rsaPublicKeyEdit->setFixedSize(640, 100);
layout->addWidget(m_rsaPublicKeyEdit, 4, 2, 1, 1, Qt::AlignLeft);
m_rsaPrivateEdit->setFixedSize(640, 100);
layout->addWidget(m_rsaPrivateEdit, 5, 2, 1, 1, Qt::AlignLeft);
//设置按钮大小
m_btnMD5->setFixedSize(150, 36);
layout->addWidget(m_btnMD5, 0, 3, 1, 1, Qt::AlignLeft);
m_btnSHA->setFixedSize(150, 36);
layout->addWidget(m_btnSHA, 0, 3, 2, 1, Qt::AlignLeft);
m_btnEncryptDES->setFixedSize(150, 36);
layout->addWidget(m_btnEncryptDES, 1, 3, 2, 1, Qt::AlignLeft);
m_btnDecryptDes->setFixedSize(150, 36);
layout->addWidget(m_btnDecryptDes, 1, 3, 3, 1, Qt::AlignLeft);
m_btnRSAPruKey->setFixedSize(150,36);
layout->addWidget(m_btnRSAPruKey,3, 3, 1, 1,Qt::AlignLeft);
m_btnEncryptRSA->setFixedSize(150, 36);
layout->addWidget(m_btnEncryptRSA, 3, 3, 2, 1, Qt::AlignLeft);
m_btnDecryptRSA->setFixedSize(150, 36);
layout->addWidget(m_btnDecryptRSA, 3, 3, 3, 1, Qt::AlignLeft);
m_btnClean->setFixedSize(150, 36);
layout->addWidget(m_btnClean, 5, 3, 1, 1, Qt::AlignLeft);
// 竖直方向挤压内容,内容紧凑,而且会好看一点
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addStretch();
vbox->addLayout(layout);
vbox->addStretch();
// 水平方向挤压内容,内容紧凑,而且会好看一点
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addLayout(vbox);
hbox->addStretch();
setLayout(hbox);
}
void Encryptionpage::initConnections()
{
connect(m_btnMD5, &DPushButton::clicked, this, [this]() {
MD5ConvertText();
});
connect(m_btnSHA,&DPushButton::clicked,this,[this](){
SHA256ConvertText();
} );
connect(m_btnEncryptDES,&DPushButton::clicked,this,[this]() {
DESEncryptText();
});
connect(m_btnDecryptDes,&DPushButton::clicked,this,[this]() {
DESDecryptText();
});
connect(m_btnRSAPruKey,&DPushButton::clicked,this,[this]() {
GenerateRSAKey();
});
connect(m_btnEncryptRSA,&DPushButton::clicked,this,[this]() {
RSAEncryptText();
});
connect(m_btnDecryptRSA,&DPushButton::clicked,this,[this]() {
RSADecryptText();
});
connect(m_btnClean, &DPushButton::clicked, this, [this] () {
m_lineEdit->clear();
m_encryptedEdit->clear();
m_decryptedEdit->clear();
m_hexEdit->clear();
m_rsaPublicKeyEdit->clear();
m_rsaPrivateEdit->clear();
m_desEncryptText.clear();
m_desDecryptText.clear();
m_rsaDecryptText.clear();
m_rsaEncryptText.clear();
});
}
void Encryptionpage::MD5ConvertText()
{
if(m_lineEdit->text().isEmpty())
{
DDialog dialog("", "原始明文为空,请输入!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
std::string encryptText;
std::string encryptHexText;
m_encryImpl.md5(m_lineEdit->text().toStdString(), encryptText, encryptHexText);
m_encryptedEdit->setText(QString::fromStdString(encryptText));
m_hexEdit->setText(QString::fromStdString(encryptHexText));
}
void Encryptionpage::SHA256ConvertText()
{
if(m_lineEdit->text().isEmpty())
{
DDialog dialog("", "原始明文为空,请输入!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
std::string encryptText;
std::string encryptHexText;
m_encryImpl.sha256(m_lineEdit->text().toStdString(), encryptText, encryptHexText);
m_encryptedEdit->setText(QString::fromStdString(encryptText));
m_hexEdit->setText(QString::fromStdString(encryptHexText));
}
void Encryptionpage::DESEncryptText()
{
if(m_lineEdit->text().isEmpty())
{
DDialog dialog("", "原始明文为空,请输入!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
std::string desKey = "12345";
m_desEncryptText = m_encryImpl.des_encrypt(m_lineEdit->text().toStdString(), desKey);
QString qcrypt = QString::fromStdString(m_desEncryptText);
//QString qstring = QString(QString::fromLocal8Bit(encryptText.c_str()));
//cstring = std::string((const char *)qstring.toLocal8Bit().constData());
//m_encryptedEdit->setText(Base64::textToBase64(QString::fromStdString(encryptText)) );
m_encryptedEdit->setText(QString::fromStdString(m_desEncryptText));
}
void Encryptionpage::DESDecryptText()
{
if(m_encryptedEdit->toPlainText().isEmpty())
{
DDialog dialog("", "待解密密文为空,请输入!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
std::string desKey = "12345";
//m_desDecryptText = m_encryImpl.des_decrypt(m_desEncryptText, desKey);
m_desDecryptText = m_encryImpl.des_decrypt(m_encryptedEdit->toPlainText().toStdString(), desKey);
m_decryptedEdit->setText(QString::fromStdString(m_desDecryptText));
}
void Encryptionpage::GenerateRSAKey()
{
m_encryImpl.generateRSAKey(m_key);
//公钥
m_rsaPublicKeyEdit->setText(QString::fromStdString(m_key[0]));
//私钥
m_rsaPrivateEdit->setText(QString::fromStdString(m_key[1]));
}
void Encryptionpage::RSAEncryptText()
{
if(m_lineEdit->text().isEmpty())
{
DDialog dialog("", "原始明文为空,请输入!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
// //RSA私钥加密
// m_rsaEncryptText2 = m_encryImpl.rsa_pri_encrypt_base64(m_lineEdit->text(),m_key[1]);
// m_encryptedEdit->setText(m_rsaEncryptText2);
//RSA公钥加密
if(m_rsaPublicKeyEdit->toPlainText().isEmpty())
{
DDialog dialog("", "RSA公钥为空,请先生成!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
m_rsaEncryptText = m_encryImpl.rsa_pub_encrypt(m_lineEdit->text().toStdString(),m_rsaPublicKeyEdit->toPlainText().toStdString());
m_encryptedEdit->setText(QString::fromStdString(m_rsaEncryptText));
}
void Encryptionpage::RSADecryptText()
{
// //RSA公钥解密
// QString decrypt_str = m_encryImpl.rsa_pub_decrypt_base64(const_cast<QString&>(m_rsaEncryptText2),m_key[0]);
// m_decryptedEdit->setText(decrypt_str);
//RSA私钥解密
if(m_encryptedEdit->toPlainText().isEmpty())
{
DDialog dialog("", "待解密密文为空,请输入!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
if(m_rsaPrivateEdit->toPlainText().isEmpty())
{
DDialog dialog("", "RSA私钥为空,请先生成!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
m_rsaDecryptText = m_encryImpl.rsa_pri_decrypt(m_encryptedEdit->toPlainText().toStdString(), m_rsaPrivateEdit->toPlainText().toStdString());
//m_rsaDecryptText = m_encryImpl.rsa_pri_decrypt(m_rsaEncryptText, m_key[1]);
m_decryptedEdit->setText(QString::fromStdString(m_rsaDecryptText));
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化