Advertisement
dimon2242

JustForFun

Jun 22nd, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6.  
  7. #define PERMISSIONS 0666
  8. #define MASK 0b1111000011110000
  9. #define MODE_ENCRYPT 1
  10. #define MODE_DECRYPT 0
  11.  
  12. void encryptData(char* data, size_t size);
  13. void decryptData(char* data, size_t size);
  14.  
  15. int main(int argc, char** argv) {
  16.   if(argc < 3) {
  17.     perror("Need more than 1 args");
  18.     return EXIT_FAILURE;
  19.   }
  20.   int mode = MODE_ENCRYPT;
  21.   if(argc >= 4) {
  22.     if(strcmp(argv[3], "-d")) {
  23.       mode = MODE_DECRYPT;
  24.     }
  25.   }
  26.  
  27.   char buffer[4096];
  28.   ssize_t cread, cwrite;
  29.   int fd_input, fd_output;
  30.   char* filename_output;
  31.   char* output_ptr;
  32.   filename_output = argv[2];
  33.   if(filename_output == NULL) {
  34.     filename_output = argv[1];
  35.   }
  36.   if((fd_input = open(argv[1], O_RDONLY)) < 0) {
  37.     perror("Error open input file!");
  38.     return EXIT_FAILURE;
  39.   }
  40.   if((fd_output = open(filename_output, O_CREAT | O_WRONLY | O_TRUNC, PERMISSIONS)) < 0) {
  41.     perror("Error open output file!");
  42.     return EXIT_FAILURE;
  43.   }
  44.   output_ptr = buffer;
  45.   while((cread = read(fd_input, buffer, sizeof buffer)) > 0) {
  46.     mode ? encryptData(buffer, sizeof buffer) : decryptData(buffer, sizeof buffer);
  47.     if((cwrite = write(fd_output, buffer, cread)) < 0) {
  48.       perror("IO error!");
  49.       return EXIT_FAILURE;
  50.     }
  51.   }
  52.     close(fd_input);
  53.     close(fd_output);
  54. }
  55.  
  56. void encryptData(char* data, size_t size) {
  57.   for(register int i = 0; i < size; i++) {
  58.     data[i] ^= MASK;
  59.   }
  60. }
  61.  
  62. void decryptData(char* data, size_t size) {
  63.   for(register int i = 0; i < size; i++) {
  64.     data[i] ^= MASK;
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement