Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- static bool consoleOutput = false;
- int Encrypt(const char* file_in, const char* keyFile_in, const char* file_out);
- int main(int argc, char** argv)
- {
- if(argc == 1)
- {
- printf("nExecute with %s -h for more informationn", argv[0]);
- return 0;
- }
- else if(argc == 2)
- {
- if((argv[1][0] == '-') && (argv[1][1] == 'h'))
- {
- printf("nUso: %s "in file" "file with key" "out file"n", argv[0]);
- return 0;
- }
- }
- Encrypt(argv[1], argv[2], argv[3]);
- return 0;
- }
- int Encrypt(const char* file_in, const char* keyFile_in, const char* file_out)
- {
- FILE* mainFile = nullptr;
- FILE* keyFile = nullptr;
- FILE* outFile = nullptr;
- char* inBuffer = nullptr;
- char key[100];// = "mZq4t7w!";
- int mainFileSize = 0;
- mainFile = fopen(file_in, "rb");
- if(mainFile == nullptr)
- {
- printf("Couldn't open file %s!", file_in);
- return -1;
- }
- fseek(mainFile, 0, SEEK_END);
- mainFileSize = ftell(mainFile);
- rewind(mainFile);
- if(mainFileSize <= 1)
- {
- printf("File is empty or the file is really small (not worthy)");
- return -2;
- }
- inBuffer = new char[mainFileSize];
- if(inBuffer == nullptr)
- {
- printf("Couldn't allocate %d bytes of memory", mainFileSize);
- return -3;
- }
- fread(inBuffer,sizeof(char), mainFileSize, mainFile);
- if(consoleOutput)
- {
- for(int i = 0; i < mainFileSize; i++)
- {
- putchar(inBuffer[i]);
- }
- puts("");
- }
- fclose(mainFile);
- // read key file
- keyFile = fopen(keyFile_in, "rb");
- if(keyFile == nullptr)
- {
- printf("Couldn't open file %s!", keyFile_in);
- return -1;
- }
- fseek(keyFile, 0, SEEK_END);
- const int keyFileSize = ftell(keyFile);
- rewind(keyFile);
- fread(key, sizeof(char), keyFileSize, keyFile);
- if(consoleOutput)
- {
- for(int i = 0; i < keyFileSize; i++)
- {
- putchar(key[i]);
- }
- printf("nSize: %i", keyFileSize);
- }
- fclose(keyFile);
- // output decryption/encryption
- puts("ntStarting to do the magicn");
- for(int i = 0; i < mainFileSize; ++i)
- {
- inBuffer[i] = inBuffer[i] ^ key[i%keyFileSize-1];
- }
- if(consoleOutput)
- {
- for(int i = 0; i < mainFileSize; i++)
- {
- putchar(inBuffer[i]);
- }
- puts("");
- }
- // open output file
- outFile = fopen(file_out, "wb");
- if(outFile == nullptr)
- {
- printf("Couldn't open file %s!", file_out);
- return -1;
- }
- fwrite(inBuffer, sizeof(char), mainFileSize, outFile);
- fclose(outFile);
- delete[] inBuffer;
- puts("Finished!");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement