Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <fcntl.h>
- #define PERMISSIONS 0666
- #define MASK 0b1111000011110000
- #define MODE_ENCRYPT 1
- #define MODE_DECRYPT 0
- void encryptData(char* data, size_t size);
- void decryptData(char* data, size_t size);
- int main(int argc, char** argv) {
- if(argc < 3) {
- perror("Need more than 1 args");
- return EXIT_FAILURE;
- }
- int mode = MODE_ENCRYPT;
- if(argc >= 4) {
- if(strcmp(argv[3], "-d")) {
- mode = MODE_DECRYPT;
- }
- }
- char buffer[4096];
- ssize_t cread, cwrite;
- int fd_input, fd_output;
- char* filename_output;
- char* output_ptr;
- filename_output = argv[2];
- if(filename_output == NULL) {
- filename_output = argv[1];
- }
- if((fd_input = open(argv[1], O_RDONLY)) < 0) {
- perror("Error open input file!");
- return EXIT_FAILURE;
- }
- if((fd_output = open(filename_output, O_CREAT | O_WRONLY | O_TRUNC, PERMISSIONS)) < 0) {
- perror("Error open output file!");
- return EXIT_FAILURE;
- }
- output_ptr = buffer;
- while((cread = read(fd_input, buffer, sizeof buffer)) > 0) {
- mode ? encryptData(buffer, sizeof buffer) : decryptData(buffer, sizeof buffer);
- if((cwrite = write(fd_output, buffer, cread)) < 0) {
- perror("IO error!");
- return EXIT_FAILURE;
- }
- }
- close(fd_input);
- close(fd_output);
- }
- void encryptData(char* data, size_t size) {
- for(register int i = 0; i < size; i++) {
- data[i] ^= MASK;
- }
- }
- void decryptData(char* data, size_t size) {
- for(register int i = 0; i < size; i++) {
- data[i] ^= MASK;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement