Advertisement
j0h

EsEnigma

j0h
Jun 8th, 2023
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <unistd.h>
  5. int main(int argc, char ** argv){
  6.     /*
  7.      * Read a file. for each character, offset by N for the first value,
  8.      * for every other value, N+previous char value.
  9.      * Example: enigma -n 3 ABC
  10.      *    A+3=D=4
  11.      *    B+4= G=6
  12.      *    C+7=J=10
  13.      * */
  14.      
  15.         if(argc !=3){
  16.         printf("Usage: %s <integer offset> <inputfile.txt>\n", argv[0]);
  17.     return -1;
  18.         }
  19.     int offset = atoi(argv[1]);
  20.         //printf("Offset : %d\n",offset);
  21.     FILE *file1=fopen(argv[2], "r"); //file to decode
  22.  
  23.         if(file1==NULL ){
  24.         printf("Input Files ( %s ) Not Found\n", argv[2]);
  25.         return 0;
  26.         }
  27.         int count =0;
  28.             char c;  //file io char
  29.         unsigned    int p; // previous character Index
  30.     while(c!=EOF){
  31.         c =tolower(fgetc(file1));
  32.             if (count ==0){
  33.             p=c+offset;
  34.             //p=offset;
  35.             printf("char: %c  Offset: %d value: %c \n",c,offset,c+offset);
  36.             count ++;
  37.             offset=p;
  38.             }
  39.             if (isalpha(c)>=97 || isalpha(c)<=122){
  40. //get the previous index plus the index of the current value c
  41. //offset = p + (c-'a'+1);
  42. p=p-c-'a';
  43. printf("P: %d \n",p);
  44. offset = p + (c-'a'+1);
  45.        
  46.             //p=p+ (c-'a');
  47.         //  offset = 1;
  48. //printf("char: %c  Offset: %d value: %c \n",c,offset, c+offset);
  49.  
  50.  
  51.                 //deal with other non alphabet characters here
  52.                 printf(" ");
  53.                
  54.                 }
  55. }
  56.  
  57. //sleep(10);
  58. //system("clear");     
  59.     return 0;
  60.     }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement