void levelAction(KeyType key, bool encode, std::istream & in, std::ostream & out)
	{
		Crypt<CypherType,Group,Pack> c;
		c.setKey(key);
		if (encode){
			c.encode(in,out);
		}else{
			c.decode(in,out);
		}
	}
//********************************************************************
//
// Method: decrypt
// Parameter: QString* cipher, read input from techniqueComboBox_2, input key
//
// Purpose: use Cyper to decrypt, return plaintext
//
//********************************************************************
QString* MainWindow::decrypt(QString* cipher)
{
    Crypt c (cipher,&(ui->keyTextField_2->text()),format);
    switch (ui->techniqueComboBox_2->currentIndex())
    {
    case 0: //Caesar
        c.caesar(DECRYPT);
        break;
    case 1: //Vigenère
        c.vigenere(DECRYPT);
        break;
    default:
        ui->keyTipLabel_2->setText("Decryption failed");
    }
    return cipher;
}
//********************************************************************
//
// Method: encrypt
// Parameter: QString* plain, read input from techniqueComboBox, input key
//
// Purpose: use Cyper to encrypt, return ciphertext
//
//********************************************************************
QString* MainWindow::encrypt(QString* plain)
{
    Crypt c (plain,&(ui->keyTextField->text()),format);
    switch (ui->techniqueComboBox->currentIndex())
    {
    case 0: //Caesar
        c.caesar(ENCRYPT);
        break;
    case 1: //Vigenère
        c.vigenere(ENCRYPT);
        break;
    default:
        ui->keyTipLabel->setText("Encryption failed");
    }
    return plain;
}
void Settings::refresh()
{        
    // Read ini file    
    this->iniPath = AppFunc::getAppSavePath() + QLatin1String("/vpn.ini");

    QSettings sett (this->iniPath, QSettings::IniFormat);

    // Die Settings einlesen    
    this->noBallonMessage = (sett.value(QLatin1String("connect/noballon"), QLatin1String("0")).toString() == QLatin1String("1") ? true : false);
    this->popUpDialogValue = (sett.value(QLatin1String("connect/popup"), QLatin1String("1")).toString() == QLatin1String("1") ? true : false);
    this->showSplashScreenValue = (sett.value(QLatin1String("connect/splash"), QLatin1String("1")).toString() == QLatin1String("1") ? true : false);
    this->autoReconnect = (sett.value(QLatin1String("connect/autoreconnect"), QLatin1String("0")).toString() == QLatin1String("1") ? true : false);
    this->useNoInteract = (sett.value(QLatin1String("connect/nointeract"), QLatin1String("0")).toString() == QLatin1String("1") ? true : false);

    // Start config
    this->startCommandConfig = (sett.value(QLatin1String("start/auto"), QLatin1String("0")).toString() == QLatin1String("1") ? true : false);
    this->commandConfigPath = sett.value(QLatin1String("start/path"), QLatin1String("")).toString();

    this->delayConfigVal = sett.value(QLatin1String("start/delay"), QLatin1String("0")).toString();

    this->cryptKey = sett.value(QLatin1String("self/key"), QLatin1String("")).toString();
    if (!this->cryptKey.isEmpty()) {        
        Crypt crypt;
        this->cryptKey = QString(crypt.cryptToPlainTextExt(this->cryptKey.toAscii()));        
    } else {
        // Neuen Key erzeugen
        qsrand(QDateTime::currentDateTime().toTime_t());
        QString _t1 (QString::number((qrand() % 1500) + 1));
        QString _t2 (QString::number((qrand() % 2500) + 1));
        QString key (QLatin1String("S3m!BHF") + _t1 + QLatin1String("83$%§kd") + _t2 + QString::number(QDateTime::currentDateTime().toTime_t()) + _t1);

        Crypt crypt;
        key = QString(crypt.cryptPlainTextExt(key.toAscii()));

        sett.setValue(QLatin1String("self/key"), key);
        this->cryptKey = key;
    }
}
Exemple #5
0
int main(int argc, char** argv) {

    cmdline_parser parser; //Initialise the parser

    if (!parser.process_cmdline(argc, argv)) { //Try parse cmdline args
        cerr << "Couldn't process command line arguments" << endl;
    }

    if (parser.should_print_help()) {   //Print help if requested
        parser.print_help(std::cout);
    }
    
    string encodable = "";      //The string that holds the text to run ciphers on
    string inputFileName = parser.get_input_filename();
    string outputFileName = parser.get_output_filename();
    cout << "Output file in use: " << outputFileName << endl;
    
    if (!parser.should_get_user_input()) {

        

        cout << "Input file in use: " << inputFileName << endl;
        


        string temp;
        ifstream inpFileStream;

        inpFileStream.open(inputFileName);


        if (inpFileStream.is_open()) {

            while (!inpFileStream.eof()) { //Read from file into string encodable
                getline(inpFileStream, temp);
                encodable += temp;
            }
        }


        inpFileStream.close();
    }
    else {
        cout << "Please enter the text you want to pass to the cipher: ";
        getline (cin, encodable);
    }

    boost::to_upper(encodable);
    
    string returnStr = "";
    
    if (parser.using_caesar()) {
        int key = parser.get_caesar_key();
        if (parser.should_pack()) {     //Will return "Not implemented"
            if (parser.should_group()) {
                if (parser.should_encode()) { 
                    Crypt<caesar, packing, grouping> crypt; 
                    returnStr = crypt.encode(encodable, key);
                }
                else {
                    Crypt<caesar, packing, grouping> crypt; 
                    returnStr = crypt.decode(encodable, key);
                }
            }
            else {
                if (parser.should_encode()) { 
                    Crypt<caesar, packing, no_grouping> crypt; 
                    returnStr = crypt.encode(encodable, key); 
                }
                else {
                    Crypt<caesar, packing, no_grouping> crypt; 
                    returnStr = crypt.decode(encodable, key); 
                }
            }
        }
        else {
            if (parser.should_group()) {
                if (parser.should_encode()) { 
                    Crypt<caesar, no_packing, grouping> crypt; 
                    returnStr = crypt.encode(encodable, key);
                }
                else {
                    Crypt<caesar, no_packing, grouping> crypt; 
                    returnStr = crypt.decode(encodable, key);
                }
            }
            else {
                if (parser.should_encode()) { 
                    Crypt<caesar, no_packing, no_grouping> crypt; 
                    returnStr = crypt.encode(encodable, key); 
                }
                else {
                    Crypt<caesar, no_packing, no_grouping> crypt; 
                    returnStr = crypt.decode(encodable, key); 
                }
            }
        }
    }
    else if (parser.using_vigenere()) {
        string key = parser.get_vigenere_key();
        if (parser.should_pack()) {     //Will return "Not implemented"
            if (parser.should_group()) {
                if (parser.should_encode()) { 
                    Crypt<vigenere, packing, grouping> crypt; 
                    returnStr = crypt.encode(encodable, key);
                }
                else {
                    Crypt<vigenere, packing, grouping> crypt; 
                    returnStr = crypt.decode(encodable, key);
                }
            }
            else {
                if (parser.should_encode()) { 
                    Crypt<vigenere, packing, no_grouping> crypt; 
                    returnStr = crypt.encode(encodable, key); 
                }
                else {
                    Crypt<vigenere, packing, no_grouping> crypt; 
                    returnStr = crypt.decode(encodable, key); 
                }
            }
        }
        else {
            if (parser.should_group()) {
                if (parser.should_encode()) { 
                    Crypt<vigenere, no_packing, grouping> crypt; 
                    returnStr = crypt.encode(encodable, key);
                }
                else {
                    Crypt<vigenere, no_packing, grouping> crypt; 
                    returnStr = crypt.decode(encodable, key);
                }
            }
            else {
                if (parser.should_encode()) { 
                    Crypt<vigenere, no_packing, no_grouping> crypt; 
                    returnStr = crypt.encode(encodable, key); 
                }
                else {
                    Crypt<vigenere, no_packing, no_grouping> crypt; 
                    returnStr = crypt.decode(encodable, key); 
                }
            }
        }
    }
    else if (parser.using_XOR()) {
        int32_t key = parser.get_XOR_key();
        if (parser.should_pack()) {     //Will return "Not implemented"
            if (parser.should_group()) {
                if (parser.should_encode()) { 
                    Crypt<XOR, packing, grouping> crypt; 
                    returnStr = crypt.encode(encodable, key);
                }
                else {
                    Crypt<XOR, packing, grouping> crypt; 
                    returnStr = crypt.decode(encodable, key);
                }
            }
            else {
                if (parser.should_encode()) { 
                    Crypt<XOR, packing, no_grouping> crypt; 
                    returnStr = crypt.encode(encodable, key); 
                }
                else {
                    Crypt<XOR, packing, no_grouping> crypt; 
                    returnStr = crypt.decode(encodable, key); 
                }
            }
        }
        else {
            if (parser.should_group()) {
                if (parser.should_encode()) { 
                    Crypt<XOR, no_packing, grouping> crypt; 
                    returnStr = crypt.encode(encodable, key);
                }
                else {
                    Crypt<XOR, no_packing, grouping> crypt; 
                    returnStr = crypt.decode(encodable, key);
                }
            }
            else {
                if (parser.should_encode()) { 
                    Crypt<XOR, no_packing, no_grouping> crypt; 
                    returnStr = crypt.encode(encodable, key); 
                }
                else {
                    Crypt<XOR, no_packing, no_grouping> crypt; 
                    returnStr = crypt.decode(encodable, key); 
                }
            }
        }
    }
    
    ofstream outFileStream;
    outFileStream.open(outputFileName);
    
    outFileStream << returnStr;

    //    Crypt<caesar, no_packing, no_grouping> cnn;
    //    std::cout << cnn.encode("I AM IN LOVE WITH A FARTY SOUND", 1000) << std::endl;
    //    std::cout << cnn.decode("U MY UZ XAHQ IUFT M RMDFK EAGZP", 1000) << std::endl << std::endl;
    //    
    //    Crypt<caesar, no_packing, grouping> cng;
    //    std::cout << cng.encode("I AM IN LOVE WITH A FARTY SOUND", 1000) << std::endl;
    //    std::cout << cng.decode("UMYUZXAHQIUFTMR MDFKE AGZP", 1000) << std::endl << std::endl;
    //
    //    Crypt<vigenere, no_packing, no_grouping> vnn;
    //    std::cout << vnn.encode("I AM IN LOVE WITH A FARTY SOUND", "LEMON") << std::endl;
    //    std::cout << vnn.decode("T EY WA WSHS JTXT O SLVFM FZYZR", "LEMON") << std::endl << std::endl;
    //    
    //    Crypt<vigenere, no_packing, grouping> vng;
    //    std::cout << vng.encode("I AM IN LOVE WITH A FARTY SOUND", "LEMON") << std::endl;
    //    std::cout << vng.decode("TEYWA WSHSJ TXTOS LVFMF ZYZR", "LEMON") << std::endl << std::endl;
    //    
    //    Crypt<XOR, no_packing, no_grouping> xnn;
    //    std::cout << xnn.encode("MATTHEWWOODISACUNT", 1) << std::endl;
    //    std::cout << xnn.decode("LATTIEWWNODIRACUOT", 1) << std::endl << std::endl;
    //    
    //    Crypt<XOR, no_packing, grouping> xng;
    //    std::cout << xng.encode("MATTHEWW OODISA C**T", 1) << std::endl;
    //    std::cout << xng.decode("LATTI EWVOO EISAB UNU", 1) << std::endl << std::endl;

    return 0;
}
int main(int argc, char** argv)
{
    /*******************************************************************************************************/
    /*******************************************************************************************************/
    if(argc < 2)
    {
        cout << "Program is used to encode/decode files using AES alghoritm.\n" \
             "-c [filename]\tEncrypt file.\n" \
             "-d [filename]\tDecrypt file.\n" \
             "-o [filename]\tOutput file.\n" \
             "-p [filename]\tPlay encrypted *.wav file.\n";
        return 0;
    }
    const char* password;
    string keystore_path;
    string input_file_name;
    string output_file_name;
    int mode = 0;
    for(int i=1; i<argc; i++)
    {
        if(strcmp(argv[i], "-c") == 0)
        {
            // Encryption
            input_file_name = argv[i+1];
            mode = ENCRYPTION_MODE;
            i++;
        }
        else if(strcmp(argv[i], "-d") == 0) {
            // Decrytpion
            input_file_name = argv[i+1];
            mode = DECRYPTION_MODE;
            i++;
        }
        else if(strcmp(argv[i], "-p") == 0) {
            // Decrytpion
            input_file_name = argv[i+1];
            mode = PLAY_MODE;
            i++;
        }
        else if(strcmp(argv[i], "-o") == 0) {
            // Output file name
            output_file_name = string(argv[i+1]);
            i++;
        }
        else
        {
            cout << "Unknown parameter \"" << argv[i] << endl;
            return 1;
        }
    }
    try
    {
        cout << "Enter secred PIN: ";
        Crypt config;
        string pin = string(config.get_password());
        if(pin.length() != 4)
            return 1;
        if(pin[0] != '5' || pin[1] != '5' || pin[2] != '5' || pin[3] != '5')
            return 1;
        config.set_key(config_key);
        config.enable_decrypt_mode_memory("config.ini", AUDIOBUFFER_SIZE);
        config.decrypt_memory(0, AUDIOBUFFER_SIZE);
        int i = 0;
        for(i=0; i< AUDIOBUFFER_SIZE; i++)
        {
            if(config.buffer[i] == 13)
            {
                config.buffer[i] = 0;
                config.buffer[i+1] = 0;
                break;
            }
        }
        for(int j=i+2; i< AUDIOBUFFER_SIZE; j++)
        {
            if(config.buffer[j] == 13)
            {
                config.buffer[j] = 0;
                config.buffer[j+1] = 0;
                break;
            }
        }
        keystore_path = string((const char*)config.buffer);
        password = (const char*) (config.buffer+i+2);
    }
    catch(...) {
        cout << "Unknown problem occured\n" << endl;
        return 1;
    }
    try
    {
        Crypt c;
        c.get_key_from_keystore(keystore_path.c_str(), password);
        if(mode == ENCRYPTION_MODE)
        {
            c.enable_encrypt_mode(input_file_name.c_str(), output_file_name.c_str(), BUFFER_SIZE);
            for(int i=0; c.in.last_character != 0 || i == 0; i += BUFFER_SIZE)
            {
                c.encrypt(i, BUFFER_SIZE);
            }
        }
        else if(mode == DECRYPTION_MODE)
        {
            c.enable_decrypt_mode(input_file_name.c_str(), output_file_name.c_str(), BUFFER_SIZE);
            for(int i=10; c.in.last_character != 0 || i == 0; i += BUFFER_SIZE)
            {
                c.encrypt(i, BUFFER_SIZE);
            }
        }
        else if(mode == PLAY_MODE)
        {
            BufferPlayer b_p(c);
            c.enable_decrypt_mode_memory(input_file_name.c_str(), AUDIOBUFFER_SIZE);
            cout << "Type number between [0 ; 100] to change slider position (percentage)\n";
            cout << "You can type replay, stop, continue, exit\n";
            b_p.play();
            string cmd = "";
            cin >> cmd;
            while(cmd != "exit")
            {
                if(cmd == "replay")
                {
                    b_p.replay();
                }
                else if(cmd == "stop")
                {
                    b_p.stop();
                }
                else if(cmd == "continue")
                {
                    b_p.continue_playing();
                }
                else
                {
                    int time = atoi(cmd.c_str());
                    b_p.change_time(time);
                }
                cin >> cmd;
            }
            b_p.exit();
        }
        else
        {
            cout << "Problem with -d/-c option. Please contact with author." << endl;
        }
    }
void ImportConfig::on_cmdImport_clicked()
{
    if (!m_ui->txtExistingOvpn->text().isEmpty()) {
        // Import existing file
        QString configName (m_ui->txtExistingOvpn->text().replace(".ovpn", ""));
        configName = configName.right(configName.size() - configName.lastIndexOf("/") - 1);

        // Anderer name als Dateiname
        if (m_ui->rbSaveAsName->isChecked()) {
            if (!m_ui->txtNewName->text().isEmpty()) {
                configName = m_ui->txtNewName->text();
            }
        }

        QString pathToConfig (m_ui->txtExistingOvpn->text());
        // Copy config to local app data and rename it, if an other name is specified
        // Only in service mode!
        if(!Settings::getInstance()->getIsPortableClient()) {

            // If file is available copy it
            // First create folder
            QString newConfigFolderPath (QString("%1/config/%2")
                                         .arg(AppFunc::getAppSavePath())
                                         .arg(configName));
            QDir newConfigFolderPathDirectory (newConfigFolderPath);
            if (newConfigFolderPathDirectory.exists(newConfigFolderPath)){
                // A config with this name is already existing
                Message::error(QObject::tr("A configuration with this name is already existing!"));
                //
                return;
            }

            // Create path
            newConfigFolderPathDirectory.mkpath(newConfigFolderPath);

            // Now copy Files
            QString newConfigPath (QString("%1/%2.ovpn")
                                   .arg(newConfigFolderPath)
                                   .arg(configName));

            QFile::copy(pathToConfig, newConfigPath);
            //
            QString sourceDirectory (pathToConfig.left(pathToConfig.lastIndexOf("/")));
            //
            // Override old path
            pathToConfig = newConfigPath;
            // Ca

			auto copyConfigFile = [&](const QString &key)
			{
				auto keyValue = (ConfigValues::instance()->valueFromConfigKey(pathToConfig, key));

				if(!keyValue.isEmpty())
				{
					QString sourcePath (QString("%1/%2")
						.arg(sourceDirectory)
						.arg(keyValue));

					QString destName (keyValue);

					if (ConfigValues::instance()->isGivenPathAbsolute(keyValue)) {
						// Yes, override path
						sourcePath = keyValue;
						// Get the file name from path
						destName = ConfigValues::instance()->fileNameOfAbsolutePath(keyValue);
						// Change value in config
						ConfigValues::instance()->changeKeyValueInConfig(pathToConfig, key, QString("\"%1\"")
							.arg(destName));
					}
					// Copy
					QFile::copy(sourcePath, QString("%1/%2")
						.arg(newConfigFolderPath)
						.arg(destName));
				}
			};

			for(const auto &key : keys)
			{
				copyConfigFile(key);
			}
        }

        Preferences::instance()->addNewConfigToDatabase(configName, pathToConfig);
        Preferences::instance()->refreshConfigList();
        Preferences::instance()->setConnectionStatus();
        Preferences::instance()->setIcon();

        // Fertig
        Message::information(QObject::tr("Import successfully ended!"), QObject::tr("Import Configuration"));
        Preferences::instance()->refreshDialog();
        this->close();
    } else {
        // Import crypt file
        if (m_ui->txtPassword->text().isEmpty()) {
            Message::error(QObject::tr("No password specified!"), QObject::tr("Import Configuration"));
            return;
        }
        if (!m_ui->txtImportPath->text().isEmpty()) {
            if (m_ui->rbSaveAsName->isChecked() && m_ui->txtNewName->text().isEmpty()) {
                Message::error(QObject::tr("No import name specified!"), QObject::tr("Import Configuration"));
                return;
            }

            // Portale oder install
            QString dirPath;
            QString configName;

            if (!m_ui->rbSaveAsName->isChecked()) {
                configName = m_ui->txtImportPath->text().right(m_ui->txtImportPath->text().size() - m_ui->txtImportPath->text().lastIndexOf("/") -1);
                configName = configName.left(configName.size()-6);
            } else {
                configName = m_ui->txtNewName->text().trimmed();
            }

            dirPath = AppFunc::getAppSavePath() + QString ("/config/") + configName;

            // Verzeichnis da?
            QDir dirobj (dirPath);
            if (!dirobj.exists(dirPath)){
                //Verzeichnis existiert nicht
                // Pfad erstellen
                if (!dirobj.mkpath(dirPath)) {
                    // Pfad konnte nicht erstellt werden
                    Message::error(QObject::tr("Unable to create directory!"), QObject::tr("Import Configuration"));
                    return;
                }
            } else {
                // Verzeichnis existiert
                Message::error(QObject::tr("A diretory with this name already exists!"), QObject::tr("Import Configuration"));
                return;
            }
            // Datei ins neue Verzeichnis kopieren
            //QFile importFileCrypt (m_ui->txtImportPath->text());
            QString packFile = dirPath + QString("/") + configName + QString(".zip");

            // Erstmal entschlüsseln
            if(QFile::exists(packFile)) {
                QFile::remove(packFile);
            }

            // Die Daten einlesen
            {
                QFile crypted (m_ui->txtImportPath->text());
                if (crypted.open(QIODevice::ReadOnly)) {
                    // Nun das Ziel öfffnen
                    QFile targetFile (packFile);
                    if (targetFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
                        // Alles offen, kann losgehen
                        QByteArray data = crypted.readAll();
                        Crypt crypt;
                        crypt.setSecretKey(m_ui->txtPassword->text());
                        targetFile.write(crypt.cryptToPlainTextExt(data));
                        targetFile.waitForBytesWritten(3000);
                        targetFile.flush();
                        targetFile.waitForBytesWritten(3000);

                        // Alles Ok
                        targetFile.close();
                        crypted.close();
                    }
                } else {
                    Message::error(QObject::tr("Can't open crypted file"));
                    return;
                }
            }

            // Nun die Datei entpacken
            if (!Zip::extract(packFile, dirPath)) {
                Message::error(QObject::tr("Can't open zip file"));
                return;
            }

            // Nun ist alles entpackt, das Ziparchiv nun löschen
            QFile configZip (packFile);
            if (!configZip.remove()) {
                Message::error(configZip.errorString(), QObject::tr("Import Configuration"));
            }

            QString saveName;
            QString savePath;
            QString ovpnFilePath = m_ui->txtImportPath->text().right(m_ui->txtImportPath->text().size() - m_ui->txtImportPath->text().lastIndexOf("/") -1);

            saveName = ovpnFilePath.left(ovpnFilePath.size()-6);
            savePath = dirPath + QString("/") + ovpnFilePath.left(ovpnFilePath.size()-6) + QString(".ovpn");

            if (m_ui->rbSaveAsName->isChecked()) {
                // ovpn umbennen
                QFile ovpnFile (savePath);
                if (ovpnFile.exists()) {
                    // umbenennen
                    ovpnFile.rename(dirPath + QString("/") + configName + QString(".ovpn"));
                    saveName = configName;
                }
            }
            savePath = dirPath + QString("/") + saveName + QString(".ovpn");

            QFile ovpnFile (dirPath + QString("/") + configName + QString(".ovpn"));
            if (!ovpnFile.exists()) {
                Message::error(QObject::tr("Import failed! Removing empty directory."), QObject::tr("Import Configuration"));
                dirobj.rmdir(dirPath);
            } else {
                Preferences::instance()->addNewConfigToDatabase(saveName, savePath.replace("\\", "/"));
                Preferences::instance()->refreshConfigList();
                Preferences::instance()->setConnectionStatus();
                Message::information(QObject::tr("Import successfully ended!"), QObject::tr("Import Configuration"));
                Preferences::instance()->refreshDialog();
                Preferences::instance()->setIcon();
                this->close();
            }
        } else {
            Message::error(QObject::tr("No import file selected!"), QObject::tr("Import Configuration"));
        }
    }
}