Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Includes.h"
- void XOR_Crypt(string& toCrypt, const string& key); //simple xor (en/de)cryption
- void XOR_Encrypt(string& toEncrypt, const string& key); //more advanced xor encryption
- void XOR_Decrypt(string& toDecrypt, const string& key);
- string randKey(const size_t len); //generates a random key
- inline int getVal(const char& letter); //gets the int value of the letter; a = 0, b = 1, c = 2, etc...
- inline char getChar(int val); //gets the character of a number; 0 = a, 1 = b, 2 = c, etc...
- void help();
- void toClipboard(const std::string &s);
- void encryptFile(const string fileToEnc);
- void decryptFile(const string fileToDec);
- void saveToFile(const string fileName, const stringstream& data);
- const char g_alpha[] = { 'a', 'b', 'c', 'd','e','f','g', 'h', 'i', 'j', 'k', 'l', 'm',
- 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
- 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
- '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
- const size_t g_arrSize = sizeof(g_alpha) / sizeof(g_alpha[0]);
- string g_encStr = "";
- size_t g_txtSize = 0;
- int main(int argc, char* argv[])
- {
- if (argv[1] == 0)
- {
- cout << "Please open this program in a command prompt. Shift + Right Click -> Open command window here.\n"
- << "Press enter to exit...\n";
- getchar();
- exit(0);
- }
- if (argc == 1)
- {
- cout << "Usage: " << argv[0] << " -command\n" <<
- "Available commands can be shown with -help\n";
- }
- if (argv[1] == (string)"-encrypt")
- {
- // entered a file, whose content will be encrypted
- if (argc == 3)
- {
- ifstream tryOpen((string)argv[2]);
- if (tryOpen.fail())
- {
- cout << "\nFailed to open file!\n";
- exit(-1);
- }
- else
- { // successfully opened file
- encryptFile((string)argv[2]);
- }
- }
- encryptStr();
- }
- if (argv[1] == (string)"-decrypt")
- {
- // entered a file, whose content will be decrypted
- if (argc == 3)
- {
- ifstream tryOpen((string)argv[2]);
- if (tryOpen.fail())
- {
- cout << "\nFailed to open file!\n";
- exit(-1);
- }
- else
- { // successfully opened file
- decryptFile((string)argv[2]);
- }
- }
- decryptStr();
- }
- if (argv[1] == (string)"-help")
- help();
- }
- void encryptFile(const string fileToEnc)
- {
- ifstream inFile(fileToEnc);
- string data = "", key = "", fileName = "";
- stringstream buffer;
- if (inFile.is_open())
- {
- cout << "~ " << fileToEnc << " opened!~\n";
- buffer << inFile.rdbuf();
- data = buffer.str();
- }
- inFile.close();
- g_txtSize = data.length();
- key = randKey(g_txtSize);
- cout << "\n~~Your key is:~~\n" << key << endl;
- toClipboard(key);
- cout << "\n~Key copied to clipboard!~\n\n";
- cout << "~Encrypting...~\n";
- //XOR_Encrypt(data, key);
- XOR_Crypt(data, key);
- buffer.str(string()); // clears stringstream contents; can also do buffer.str(""), but buffer.str(string()) is technically more efficient
- buffer << data;
- cout << "What would you want your encrypted file to be called?\n";
- cin >> fileName;
- saveToFile(fileName, buffer);
- exit(0);
- }
- void decryptFile(const string fileToDec)
- {
- stringstream buffer;
- string data = "", key = "", fileName;
- ifstream inFile(fileToDec);
- if (inFile.is_open())
- {
- cout << "~ " << fileToDec << " opened!~\n";
- buffer << inFile.rdbuf();
- data = buffer.str();
- }
- inFile.close();
- cout << "\nEnter the key to decrypt " << fileToDec << " :\n";
- cin >> key;
- cout << "\n~Decrypting...~\n";
- //XOR_Decrypt(data, key);
- XOR_Crypt(data, key);
- buffer.str(string());
- buffer << data;
- cout << "What would you want your decrypted file to be called?\n";
- cin >> fileName;
- saveToFile(fileName, buffer);
- exit(0);
- }
- void saveToFile(const string fileName, const stringstream& data)
- {
- ofstream outFile(fileName);
- if (outFile.is_open())
- {
- cout << "~Saving to " << fileName << " ...~\n";
- outFile << data.rdbuf();
- cout << "~Done!~\n";
- }
- outFile.close();
- }
- void XOR_Crypt(string& toCrypt, const string& key)
- {
- for (size_t i = 0; i < toCrypt.length(); i++)
- toCrypt[i] ^= key[i];
- }
- string randKey(const size_t len)
- {
- srand(time(NULL));
- string key = "";
- for (size_t i = 0; i < len; i++)
- key += g_alpha[rand() % g_arrSize];
- return key;
- }
- inline int getVal(const char& letter)
- {
- for (int i = 0; i < g_arrSize; i++)
- {
- if (letter == g_alpha[i])
- return i;
- }
- return 0;
- }
- inline char getChar(int val)
- {
- if (val < 0)
- val += g_arrSize;
- return val > g_arrSize ? g_alpha[val % g_arrSize] : g_alpha[val];
- }
- void toClipboard(const std::string &s) { //courtesy of helios
- OpenClipboard(0);
- EmptyClipboard();
- HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size());
- if (!hg) {
- CloseClipboard();
- return;
- }
- memcpy(GlobalLock(hg), s.c_str(), s.size());
- GlobalUnlock(hg);
- SetClipboardData(CF_TEXT, hg);
- CloseClipboard();
- GlobalFree(hg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement