Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #include <cstdio>
  2. static bool consoleOutput = false;
  3.  
  4. int Encrypt(const char* file_in, const char* keyFile_in, const char* file_out);
  5.  
  6. int main(int argc, char** argv)
  7. {
  8. if(argc == 1)
  9. {
  10. printf("nExecute with %s -h for more informationn", argv[0]);
  11. return 0;
  12. }
  13. else if(argc == 2)
  14. {
  15. if((argv[1][0] == '-') && (argv[1][1] == 'h'))
  16. {
  17. printf("nUso: %s "in file" "file with key" "out file"n", argv[0]);
  18. return 0;
  19. }
  20. }
  21. Encrypt(argv[1], argv[2], argv[3]);
  22.  
  23. return 0;
  24. }
  25.  
  26. int Encrypt(const char* file_in, const char* keyFile_in, const char* file_out)
  27. {
  28. FILE* mainFile = nullptr;
  29. FILE* keyFile = nullptr;
  30. FILE* outFile = nullptr;
  31. char* inBuffer = nullptr;
  32. char key[100];// = "mZq4t7w!";
  33. int mainFileSize = 0;
  34.  
  35. mainFile = fopen(file_in, "rb");
  36.  
  37. if(mainFile == nullptr)
  38. {
  39. printf("Couldn't open file %s!", file_in);
  40. return -1;
  41. }
  42.  
  43. fseek(mainFile, 0, SEEK_END);
  44. mainFileSize = ftell(mainFile);
  45. rewind(mainFile);
  46.  
  47. if(mainFileSize <= 1)
  48. {
  49. printf("File is empty or the file is really small (not worthy)");
  50. return -2;
  51. }
  52.  
  53. inBuffer = new char[mainFileSize];
  54.  
  55. if(inBuffer == nullptr)
  56. {
  57. printf("Couldn't allocate %d bytes of memory", mainFileSize);
  58. return -3;
  59. }
  60.  
  61. fread(inBuffer,sizeof(char), mainFileSize, mainFile);
  62.  
  63. if(consoleOutput)
  64. {
  65. for(int i = 0; i < mainFileSize; i++)
  66.  
  67. {
  68. putchar(inBuffer[i]);
  69. }
  70. puts("");
  71. }
  72.  
  73. fclose(mainFile);
  74.  
  75. // read key file
  76. keyFile = fopen(keyFile_in, "rb");
  77. if(keyFile == nullptr)
  78. {
  79. printf("Couldn't open file %s!", keyFile_in);
  80. return -1;
  81. }
  82.  
  83. fseek(keyFile, 0, SEEK_END);
  84. const int keyFileSize = ftell(keyFile);
  85. rewind(keyFile);
  86.  
  87. fread(key, sizeof(char), keyFileSize, keyFile);
  88.  
  89. if(consoleOutput)
  90. {
  91. for(int i = 0; i < keyFileSize; i++)
  92. {
  93. putchar(key[i]);
  94. }
  95. printf("nSize: %i", keyFileSize);
  96. }
  97.  
  98. fclose(keyFile);
  99.  
  100. // output decryption/encryption
  101. puts("ntStarting to do the magicn");
  102.  
  103. for(int i = 0; i < mainFileSize; ++i)
  104. {
  105. inBuffer[i] = inBuffer[i] ^ key[i%keyFileSize-1];
  106. }
  107.  
  108. if(consoleOutput)
  109. {
  110. for(int i = 0; i < mainFileSize; i++)
  111. {
  112. putchar(inBuffer[i]);
  113. }
  114. puts("");
  115. }
  116.  
  117. // open output file
  118. outFile = fopen(file_out, "wb");
  119. if(outFile == nullptr)
  120. {
  121. printf("Couldn't open file %s!", file_out);
  122. return -1;
  123. }
  124.  
  125. fwrite(inBuffer, sizeof(char), mainFileSize, outFile);
  126.  
  127. fclose(outFile);
  128.  
  129. delete[] inBuffer;
  130. puts("Finished!");
  131.  
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement