Thread: Encryption/Decryption/File I/O in C

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28

    Encryption/Decryption/File I/O in C

    Code:
    #include<stdio.h>
    typedef int boolean;
    #define true 1
    #define false 0
    char Shift(char Achar,int Shifts)
    {
        boolean Shiftleft =
        Shifts <= 0;
        if (Shiftleft)
         Shifts=-Shifts;
        int Lowerbound = 32;
        int Remainder;
        int Upperbound = 126;
        Remainder =
    Shifts%(Upperbound-Lowerbound);
        if (Shiftleft)
        {
            if (Remainder>
            (int)Achar-Lowerbound)
            {
                return (char)(Upperbound+(((int)Achar-Lowerbound)-Remainder)+1);
            }
            else return (char)((int)Achar-Remainder);
    
        }
        else
        {
            if (Remainder>
            Upperbound-(int)Achar)
             {
                return (char)(Lowerbound+(Remainder-(Upperbound-(int)Achar))-1);
            }
            else return (char)((int)Achar+Remainder);
        }
    }
    int Encryption(char *AString,char *Buffer)
    {
    int i = 0;
    for (i;i!=strlen(AString);i++)
    {
    if ((int)AString[i] >= 32 && (int)AString[i] <= 126)
        if (AString[i]!=10)
            Buffer[i]=Shift(AString[i],i+1);
    }
    Buffer[i]='\0';
    return 1;
    }
    int Decryption(char *AString,char *Buffer)
    {
    int i = 0;
    for (i;i!=strlen(AString);i++)
    {
    if (AString[i] >= 32 && AString[i] <= 126)
        if (AString[i]!=10)
    Buffer[i]=Shift(AString[i],-i-1);
    }
    Buffer[i]='\0';
    return 1;
    }
    
    
    
    int main(int argc,char *argv[])
    {
    
        char Char;
        char Line[501],TLine[501];
        TLine[0]=0;
    
        if (argc==1 || argc==2)
        {
    
            printf("Useage : \n");
    
        }
        else
        {
        if (strcmp(argv[1],"-e")==0 || strcmp(argv[1],"-d")==0)
        {
            if (argv[2]!="")
            {
                char Temp[501];
                if (strcmp(argv[1],"-d")==0) Decryption(argv[2],Temp);
                if (strcmp(argv[1],"-e")==0) Encryption(argv[2],Temp);
    
                printf("%s\n",Temp);
                exit(0);
            }
        }
    
            if (strcmp(argv[1],"-fe")==0)
        {
        FILE *Source,*Dest;
        Source = fopen(argv[2],"r");
        if(Source==NULL)
        {
            printf("Error Opening The Source File.\n");
            exit(1);
        }
        Dest = fopen(argv[3],"w");
        //printf("File : %s\n",argv[2]);
        if(Dest==NULL)
        {
            printf("Error Opening The Destination File.\n");
            exit(1);
        }
        char ch = 0;
        int i = -1;
        int EOFReached=0;
        while (EOFReached ==0)
        {
            i++;
            ch=fgetc(Source);
            if ((ch==EOF)||(ch=='\n'))
            {
                Line[i]='\0';
                Encryption(Line,TLine);
                printf("%s||%s\n",Line,TLine);
                fprintf(Dest,TLine);
                fprintf(Dest,"\n");
                Line[0]=0;
                TLine[0]=0;
                i=-1;
                if (ch==EOF) EOFReached =1;
            }
            else
            {
             Line[i]=ch;
            }
        }
        printf("\n");
        exit(0);
        }
           if (strcmp(argv[1],"-fd")==0)
        {
        FILE *Source,*Dest;
        Source = fopen(argv[2],"r");
        if(Source==NULL)
        {
            printf("Error Opening The Source File.\n");
            exit(1);
        }
        Dest = fopen(argv[3],"w");
        //printf("File : %s\n",argv[2]);
        if(Dest==NULL)
        {
            printf("Error Opening The Destination File.\n");
            exit(1);
        }
        char ch = 0;
        int i = -1;
        int EOFReached=0;
        while (EOFReached ==0)
        {
            i++;
            ch=fgetc(Source);
            if ((ch==EOF)||(ch=='\n'))
            {
                Line[i]='\0';
                Decryption(Line,TLine);
                printf("%s||%s\n",Line,TLine);
                fprintf(Dest,TLine);
                fprintf(Dest,"\n");
                Line[0]=0;
                TLine[0]=0;
                i=-1;
                if (ch==EOF) EOFReached =1;
            }
            else
            {
             Line[i]=ch;
            }
        }
        printf("\n");
        exit(0);
        }
        if (strcmp(argv[1],"-s")==0)
        {
        FILE *RFile;
        RFile = fopen(argv[2],"r");
        if(RFile==NULL)
        {
            printf("Error Opening The File.\n");
            exit(1);
        }
        char Char;
        while ((Char = fgetc(RFile))!=EOF)
               {
                printf("%c",Char);
               }
        }
        }
        printf("\n");
        return 0;
    }
    I'm using arguments
    Encryption -fe Test.txt Encrypted.txt
    Test.txt contains:
    Code:
    BlahBlahDieBlah.DahBlah
    BlahBlahDieBlah.DahBlah
    BlahBlahDieBlah.DahBlah
    BlahBlahDieBlah.DahBlah
    BlahBlahDieBlah.DahBlah
    BlahBlahDieBlah.DahBlah
    BlahBlahDieBlah.DahBlah
    Encrypted.txt Then Contains
    Code:
    CndlGrhpMspNyow>Us{V"w 
    CndlGrhpMspNyow>Us{V"w 
    CndlGrhpMspNyow>Us{V"w 
    CndlGrhpMspNyow>Us{V"w 
    CndlGrhpMspNyow>Us{V"w 
    CndlGrhpMspNyow>Us{V"w 
    CndlGrhpMspNyow>Us{V"w
    If i then use Encryption -fd Encrypted.txt Test.txt
    Test.txt will contain :

    Code:
    BlahBlahDieBlah.DahBüa
    BlahBlahDieBlah.DahBüa
    BlahBlahDieBlah.DahBüa
    BlahBlahDieBlah.DahBüa
    BlahBlahDieBlah.DahBüa
    BlahBlahDieBlah.DahBüa
    BlahBlahDieBlah.DahBüa
    With my previous tests if i called the encryption function on a string and
    the decryption function on the resulting string they matched but as soon as i use
    a file to read from something gets lost somewhere.
    Some help please?

    Also how do i pick a random number i tried rand()%20 or something like that but it just kept giving me 0.

    Thanks in advance !

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You have bizarrely formatted code with many compilation errors.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘Encryption’:
    bar.c:30:3: warning: statement with no effect [-Wunused-value]
    bar.c:30:3: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]
    bar.c:30:16: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
    bar.c: In function ‘Decryption’:
    bar.c:42:3: warning: statement with no effect [-Wunused-value]
    bar.c:42:16: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
    bar.c: In function ‘main’:
    bar.c:60:5: warning: implicit declaration of function ‘strcmp’ [-Wimplicit-function-declaration]
    bar.c:61:19: warning: comparison with string literal results in unspecified behavior [-Waddress]
    bar.c:69:9: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
    bar.c:69:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    bar.c:78:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    bar.c:84:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    bar.c:96:11: warning: format not a string literal and no format arguments [-Wformat-security]
    bar.c:108:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    bar.c:115:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    bar.c:121:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    bar.c:133:11: warning: format not a string literal and no format arguments [-Wformat-security]
    bar.c:145:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    bar.c:152:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    bar.c:53:8: warning: unused variable ‘Char’ [-Wunused-variable]
    Most of that is down to missing header files.
    Here is your code.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    typedef int boolean;
    #define true 1
    #define false 0
    char Shift(char Achar, int Shifts)
    {
      boolean Shiftleft = Shifts <= 0;
      if (Shiftleft)
        Shifts = -Shifts;
      int Lowerbound = 32;
      int Remainder;
      int Upperbound = 126;
      Remainder = Shifts % (Upperbound - Lowerbound);
      if (Shiftleft) {
        if (Remainder > (int) Achar - Lowerbound) {
          return (char) (Upperbound + (((int) Achar - Lowerbound) - Remainder) + 1);
        } else
          return (char) ((int) Achar - Remainder);
      } else {
        if (Remainder > Upperbound - (int) Achar) {
          return (char) (Lowerbound + (Remainder - (Upperbound - (int) Achar)) - 1);
        } else
          return (char) ((int) Achar + Remainder);
      }
    }
    
    int Encryption(char *AString, char *Buffer)
    {
      int i = 0;
      for (i; i != strlen(AString); i++) {
        if ((int) AString[i] >= 32 && (int) AString[i] <= 126)
          if (AString[i] != 10)
            Buffer[i] = Shift(AString[i], i + 1);
      }
      Buffer[i] = '\0';
      return 1;
    }
    
    int Decryption(char *AString, char *Buffer)
    {
      int i = 0;
      for (i; i != strlen(AString); i++) {
        if (AString[i] >= 32 && AString[i] <= 126)
          if (AString[i] != 10)
            Buffer[i] = Shift(AString[i], -i - 1);
      }
      Buffer[i] = '\0';
      return 1;
    }
    
    int main(int argc, char *argv[])
    {
      char Char;
      char Line[501], TLine[501];
      TLine[0] = 0;
    
      if (argc == 1 || argc == 2) {
        printf("Useage : \n");
      } else {
        if (strcmp(argv[1], "-e") == 0 || strcmp(argv[1], "-d") == 0) {
          if (argv[2] != "") {
            char Temp[501];
            if (strcmp(argv[1], "-d") == 0)
              Decryption(argv[2], Temp);
            if (strcmp(argv[1], "-e") == 0)
              Encryption(argv[2], Temp);
    
            printf("%s\n", Temp);
            exit(0);
          }
        }
    
        if (strcmp(argv[1], "-fe") == 0) {
          FILE *Source, *Dest;
          Source = fopen(argv[2], "r");
          if (Source == NULL) {
            printf("Error Opening The Source File.\n");
            exit(1);
          }
          Dest = fopen(argv[3], "w");
          //printf("File : %s\n",argv[2]);
          if (Dest == NULL) {
            printf("Error Opening The Destination File.\n");
            exit(1);
          }
          char ch = 0;
          int i = -1;
          int EOFReached = 0;
          while (EOFReached == 0) {
            i++;
            ch = fgetc(Source);
            if ((ch == EOF) || (ch == '\n')) {
              Line[i] = '\0';
              Encryption(Line, TLine);
              printf("%s||%s\n", Line, TLine);
              fprintf(Dest, TLine);
              fprintf(Dest, "\n");
              Line[0] = 0;
              TLine[0] = 0;
              i = -1;
              if (ch == EOF)
                EOFReached = 1;
            } else {
              Line[i] = ch;
            }
          }
          printf("\n");
          exit(0);
        }
        if (strcmp(argv[1], "-fd") == 0) {
          FILE *Source, *Dest;
          Source = fopen(argv[2], "r");
          if (Source == NULL) {
            printf("Error Opening The Source File.\n");
            exit(1);
          }
          Dest = fopen(argv[3], "w");
          //printf("File : %s\n",argv[2]);
          if (Dest == NULL) {
            printf("Error Opening The Destination File.\n");
            exit(1);
          }
          char ch = 0;
          int i = -1;
          int EOFReached = 0;
          while (EOFReached == 0) {
            i++;
            ch = fgetc(Source);
            if ((ch == EOF) || (ch == '\n')) {
              Line[i] = '\0';
              Decryption(Line, TLine);
              printf("%s||%s\n", Line, TLine);
              fprintf(Dest, TLine);
              fprintf(Dest, "\n");
              Line[0] = 0;
              TLine[0] = 0;
              i = -1;
              if (ch == EOF)
                EOFReached = 1;
            } else {
              Line[i] = ch;
            }
          }
          printf("\n");
          exit(0);
        }
        if (strcmp(argv[1], "-s") == 0) {
          FILE *RFile;
          RFile = fopen(argv[2], "r");
          if (RFile == NULL) {
            printf("Error Opening The File.\n");
            exit(1);
          }
          char Char;
          while ((Char = fgetc(RFile)) != EOF) {
            printf("%c", Char);
          }
        }
      }
      printf("\n");
      return 0;
    }

    With the right headers, the list is smaller.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘Encryption’:
    bar.c:32:3: warning: statement with no effect [-Wunused-value]
    bar.c: In function ‘Decryption’:
    bar.c:44:3: warning: statement with no effect [-Wunused-value]
    bar.c: In function ‘main’:
    bar.c:63:19: warning: comparison with string literal results in unspecified behavior [-Waddress]
    bar.c:98:11: warning: format not a string literal and no format arguments [-Wformat-security]
    bar.c:135:11: warning: format not a string literal and no format arguments [-Wformat-security]
    bar.c:55:8: warning: unused variable ‘Char’ [-Wunused-variable]
    bar.c:63:19: warning: comparison with string literal results in unspecified behavior [-Waddress]
    You don't compare a string using != "" to find out if it's an empty string.

    bar.c:98:11: warning: format not a string literal and no format arguments [-Wformat-security]
    bar.c:135:11: warning: format not a string literal and no format arguments [-Wformat-security]
    NEVER pass a raw string as the first parameter of printf style functions.
    Use either puts(s) or printf("%s",s)


    Code:
            if (strcmp(argv[1], "-d") == 0)
              Decryption(argv[2], Temp);
    This is good.
    Now make functions for your -fe, -fd and -s options as well, so that main() is a much tidier function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28
    Hi!
    I did as you suggested and turned my compilers warning level to max as well.

    I fixed most of the warnings except for one pointing to an unused variable pointing to i in my for loop. I guess thats fine though.

    The fe function is still behaving strangely though. I tested the shift function on chars 32 to 126 ; Shifting with 1 to 200 for each character and then comparing the input for the shift char with the result of the second shift (which would be -Shifts)

    0/19877 errors occured so i doubt that it's my shift function.

    Unfortunately i am not close to my pc and dont have the new code. Ill upload it later!

    Code:
    Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption and Decryption
    By dbz8gokugohan in forum C Programming
    Replies: 7
    Last Post: 12-02-2010, 07:40 AM
  2. encryption / decryption
    By ghatoz in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2010, 06:45 AM
  3. Encryption/Decryption Help
    By coolage in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2008, 01:53 AM
  4. Ask about Encryption and Decryption
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 12:55 AM
  5. Encryption/Decryption -- Correction
    By Abdi in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2002, 08:00 AM

Tags for this Thread