Example #1
0
/**
 * Decrypt received facial recognition parameters.
 * Encrypt facial recognition parameters.
 */
bool replyToThirdMessage() {
    cout << "Reply To Third Message" << endl;
    // Read session key from file
    SecByteBlock key;
    readFromFile(COMPUTED_KEY_FILE_NAME, key);

    // Read in the current initialization vector from file
    byte curIv[AES::BLOCKSIZE];
    // TODO: actually read it in
    // Set to 0 for now
    memset(curIv, 0, AES::BLOCKSIZE);

    // Decrypt received facial recognition params
    if (!decryptFile(THIRD_MESSAGE_FILE_NAME, 
            RECEIVED_FACIAL_RECOGNITION_FILE_NAME,
            key, curIv)) {
        cerr << "Security Error in replyToThirdMessage. MAC could not be verified." << endl;
        return false;
    }

    // Encrypt facial recognition params
    encryptFile(FACIAL_RECOGNITION_FILE_NAME, 
            THIRD_MESSAGE_REPLY_FILE_NAME, 
            key, curIv);
    return true;
}
Example #2
0
void Thread::run()
{
    qDebug()<<"run";
    if (l_source_files.size()!=l_destination_files.size())
    {
        exec();
        emit processFinished(encrypt);
        return;
    }

    if (encrypt)
    {
         //encryptFile(v.toString().toLatin1().data(),dest_path.toLatin1().data(),text.toLatin1().data());
        for (int i = 0; i< l_source_files.size(); i++)
        {
            emit setLabel("Encrypt: " + *l_source_files[i]);
            encryptFile(l_source_files[i]->toLatin1().data(), l_destination_files[i]->toLatin1().data(), password.toLatin1().data());
        }
    }
    else
    {
        for (int i = 0; i<l_source_files.size(); i++)
        {
            emit setLabel("Decrypt: " + *l_source_files[i]);
            decryptFile(l_source_files[i]->toLatin1().data(), l_destination_files[i]->toLatin1().data(), password.toLatin1().data());
        }
    }
    emit setLabel("");
    emit processFinished(encrypt);
    exec();
}
Example #3
0
// Sets the color of the button to green if it was clicked and acts accordingly.
void RunManager::handleClick(string key) {
    // If ECB makes button green and sets the cipher type accordingly.
    if (key == "ECB") {
        cipherType = 0;
        ECBButton.setColor(sf::Color(0, 255, 0));
        ECBText.setColor(sf::Color(0, 255, 0));
    }
    // Same but for CBC.
    if (key == "CBC") {
        cipherType = 1;
        CBCButton.setColor(sf::Color(0, 255, 0));
        CBCText.setColor(sf::Color(0, 255, 0));
    }
    // Etc.
    if (key == "CTR") {
        cipherType = 2;
        CTRButton.setColor(sf::Color(0, 255, 0));
        CTRText.setColor(sf::Color(0, 255, 0));
    }
    
    // If the encrypt button was clicked the program will encrypt the file based on the location given.
    if (key == "encrypt")
        encryptFile();
    // If the decrypt button was clicked.
    else if (key == "decrypt")
        decryptFile();
    
    // If encrypt file location was clicked.
    if (key == "eFile") {
        // Informs program the user is typing in the encryption file location.
        typing = 1;
        // Sets encryption file location color.
        encryptFileSelector.setColor(sf::Color(0, 255, 0));
        encryptFileLocationText.setColor(sf::Color(0, 255, 0));
        
        decryptFileSelector.setColor(sf::Color(255, 255, 255));
        decryptFileLocationText.setColor(sf::Color(0, 0, 0));
    }
    // Same but for decryption file location.
    else if (key == "dFile") {
        typing = 2;
        decryptFileSelector.setColor(sf::Color(0, 255, 0));
        decryptFileLocationText.setColor(sf::Color(0, 255, 0));
        
        encryptFileSelector.setColor(sf::Color(255, 255, 255));
        encryptFileLocationText.setColor(sf::Color(0, 0, 0));
    }
    
    // If the user was typing but selects a button other than the two file locations, it unhighlights them both and infroms the program the user has stopped typing.
    if (typing && key != "eFile" && key != "dFile") {
        typing = 0;
        encryptFileSelector.setColor(sf::Color(255, 255, 255));
        encryptFileLocationText.setColor(sf::Color(0, 0, 0));
        decryptFileSelector.setColor(sf::Color(255, 255, 255));
        decryptFileLocationText.setColor(sf::Color(0, 0, 0));
    }
}
int main()
{
	char sourceFileName[MAXLENGTH];
	int encryptFileLength = MAXLENGTH + 11;
	char encryptFileName[encryptFileLength];
	char decryptFileName[encryptFileLength];
	int encryptFile(char fileName[],char encryptFileName[]);
	int decryptFile(char encryptfileName[],char decryptFileName[]);
	
	printf("Enter the name of file\n");
	scanf("%s",sourceFileName);

	if (strlen(sourceFileName) > MAXLENGTH-1 || strlen(sourceFileName) < 1 )
	{
		printf("File name must be within range of 1 to %d\n",MAXLENGTH-1);
		return 0;
	}

	sprintf(encryptFileName,"%s_encrypted",sourceFileName);

	if (strlen(encryptFileName) > encryptFileLength-1 || strlen(encryptFileName) < 1 )
	{
		printf("File name must be within range of 1 to %d\n",encryptFileLength-1);
		return 0;
	}

	sprintf(decryptFileName,"%s_decrypted",sourceFileName);

	if (strlen(decryptFileName) > encryptFileLength-1 || strlen(decryptFileName) < 1 )
	{
		printf("File name must be within range of 1 to %d\n",encryptFileLength-1);
		return 0;
	}

	if (encryptFile(sourceFileName,encryptFileName))
	{
		if (!decryptFile(encryptFileName,decryptFileName))
		{
			printf("File decryption failed \n");
			return 0;
		}
	}
	else
	{
		printf("File encryption failed \n");
		return 0;
	}
	
	return 1;
}
Example #5
0
// main is a menu method
int main (int argc, char *argv[]) {
  int status = 0;
  if (argv[1] != NULL
          && (strcmp(argv[1], "-e")
          == 0 || strcmp(argv[1], "-d")
          == 0 || strcmp(argv[1], "-h")
          == 0)) {
    if (argv[2] != NULL && argv[3] != NULL && argv[4] != NULL) {
      if (strcmp(argv[1], "-e") == 0) {
        // check for distance, defaults to 0.
        if (argc == 6) {
          status = encryptFile("%t %k %o %d", argv[3], argv[2], argv[4],
                  atoi(argv[5]));
        } else {
          status = encryptFile("%t %k %o", argv[3], argv[2], argv[4]);
        }
      } else if (strcmp(argv[1], "-d") == 0) {
        status = decryptFile(argv[3], argv[2], argv[4]);
      } else if (strcmp(argv[1], "-h") == 0) {
        if (argv[5] != NULL) {
          status = hackFile(argv[2], argv[3], argv[4], argv[5]);
        } else {
          printf("\nCorrect format is \"assignment2 -h <(targetfile).txt>");
          printf(" <(outputFile).txt> <keyDir> <DictionaryDir>\"\n\n");
          status = 0;
        }
      }
    } else {
      printf("\nCorrect format is \"assignment2 -[edh] "
              "<(keyfile).txt)|(targetFile).txt>");
      printf(" <(targetfile).txt|(outputFile).txt> "
              "<(outputFile).txt|keyDir> [distance|<dictionaryDir>]\"\n\n");
      status = 0;
    }
  } else {
    printf("\nNot recognized control. \"-[edh]\"\n\n");
    status = 0;
  }

  if (statusMessage != NULL) {
    printf("%s", statusMessage);
  }

  return status;
}
Example #6
0
/* Download the file with the given name from the server.  If decrypt is 0
 * then the file will not be decrypted, and will be left in TEMP_ENCRYPTED_FILENAME.
 * If decrypt is not 0 then the file will be decrypted according to the clients
 * records and saved under its original name. */
int clientDownloadFile(BIO *conn, char *filename, int decrypt)  {
	//trigger the server to call serverDownloadFile()
	if(writeInt(conn, DOWNLOAD_FILE_CODE) == -1) return -1;
	
	//send the server the filename we want to download
	if ( writeString(conn, filename) < 1 ) return -1;

	int status = readInt(conn);
	if(status == -1) {
		printf("No such file %s\n", filename);
		return 0;
	}
	
	//receive the encrypted file, save to temporary file
	status = recvFile(conn, TEMP_ENCRYPTED_FILENAME);
	if ( status < 0 ) return -1;
	if ( status == 5 ) return 5;

	//if no decryption requested quit early
	if(!decrypt) return 0; 
	

	//otherwise check our records to decrypt this file
	FILERECORD *r = getRecord(filename);
	if(r == NULL) {
		printf("No record stored for this file, so no decryption performed.\n");
		return -1;
	}
	
	//decrypt the file, and save with it's original name
	status = decryptFile(TEMP_ENCRYPTED_FILENAME, filename, r->key, r->iv);
	if ( status < 0 ) return -1;

	unlink(TEMP_ENCRYPTED_FILENAME);
	printf("Successfully downloaded %s\n", filename);
	
	return 0;
	
}
Example #7
0
int main(int argc,char **argv)
{
    if(argc>2)
    {
        switch(decryptFile(argv[1],argv[2]))
        {
        case -1:
            printf("Error opening File %s\r\n",argv[1]);
            break;
        case -2:
            printf("Error opening File %s\r\n",argv[2]);
            break;
        default:
            printf("Password removed\r\nDo the folowing now:\r\n");
            printf("1. Open the excel sheet and press [ALT]+[F11]\r\n");
            printf("   Confirm any errors that might appear\r\n");
            printf("2. go to Tools > VBA project properties.\r\n");
            printf("3. In the Tab \"Protection\" enter any password\r\n");
            printf("   Do not clear the Checkbox!\r\n");
            printf("4. Save, close the Editor, save the excel sheet and close it\r\n");
            printf("5. Open it again\r\n");
            printf("6. Repear step 1 and 2, there should be no errors\r\n");
            printf("7. Clear the password checkbox in the \"protection\" tab\r\n");
            printf("8. Repeat Steps 4 and 5.\r\n");
            printf("9. Your password is gone!\r\n");
            printf("\r\n");
            printf("/u/AyrA_ch\r\n");
            printf("\r\n");
            break;
        }
    }
    else
    {
        printf("excelDecrypt <input-filename> <output-filename>");
    }
    printf("\r\n");
    return 0;
}
Example #8
0
int main( int argc, char *argv[] )
{
	std::string inputfile  = "";
	std::string outputfile = "";
	std::string passphrase = "";
	std::string publicKey  = "";
	bool encrypt = false,
	     decrypt = false,
	     keygen  = false;

	// set up command line options and
	// check for help requests and usage errors
	po::variables_map var_map;

	try
	{
		init_program_options(argc, argv, &var_map);
	}
	catch( po::multiple_occurrences m )
	{
		std::cerr << "Error: Please use each options at most once."
			  << std::endl << std::endl;
		print_usage( argv[0] );
		exit( 2 );
	}
	catch( ... )
	{
		std::cerr << "Error: unknown error in command line options."
			  << std::endl << std::endl;
		print_usage( argv[0] );
		exit( 2 );
	}

	// no options -> print usage message
	if( argc == 1 )
	{
		print_usage( argv[0] );
		exit( 1 );
	}

	// check command line options
	if( var_map.count("test") > 0 )
	{
		exit( run_tests() );
	}

	encrypt = (var_map.count("encrypt")    > 0);
	decrypt = (var_map.count("decrypt")    > 0);
	keygen  = (var_map.count("create-key") > 0);

	// check that the user only request one out of decrypt/encrypt/keygen
	if( (int)decrypt + (int)encrypt + (int)keygen > 1 )
	{
		std::cerr << "You need to chose one of encrypt, decrypt and key-gen."
			  << std::endl << std::endl;
		print_usage( argv[0] );
		exit( 2 );
	}

	if (var_map.count("pass")   > 0) passphrase = var_map["pass"].as<std::string>();
	if (var_map.count("input")  > 0) inputfile  = var_map["input"].as<std::string>();
	if (var_map.count("output") > 0) outputfile = var_map["output"].as<std::string>();
	if (var_map.count("key")    > 0) publicKey  = var_map["key"].as<std::string>();
	quiet = (var_map.count("quiet") > 0);

	// here comes the program logic
	if( keygen )
	{
		if( passphrase == "" )
			passphrase = prompt_password( "passphrase", true );
	
		uint8_t publicKey[32];
		if( generate_key( publicKey, passphrase ) )
			std::cerr << "Public key: " << b64encode( publicKey );
		else
			std::cerr << "Failed to generate public key" << std::endl;
	}
	else if( encrypt )
	{
		if( inputfile == "" && publicKey == "" )
		{
			std::cerr << "Error: Public key needs to be given on "
				  << "commandline when encrypting from stdin" << std::endl;
			exit( 1 );
		}

		if( publicKey == "" )
			publicKey  = prompt_password( "public key" );

		encryptFile( outputfile, inputfile, passphrase, publicKey );
	}
	else if( decrypt )
	{
		if( inputfile == "" && passphrase == "" )
		{
			std::cerr << "Error: passphrase needs to be given on "
				  << "commandline when decrypting from stdin" << std::endl;
			exit( 1 );
		}

		if( passphrase == "" )
			passphrase = prompt_password( "passphrase", true );

		decryptFile( outputfile, inputfile, passphrase );
	}
	else
	{
		print_usage( argv[0] );
		exit( 0 );
	}

	return 0;
}
Example #9
0
bool EncryptedStore::openRead(const QString& name)
{
    Q_D(KOdfStore);
    if (bad())
        return false;

    const KArchiveEntry* fileArchiveEntry = m_pZip->directory()->entry(name);
    if (!fileArchiveEntry) {
        return false;
    }
    if (fileArchiveEntry->isDirectory()) {
        kWarning(30002) << name << " is a directory!";
        return false;
    }
    const KZipFileEntry* fileZipEntry = static_cast<const KZipFileEntry*>(fileArchiveEntry);

    delete d->stream;
    d->stream = fileZipEntry->createDevice();
    d->size = fileZipEntry->size();
    if (m_encryptionData.contains(name)) {
        // This file is encrypted, do some decryption first
        if (m_bPasswordDeclined) {
            // The user has already declined to give a password
            // Open the file as empty
            d->stream->close();
            delete d->stream;
            d->stream = new QBuffer();
            d->stream->open(QIODevice::ReadOnly);
            d->size = 0;
            return true;
        }
        QCA::SecureArray encryptedFile(d->stream->readAll());
        if (encryptedFile.size() != d->size) {
            // Read error detected
            d->stream->close();
            delete d->stream;
            d->stream = NULL;
            kWarning(30002) << "read error";
            return false;
        }
        d->stream->close();
        delete d->stream;
        d->stream = NULL;
        KoEncryptedStore_EncryptionData encData = m_encryptionData.value(name);
        QCA::SecureArray decrypted;

        // If we don't have a password yet, try and find one
        if (m_password.isEmpty()) {
            findPasswordInKWallet();
        }

        while (true) {
            QByteArray pass;
            QCA::SecureArray password;
            bool keepPass = false;
            // I already have a password! Let's test it. If it's not good, we can dump it, anyway.
            if (!m_password.isEmpty()) {
                password = m_password;
                m_password = QCA::SecureArray();
            } else {
                if (!m_filename.isNull())
                    keepPass = false;
                QPointer<KPasswordDialog> dlg = new KPasswordDialog(d->window , 
				keepPass ? KPasswordDialog::ShowKeepPassword : static_cast<KPasswordDialog::KPasswordDialogFlags>(0));
                dlg->setPrompt(i18n("Please enter the password to open this file."));
                if (! dlg->exec()) {
                    m_bPasswordDeclined = true;
                    d->stream = new QBuffer();
                    d->stream->open(QIODevice::ReadOnly);
                    d->size = 0;
		    delete dlg;
                    return true;
                }
		if (dlg) {
                    password = QCA::SecureArray(dlg->password().toUtf8());
                    if (keepPass)
                        keepPass = dlg->keepPassword();
                    if (password.isEmpty()) {
		        delete dlg;
                        continue;
                    }
		}
		delete dlg;
            }

            decrypted = decryptFile(encryptedFile, encData, password);
            if (decrypted.isEmpty()) {
                kError(30002) << "empty decrypted file" << endl;
                return false;
            }

            if (!encData.checksum.isEmpty()) {
                QCA::SecureArray checksum;
                if (encData.checksumShort && decrypted.size() > 1024) {
                    // TODO: Eww!!!! I don't want to convert via insecure arrays to get the first 1K characters of a secure array <- fix QCA?
                    checksum = QCA::Hash("sha1").hash(QCA::SecureArray(decrypted.toByteArray().left(1024)));
                } else {
                    checksum = QCA::Hash("sha1").hash(decrypted);
                }
                if (checksum != encData.checksum) {
                    continue;
                }
            }

            // The password passed all possible tests, so let's accept it
            m_password = password;
            m_bPasswordUsed = true;

            if (keepPass) {
                savePasswordInKWallet();
            }

            break;
        }

        QByteArray *resultArray = new QByteArray(decrypted.toByteArray());
        QIODevice *resultDevice = KFilterDev::device(new QBuffer(resultArray, NULL), "application/x-gzip");
        if (!resultDevice) {
            delete resultArray;
            return false;
        }
        static_cast<KFilterDev*>(resultDevice)->setSkipHeaders();
        d->stream = resultDevice;
        d->size = encData.filesize;
    }
    d->stream->open(QIODevice::ReadOnly);

    return true;
}