#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<time.h>
#include<sys/types.h>
#include<getopt.h>
void help(char *argv);
int main(int argc , char **argv)
{
int sourcefd , destfd;
char cc;
unsigned char in , out;
int rc , ch , key;
time_t time_var;
char *source = NULL , *dest = NULL , *key_name = NULL;
struct option long_op[]=
{
{"help" , 0 , NULL , 'h'},
{"source" , 1 , NULL , 's'},
{"dest" , 1 , NULL ,'d'},
{"key" , 1 , NULL , 'k'},
{0,0,0,0}
};
srand((unsigned) time(&time_var));
if(argc < 4)
{
help(argv[0]);
_exit(0);
}
while((ch = getopt_long(argc , argv , "hs:d:k:" , long_op , NULL)) != -1)
{
switch (ch)
{
case 'h':
help(argv[0]);
_exit(0);
break;
case 's':
source = optarg;
break;
case 'd':
dest = optarg;
break;
case 'k':
key_name= optarg;
break;
default:
printf("[-]Undefined switch <%c>\n",ch);
_exit(1);
break;
}
}
if( strstr(key_name ,"SET") != NULL )
{
printf("[1;34;40m\n");
printf("Using Random Number \n");
key = rand();
printf("Your Key Is %d\n",key);
}else
{
key = atoi(key_name);
printf("Your Key is %d\n",key);
}
if(access(source , R_OK) == -1)
{
printf("[5;31;40m\n");
printf("ACCESS DENIED\n");
printf("[0;37;37m\n");
_exit(1);
}
if((sourcefd = open(source , O_RDONLY)) < 0)
{
printf("Cannot Open File\n");
_exit(1);
}
if(access(dest , F_OK) == 0)
{
printf("Dest file already exist over write(y/n):\n");
scanf("%c",&cc);
switch(cc)
{
case 'y':
case 'Y':
if((destfd= open(dest , O_RDWR | O_CREAT | O_TRUNC)) < 0)
{
printf("Cannot Open Dest File\n");
_exit(0);
}
break;
case 'n':
case 'N':
_exit(0);
break;
}
}
else
{
if((destfd = open(dest , O_RDWR | O_CREAT | O_TRUNC )) < 0)
{
printf("Cannot Open Dest File\n");
_exit(1);
}
}
printf("[+1] Ready To Encrypt\n");
while((rc = read(sourcefd , &in , 1)) != 0)
{
out = key ^ in;
write(destfd , &out , 1);
}
printf("[+2] Encryption/Decryption Complete\n");
close(sourcefd);
close(destfd);
return 0;
}
void help(char *argv)
{
fprintf(stderr , "usage [+]<%s> -s <source> -d <dest> -k <key>\n",argv);
}
اكتشف بنفسك اذاي تشغلها xD