Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include "caesar.h"
  2.  
  3. static char *alphabet = "abcdefghijklmnopqrstuvwxyz";
  4.  
  5. void Decrypt(char *text,int shift){
  6.  
  7.     int i,x;
  8.    
  9.     for(i=0;i<strlen(text);i++){
  10.         if(isspace(text[i]) != 0 || ispunct(text[i]) != 0 || text[i] == '\n'){
  11.             /*when a space, punctuation character or new line is encountered skip*/
  12.         }
  13.         else{
  14.             text[i] = tolower(text[i]);
  15.             x = (text[i] - 'a' - shift) % 26;
  16.             if (x < 0)
  17.                 x -= x;
  18.             text[i] = alphabet[x];
  19.         }
  20.     }
  21. }
  22.  
  23. void Encrypt(char *text,int shift){
  24.  
  25.     int i,x;
  26.  
  27.     for(i=0;i<strlen(text);i++){
  28.         if(isspace(text[i]) != 0 || ispunct(text[i]) != 0 || text[i] == '\n'){
  29.             /*when a space, punctuation character or new line is encountered skip*/
  30.            
  31.         }
  32.         else{
  33.             text[i] = tolower(text[i]);
  34.             x = (text[i] - 'a' + shift) % 26;
  35.             if (x < 0)
  36.                 x -= x;
  37.             text[i] = alphabet[x];
  38.         }
  39.     }
  40. }
  41.  
  42. void BruteForce(char *str) {
  43.     int i;
  44.     char *text = strdup(str);
  45.  
  46.     for (i=0;i<ALPHA;i++) {
  47.         Decrypt(text,i+1); /*Shifts it by one every time*/
  48.         printf("%s\n", text);
  49.     }
  50.     free(text);
  51. }
  52.  
  53. char *OpenFromFile(char *fileIn){
  54.     FILE *fin = fopen(fileIn, "r");
  55.     char *text;
  56.  
  57.     text = malloc(N * sizeof(char));
  58.  
  59.     if(fin == NULL){
  60.         fprintf(stderr, "Error, could not open file.\n");
  61.         exit(-1);
  62.     }
  63.  
  64.     /*read entire file and put it in text*/
  65.     fread(text,N,1,fin);
  66.  
  67.     fclose(fin);
  68.     return text;
  69. }
  70.  
  71. void WriteToFile(char *text, char *fileOut){
  72.     FILE *fout = fopen(fileOut, "w");
  73.  
  74.     if(fout == NULL){
  75.         fprintf(stderr, "Error, could not open file.\n");
  76.         exit(-1);
  77.     }
  78.  
  79.     printf("%s\n", text);
  80.     fwrite(text,strlen(text),1,fout);
  81.  
  82.     fclose(fout);
  83. }
  84.  
  85. void Usage(){
  86.     printf("Usage: caesar <shift> [-e | -d]  [-i] <input_file> [-o] <output_file>\n");
  87.     printf("Usage without known shift: caesar [-b]  [-i] <input_file> [-o] <output_file>\n");
  88.     return;
  89. }
  90.  
  91. int main(int argc, char **argv){
  92.     char *text;
  93.  
  94.     if (argc != 7 && argc != 6 ){
  95.             Usage();
  96.             exit(0);
  97.         }
  98.     else if(strcmp(argv[3],"-i") == 0 && strcmp(argv[5],"-o") == 0){
  99.  
  100.         text = OpenFromFile(argv[4]);
  101.         printf("%s\n", text);
  102.         /*encrypt*/
  103.         if (strcmp(argv[2],"-e") == 0){
  104.             Encrypt(text,atoi(argv[1]));
  105.  
  106.         }
  107.         /*decrypt*/
  108.         else if (strcmp(argv[2],"-d") == 0){
  109.             Decrypt(text,atoi(argv[1]));
  110.         }
  111.         else{
  112.             Usage();
  113.             exit(0);
  114.         }
  115.  
  116.         WriteToFile(text,argv[6]);
  117.     }
  118.    
  119.     else if(strcmp(argv[2],"-i") == 0 && strcmp(argv[4],"-o") == 0){
  120.  
  121.         text = OpenFromFile(argv[3]);
  122.  
  123.         /*bruteforce*/
  124.         if(strcmp(argv[1],"-b") == 0){
  125.             BruteForce(argv[3]);
  126.         }
  127.         else{
  128.             Usage();
  129.             exit(0);
  130.         }
  131.  
  132.         WriteToFile(text,argv[5]);
  133.     }
  134.     else{
  135.         Usage();
  136.         exit(0);
  137.     }
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement