#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle(tr("IT-Security SS2015")); m_cryptEngine = new CryptEngine(this); //Create ActionGroup to group the four Crypt-Actions //(only one can be active at a time, ActionGroup takes care of that) m_actionGroupAlgorithm = new QActionGroup(this); //Add the four Crypt-Actions m_actionGroupAlgorithm->addAction(ui->actionNullCipher); m_actionGroupAlgorithm->addAction(ui->actionSymmetric); m_actionGroupAlgorithm->addAction(ui->actionAsymmetric); m_actionGroupAlgorithm->addAction(ui->actionHybrid); //ActionGroup for the format of PlainTextEditEncrypted QActionGroup* actionGroup = new QActionGroup(this); actionGroup->addAction(ui->actionEncrypted_as_Text); actionGroup->addAction(ui->actionEncrypted_as_Binary); //ActionGroup for the format of PlainTextEditUnencrypted actionGroup = new QActionGroup(this); actionGroup->addAction(ui->actionUnencrypted_as_Text); actionGroup->addAction(ui->actionUnencrypted_as_Binary); //NullCipher/Symmetric/Asymmetric/Hybrid can also be selected via TabWidget: connect( ui->tabWidgetSelectAlgorithm, SIGNAL(currentTabChanged(QString)), this, SLOT(oncurrentTabChanged_triggered(QString))); //When another Cipher is selected, tell that directly to the CryptEngine connect( ui->tabWidgetSelectAlgorithm, SIGNAL(cipherChanged(int)), m_cryptEngine, SLOT(oncipherSelected_triggered(int))); //When another Cipher is selected, change PlainTextEdits for Hex or PlainText Data connect( m_cryptEngine, SIGNAL(binaryData(bool)), this, SLOT(ondataIsBinary_triggered(bool))); //When a new Keylength is selected, pass it on connect( ui->tabWidgetSelectAlgorithm, SIGNAL(keyLengthChanged(int,bool)), m_cryptEngine, SLOT(onkeylengthChanged(int,bool))); } MainWindow::~MainWindow() { delete m_cryptEngine; delete ui; } void MainWindow::setDataToPlainTextEditUnencrypted(QString text) { // //Set ClearText in CryptEngine, as binary or not // if( ui->actionUnencrypted_as_Text->isChecked() ) // { // m_cryptEngine->setClearText( ui->PlainTextEditUnencrypted->toPlainText().toAscii() ); // } // else if( ui->actionUnencrypted_as_Binary->isChecked() ) // { // QString hexString = ui->PlainTextEditUnencrypted->toPlainText().toAscii(); // QStringList hexStrings = hexString.split(' '); // hexString = hexStrings.join(""); // m_cryptEngine->setClearText( QByteArray::fromHex(QByteArray(hexString.toLatin1())) ); // } // else // { // QMessageBox::critical(this, "Error", "Unencrypted Text as neither ClearText nor Binary data!"); // return; // } } void MainWindow::setDataToPlainTextEditEncrypted(QString text) { } //private Methods void MainWindow::setPlainTextFromFile( QPlainTextEdit* destination, QFile* source ) { destination->clear(); destination->appendPlainText(QString(source->readAll())); } void MainWindow::setBinaryDataFromFile( QPlainTextEdit* destination, QFile* source ) { destination->clear(); destination->appendPlainText(source->readAll().toHex()); } void MainWindow::writePlainTextToFile( QPlainTextEdit* source, QFile* destination ) { destination->write( source->toPlainText().toLatin1() ); } void MainWindow::writeBinaryDataToFile( QPlainTextEdit* source, QFile* destination ) { destination->write(QByteArray::fromHex(QByteArray().append(source->toPlainText()))); } //private slots, actions: void MainWindow::on_actionLoad_triggered() { DialogLoad loadDialog(this); if (loadDialog.exec()) { QFile file( loadDialog.getFileName() ); if( file.open(QIODevice::ReadOnly) ) { if( loadDialog.getEncrypted() ) { if( loadDialog.getBinary() ) { setBinaryDataFromFile( ui->PlainTextEditEncrypted, &file ); } else { setPlainTextFromFile( ui->PlainTextEditEncrypted, &file ); } } else { if( loadDialog.getBinary() ) { setBinaryDataFromFile( ui->PlainTextEditUnencrypted, &file ); } else { setPlainTextFromFile( ui->PlainTextEditUnencrypted, &file ); } } file.close(); } else { QMessageBox::warning(this,"Warning: Loading of File", "File " + loadDialog.getFileName() + " could not be loaded!"); return; } } } void MainWindow::on_actionSave_triggered() { DialogSave saveDialog(this); if (saveDialog.exec()) { QFile file( saveDialog.getFileName() ); if( file.open(QIODevice::WriteOnly) ) { if( saveDialog.getEncrypted() ) { if( saveDialog.getBinary() ) { writeBinaryDataToFile( ui->PlainTextEditEncrypted, &file ); } else { writePlainTextToFile( ui->PlainTextEditEncrypted, &file ); } } else { if( saveDialog.getBinary() ) { writeBinaryDataToFile( ui->PlainTextEditUnencrypted, &file ); } else { writePlainTextToFile( ui->PlainTextEditUnencrypted, &file ); } } file.close(); } else { QMessageBox::warning(this,"Warning: Saving of File", "File " + saveDialog.getFileName() + " could not be saved!"); return; } } } void MainWindow::on_actionExit_triggered() { QApplication::quit(); } void MainWindow::on_actionAbout_triggered() { QMessageBox::information(this, "Project IT-Sicherheit", "Projectbase for the course\nIT-Sicherheit, SS2013\n@FH-SWF, Iserlohn"); } void MainWindow::on_actionNullCipher_triggered() { ui->tabWidgetSelectAlgorithm->onNullCipher_selected(); } void MainWindow::on_actionSymmetric_triggered() { ui->tabWidgetSelectAlgorithm->onSymmetric_selected(); } void MainWindow::on_actionAsymmetric_triggered() { ui->tabWidgetSelectAlgorithm->onAsymmetric_selected(); } void MainWindow::on_actionHybrid_triggered() { ui->tabWidgetSelectAlgorithm->onHybrid_selected(); } //private slots, buttons: void MainWindow::on_pushButtonClearUnencrypted_clicked() { ui->PlainTextEditUnencrypted->clear(); } void MainWindow::on_pushButtonClearEncrypted_clicked() { ui->PlainTextEditEncrypted->clear(); } void MainWindow::on_pushButtonEncrypt_clicked() { //If neither Symmetric, Asymmetric nor Hybrid is selected: Error if( m_actionGroupAlgorithm->checkedAction() == 0 ) { QMessageBox::critical(this, "Error", "No Algorithm selected"); return; } else if( ui->PlainTextEditUnencrypted->toPlainText().length() == 0 ) { QMessageBox::critical(this, "Error", "No Data to encrypt"); return; } //Set Key m_cryptEngine->setKey( ui->lineEditPassword->text() ); //Set ClearText in CryptEngine, as binary or not if( ui->actionUnencrypted_as_Text->isChecked() ) { m_cryptEngine->setClearText( ui->PlainTextEditUnencrypted->toPlainText().toLatin1() ); } else if( ui->actionUnencrypted_as_Binary->isChecked() ) { QString hexString = ui->PlainTextEditUnencrypted->toPlainText().toLatin1(); QStringList hexStrings = hexString.split(" "); hexString = hexStrings.join(""); m_cryptEngine->setClearText( QByteArray::fromHex(QByteArray(hexString.toLatin1())) ); } else { QMessageBox::critical(this, "Error", "Unencrypted Text as neither ClearText nor Binary data!"); return; } //encrypt m_cryptEngine->encrypt(); //Fetch CryptText to display ui->PlainTextEditEncrypted->clear(); if( ui->actionEncrypted_as_Text->isChecked() ) { ui->PlainTextEditEncrypted->appendPlainText( QString(m_cryptEngine->getCryptText()) ); } else if( ui->actionEncrypted_as_Binary->isChecked() ) { QString tempString = QString(m_cryptEngine->getCryptText().toHex()); QString hexString; for (int var = 0; var < tempString.length(); var+=2) { hexString.append( tempString.at(var) ); hexString.append( tempString.at(var+1) ); hexString.append( ' ' ); } ui->PlainTextEditEncrypted->appendPlainText( hexString ); } else { QMessageBox::critical(this, "Error", "Encrypted Text as neither ClearText nor Binary data!"); return; } } void MainWindow::on_pushButtonDecrypt_clicked() { //If neither Symmetric, Asymmetric nor Hybrid is selected: Error if( m_actionGroupAlgorithm->checkedAction() == 0 ) { QMessageBox::critical(this, "Error", "No Algorithm selected"); return; } else if( ui->PlainTextEditEncrypted->toPlainText().length() == 0 ) { QMessageBox::critical(this, "Error", "No Data to decrypt"); return; } //Set Key m_cryptEngine->setKey( ui->lineEditPassword->text() ); //Set CryptText in CryptEngine if( ui->actionEncrypted_as_Text->isChecked() ) { m_cryptEngine->setCryptText( ui->PlainTextEditEncrypted->toPlainText().toLatin1() ); } else if( ui->actionEncrypted_as_Binary->isChecked() ) { QString hexString = ui->PlainTextEditEncrypted->toPlainText().toLatin1(); QStringList hexStrings = hexString.split(" "); hexString = hexStrings.join(""); m_cryptEngine->setCryptText( QByteArray::fromHex(QByteArray(hexString.toLatin1())) ); } else { QMessageBox::critical(this, "Error", "Encrypted Text as neither ClearText nor Binary data!"); return; } //decrypt m_cryptEngine->decrypt(); //fetch ClearText to display ui->PlainTextEditUnencrypted->clear(); if( ui->actionUnencrypted_as_Text->isChecked() ) { ui->PlainTextEditUnencrypted->appendPlainText( QString(m_cryptEngine->getClearText()) ); } else if( ui->actionUnencrypted_as_Binary->isChecked() ) { QString tempString = QString(m_cryptEngine->getClearText().toHex()); QString hexString; for (int var = 0; var < tempString.length(); var+=2) { hexString.append( tempString.at(var) ); hexString.append( tempString.at(var+1) ); hexString.append( ' ' ); } hexString.chop(1); //Last ' ' ui->PlainTextEditUnencrypted->appendPlainText( hexString ); } else { QMessageBox::critical(this, "Error", "Unencrypted Text as neither ClearText nor Binary data!"); return; } } void MainWindow::on_pushButtonLoad_clicked() { on_actionLoad_triggered(); } void MainWindow::on_pushButtonSave_clicked() { on_actionSave_triggered(); } void MainWindow::oncurrentTabChanged_triggered(QString currentTabName) { //Selection on TabWidget changes (NullCipher)/Symmetric/Asymmetric/Hybrid as well if( currentTabName == "NullCipher" ) ui->actionNullCipher->setChecked(true); else if( currentTabName == "Symmetric" ) ui->actionSymmetric->setChecked(true); else if( currentTabName == "Asymmetric" ) ui->actionAsymmetric->setChecked(true); else if( currentTabName == "Hybrid" ) ui->actionHybrid->setChecked(true); //set Algorithm Indicator Label to current kind of Algorithm ui->labelAlgorithmCurrent->setText(currentTabName); } void MainWindow::ondataIsBinary_triggered(bool dataIsBinary) { if(dataIsBinary) { ui->actionEncrypted_as_Binary->setChecked(true); ui->actionUnencrypted_as_Binary->setChecked(true); ui->actionEncrypted_as_Text->setChecked( false); ui->actionUnencrypted_as_Text->setChecked(false); on_actionUnencrypted_as_Binary_triggered( true ); on_actionEncrypted_as_Binary_triggered( true ); qDebug("databinary true"); } else { ui->actionEncrypted_as_Binary->setChecked(false); ui->actionUnencrypted_as_Binary->setChecked(false); ui->actionEncrypted_as_Text->setChecked( true); ui->actionUnencrypted_as_Text->setChecked(true); on_actionUnencrypted_as_Binary_triggered( false ); on_actionEncrypted_as_Binary_triggered( false ); qDebug("databinary false"); } } void MainWindow::on_pushButtonGeneratePassword_clicked() { m_cryptEngine->generateRandomKey(); ui->lineEditPassword->setText( m_cryptEngine->getKey().toHex() ); } void MainWindow::on_actionUnencrypted_as_Text_triggered(bool checked) { if( checked ) ui->PlainTextEditUnencrypted->setEnabled(true); else ui->PlainTextEditUnencrypted->setEnabled(false); } void MainWindow::on_actionUnencrypted_as_Binary_triggered(bool checked) { if( checked ) ui->PlainTextEditUnencrypted->setEnabled(false); else ui->PlainTextEditUnencrypted->setEnabled(true); } void MainWindow::on_actionEncrypted_as_Text_triggered(bool checked) { if( checked ) ui->PlainTextEditEncrypted->setEnabled(true); else ui->PlainTextEditEncrypted->setEnabled(false); } void MainWindow::on_actionEncrypted_as_Binary_triggered(bool checked) { if( checked ) ui->PlainTextEditEncrypted->setEnabled(false); else ui->PlainTextEditEncrypted->setEnabled(true); }