Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main( int argc, char *argv[])
  5. {
  6. int pindex=0;
  7. long lsize=0,i=0;
  8. char password[1024];
  9. int plen;
  10. char filename[64];
  11. char nfilename[64];
  12.  
  13. if(argc!=4) // there should be exavtly 4 parameters including tar2
  14. {
  15. printf("Not enough arguments!\n");
  16. return 0;
  17. }
  18.  
  19. strcpy(filename,argv[1]);
  20. FILE* f = fopen(filename, "rb"); // opening file for reading
  21. fseek(f,0,SEEK_END);
  22. lsize = ftell(f);
  23. rewind(f);
  24.  
  25. // taking argument [3] as the password
  26. strcpy(password,argv[3]);
  27. plen = strlen(password);
  28.  
  29. unsigned char xored;
  30.  
  31. // for encrypting
  32. if(strcmp(argv[2],"-e")==0)
  33. {
  34. printf("encrypting the file %s \n", filename);
  35. strcat(filename,".tar2");
  36. FILE* fout = fopen(filename, "wb");
  37. while(i<lsize){
  38. unsigned char first = fgetc(f); // reading byte from file
  39. i++;
  40. xored = first^(password[pindex]);
  41. fputc(xored,fout); // writing to output file
  42. pindex = (pindex+1)%plen;
  43. }
  44. fclose(fout);
  45. }
  46.  
  47. // for decrypting
  48. if(strcmp(argv[2],"-d")==0)
  49. {
  50. printf("decrypting the file %s \n", filename);
  51. int file_len = strlen(filename);
  52. int i;
  53. pindex = plen-1;
  54. for(i=0; i<file_len-5; i++)
  55. {
  56. nfilename[i] = filename[i];
  57. }
  58. nfilename[i]='\0';
  59. FILE* fout = fopen(nfilename, "wb");
  60. while(i<lsize){
  61. unsigned char first = fgetc(f);
  62.  
  63. i++;
  64. xored = first^password[pindex];
  65. fputc(xored,fout);
  66. pindex = pindex-1;
  67. if(pindex<0)
  68. {
  69. pindex = plen-1;
  70. }
  71. }
  72. fclose(fout);
  73. }
  74. fclose(f);
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement