MainWindow.cpp
#include "MainWindow.h"
#include "..\__DESAlgorithm.h"
MainWindow::MainWindow(){
QTextCodec::setCodecForTr( QTextCodec::codecForName( "UTF-8" ) );
pqlMainLayout = new QVBoxLayout( this );
setLayout( pqlMainLayout );
resize( 600, 400 );
CreatMenuBar();
CreatLayouts();
}
MainWindow::~MainWindow(){
;
}
void MainWindow::CreatMenuBar(){
pmbMenuBar = new QMenuBar( this );
setMenuBar( pmbMenuBar );
pqmFile = new QMenu( tr("文件(&F)"), this );
pmbMenuBar->addMenu( pqmFile );
pqaExit = pqmFile->addAction( tr("退出(&X)") );
pqaExit->setShortcut( tr("Ctrl+Q") );
connect( pqaExit, SIGNAL( triggered() ), this, SLOT( close() ) );
pqmHelp = new QMenu( tr("帮助(&H)"), this);
pmbMenuBar->addMenu( pqmHelp );
pqaAbout = pqmHelp->addAction( tr("关于(&A)") );
pqaAbout->setShortcut( tr("Ctrl+A") );
connect( pqaAbout, SIGNAL( triggered() ), this, SLOT( About() ) );
pqaAboutQt = pqmHelp->addAction( tr("关于Qt(&Q)") );
pqaAboutQt->setShortcut( tr("Ctrl+T") );
connect( pqaAboutQt, SIGNAL( triggered() ), this, SLOT( AboutQt() ) );
}
void MainWindow::CreatLayouts(){
pqwCentralWidget = new QWidget( this );
setCentralWidget( pqwCentralWidget );
pqlMainLayout = new QVBoxLayout( this );
pqwCentralWidget -> setLayout( pqlMainLayout );
pqgbTextBox = new QGroupBox( tr("明文(&T)"), this );
pqlMainLayout->addWidget( pqgbTextBox );
pqlTextLayout = new QHBoxLayout();
pqgbTextBox->setLayout( pqlTextLayout );
pqteText = new QTextEdit;
pqlTextLayout->addWidget( pqteText );
pqgbCipherTextBox = new QGroupBox( tr("密文(&C)"), this );
pqlMainLayout->addWidget( pqgbCipherTextBox );
pqlCipherTextLayout = new QHBoxLayout();
pqgbCipherTextBox->setLayout( pqlCipherTextLayout );
pqteCipherText = new QTextEdit;
pqlCipherTextLayout->addWidget( pqteCipherText );
pqwKeyWidget = new QWidget();
pqlMainLayout->addWidget( pqwKeyWidget );
pqlKeyLayout = new QHBoxLayout();
pqwKeyWidget->setLayout( pqlKeyLayout );
pqlKeyLabel= new QLabel( tr("密钥(&K)") );
pqlKeyLayout->addWidget( pqlKeyLabel );
pqleKey = new QLineEdit();
pqlKeyLayout->addWidget( pqleKey );
pqdbbButtonBox = new QDialogButtonBox( this );
pqlMainLayout->addWidget( pqdbbButtonBox );
pqpbEncryption = pqdbbButtonBox->addButton( tr("加密(&E)"), QDialogButtonBox::ActionRole );
pqpbDecryption = pqdbbButtonBox->addButton( tr("解密(&D)"), QDialogButtonBox::ActionRole );
connect( pqpbEncryption, SIGNAL( clicked() ), this, SLOT( Encryption() ) );
connect( pqpbDecryption, SIGNAL( clicked() ), this, SLOT( Decryption() ) );
}
void MainWindow::About(){
QMessageBox::about(
this,
tr("关于本程序"),
tr("
这Qt应用程序部分源代码使用了Qt库。
"));
}
void MainWindow::AboutQt(){
QMessageBox::aboutQt(
this,
tr("关于 Qt")
);
}
void MainWindow::Encryption(){
QString
qsText = pqteText->toPlainText()/*从明文文本框获取输入的明文*/,
qsKey = pqleKey->text()/*从密钥文本框获取输出的密钥*/
;
QByteArray
qbaText = qsText.toUtf8()/*将目标转换为UTF-8串*/,
qbaKey = qsKey.toUtf8()/*将密钥转换为UTF-8串*/
;
des::CString * c_string_text = des::c_string_new( qbaText, -1 );
des::CString * c_string_key = des::c_string_new( qbaKey, -1);
//加密得到密文
des::CString * c_string_target = encryption_fun ( c_string_text, c_string_key, encryption/*加密*/ );
pqteCipherText->setPlainText ( QString::fromUtf8( c_string_target->str )/*将UTF-8串转为QString*/ );
des::c_string_free ( c_string_text );
des::c_string_free ( c_string_key );
des::c_string_free ( c_string_target );
}
void MainWindow::Decryption(){
QString
qsText = pqteCipherText->toPlainText()/*从密文文本框获取输入的密文*/,
qsKey = pqleKey->text()/*从密钥文本框获取输出的密钥*/
;
QByteArray
qbaText = qsText.toUtf8()/*将目标转换为UTF-8串*/,
qbaKey = qsKey.toUtf8()/*将密钥转换为UTF-8串*/
;
des::CString * c_string_text = des::c_string_new( qbaText, -1 );
des::CString * c_string_key = des::c_string_new( qbaKey, -1);
//解密得到明文
des::CString * c_string_target = encryption_fun ( c_string_text, c_string_key, decryption/*解密*/ );
pqteText->setPlainText ( QString::fromUtf8( c_string_target->str )/*将UTF-8串转为QString*/ );
des::c_string_free ( c_string_text );
des::c_string_free ( c_string_key );
des::c_string_free ( c_string_target );
}