Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the pass... ex:
pass: 01
11110000 - file
01010101 - pass (repeated until EOF)
10100101 - resulting file
decryption would be the exact same process, but i've made it slightly more complicated. first at the beginning of the file I have a 4 bit unsigned int that is the size of the file, then a 4 byte unsigned int that is the size of the string of its file name. Then it has the actual string for the file name (the ultimate goal is so that I can have multiple files in there similar to an archive)
The problem is when I try to decrypt my console window spazzes out at me displaying a bunch of random paths like application data and what appears to be an ASCII table...
Expand|Select|Wrap|Line Numbers
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
- string getFileName(string input)
- {
- int pos = 0;
- while (input.find_first_of("/\\", pos) != string::npos)
- {
- pos = input.find_first_of("/\\", pos) + 1;
- }
- int length = input.length() - pos;
- return input.substr(pos, length);
- }
- string getPath(string input)
- {
- int pos = 0;
- while (input.find_first_of("/\\", pos) != string::npos)
- {
- pos = input.find_first_of("/\\", pos) + 1;
- }
- return input.substr(0, pos);
- }
- int main()
- {
- //GET USER INPUT
- string filestr, passstr, outputstr, action, filename;
- cout << "Action (package, extract): ";
- getline (cin, action);
- if (action == "package")
- {
- cout << "Path to file: ";
- getline (cin, filestr);
- cout << "Pass: ";
- getline (cin, passstr);
- /*cout << "Output File: ";
- getline (cin, outputstr);*/
- filename = getFileName(filestr);
- outputstr = filestr + ".craft";
- char *file = new char[filestr.length()+1];
- strcpy(file, filestr.c_str());
- char *pass = new char[passstr.length()+1];
- strcpy(pass, passstr.c_str());
- char *output = new char[outputstr.length()+1];
- strcpy(output, outputstr.c_str());
- char buffer[sizeof(pass)];
- ifstream myFile(file, ios::binary);
- ofstream outFile(output, ios::binary);
- FILE * stream = fopen( file, "r" );
- fseek( stream, 0L, SEEK_END );
- unsigned int endPos = ftell( stream );
- fclose( stream );
- //WRITE SIZE OF FILE
- outFile.write((char *)(&endPos), 4);
- //WRITE SIZE OF NAME
- unsigned int sizeofname = filename.length();
- outFile.write((char *)(&sizeofname), 4);
- //DEBUG
- //cout << endPos << endl << sizeofname << endl << sizeof(unsigned int) << endl;
- //WRITE NAME OF FILE
- outFile.write((char *)(&filename), sizeofname);
- //cout << filename << endl;
- while (!myFile.eof())
- {
- //cout << "endPos: " << endPos << "Size of pass: " << sizeof(pass) << endl;
- if (endPos < sizeof(pass))
- {
- myFile.read (buffer, sizeof(pass));
- for (unsigned int x = 0; x < endPos; x++)
- {
- char write = buffer[x] ^ pass[x];
- outFile.write(&write, 1);
- }
- } else {
- myFile.read (buffer, sizeof(pass));
- for (unsigned int x = 0; x < sizeof(pass); x++)
- {
- char write = buffer[x] ^ pass[x];
- outFile.write(&write, 1);
- }
- endPos -= sizeof(pass);
- }
- }
- myFile.close();
- outFile.close();
- }
- if (action == "extract")
- {
- cout << "Path to file: ";
- getline (cin, filestr);
- cout << "Pass: ";
- getline (cin, passstr);
- char *file = new char[filestr.length()+1];
- strcpy(file, filestr.c_str());
- char *pass = new char[passstr.length()+1];
- strcpy(pass, passstr.c_str());
- char buffer[sizeof(pass)];
- ifstream myFile(file, ios::binary);
- unsigned int sizeoffile, sizeofname;
- string name = "";
- myFile.read ((char *)(&sizeoffile), 4);
- myFile.read ((char *)(&sizeofname), 4);
- cout << sizeofname << endl;
- cout << &sizeofname << endl;
- myFile.read ((char *)(&name), sizeofname);
- cout << sizeoffile << endl << sizeofname << endl << name << endl;
- outputstr = "";
- outputstr = getPath(filestr) + name;
- char *output = new char[outputstr.length()+1];
- strcpy(output, outputstr.c_str());
- cout << "Output: " << output << endl;
- ofstream outFile(output, ios::binary);
- unsigned int pos = 0;
- while (pos < sizeoffile)
- {
- //cout << "pos: " << pos << "sizeoffile: " << sizeoffile << endl;
- unsigned int endPos = sizeoffile - pos;
- if (endPos < sizeof(pass))
- {
- myFile.read (buffer, sizeof(pass));
- for (unsigned int x = 0; x < endPos; x++)
- {
- char write = buffer[x] ^ pass[x];
- outFile.write(&write, 1);
- }
- pos += endPos;
- } else {
- myFile.read (buffer, sizeof(pass));
- for (unsigned int x = 0; x < sizeof(pass); x++)
- {
- char write = buffer[x] ^ pass[x];
- outFile.write(&write, 1);
- }
- pos += sizeof(pass);
- }
- }
- myFile.close();
- outFile.close();
- }
- return 0;
- }