Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int main( int argc, char *argv[])
- {
- int pindex=0;
- long lsize=0,i=0;
- char password[1024];
- int plen;
- char filename[64];
- char nfilename[64];
- if(argc!=4) // there should be exavtly 4 parameters including tar2
- {
- printf("Not enough arguments!\n");
- return 0;
- }
- strcpy(filename,argv[1]);
- FILE* f = fopen(filename, "rb"); // opening file for reading
- fseek(f,0,SEEK_END);
- lsize = ftell(f);
- rewind(f);
- // taking argument [3] as the password
- strcpy(password,argv[3]);
- plen = strlen(password);
- unsigned char xored;
- // for encrypting
- if(strcmp(argv[2],"-e")==0)
- {
- printf("encrypting the file %s \n", filename);
- strcat(filename,".tar2");
- FILE* fout = fopen(filename, "wb");
- while(i<lsize){
- unsigned char first = fgetc(f); // reading byte from file
- i++;
- xored = first^(password[pindex]);
- fputc(xored,fout); // writing to output file
- pindex = (pindex+1)%plen;
- }
- fclose(fout);
- }
- // for decrypting
- if(strcmp(argv[2],"-d")==0)
- {
- printf("decrypting the file %s \n", filename);
- int file_len = strlen(filename);
- int i;
- pindex = plen-1;
- for(i=0; i<file_len-5; i++)
- {
- nfilename[i] = filename[i];
- }
- nfilename[i]='\0';
- FILE* fout = fopen(nfilename, "wb");
- while(i<lsize){
- unsigned char first = fgetc(f);
- i++;
- xored = first^password[pindex];
- fputc(xored,fout);
- pindex = pindex-1;
- if(pindex<0)
- {
- pindex = plen-1;
- }
- }
- fclose(fout);
- }
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement