decryptor

decryptor

Post by J-Progress on Mon Jan 18, 2016 6:46 pm
([msg=91320]see decryptor[/msg])

Hello guys,
I recently decided to start my own encryption program in c. The basic idea was to take some characters and encrypt the using their ascii values. I made the encryptor and it work almost fine. The decryptor instead is a problem for me more than a week now. I have write the code but it doesn't work....It should get the encrypted text from a text file then decrypt it and write to another text file. I have post the code below..please if you see something that may cause problem to the operation of the program, help me solve the problem.

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void infile(key){
   FILE *e;
   FILE *f;
   
   
   int count=0, i, n, j, val;
   char *array, ascarray[4], ch;
   
   
   e=fopen("encfile.txt", "r");
   if(e==NULL){
      printf("Error opening file!\n");
      system("pause");
      exit(0);                                 
   }
   
   
   while((ch=fgetc(e))!=EOF){
      count++;
   }
    printf("count: %d\n", count);
   
   
   array=malloc(count*sizeof(char));
   
   for(i=0;i<count;i++){
      *(array+i)=fgetc(e);
      
   }
   
   
   f=fopen("file.txt", "w");
   
   i=0;
   j=0;
   
   do{
      
      int n = array[i] - '0';
      printf("%c\n",array[5]);
      printf("%d\n", n);
      printf("loop");
      if(n==1){
         printf("1");
         ascarray[0] = *(array+i+1);
         ascarray[1] = '\0';
         val = atoi(ascarray);
         val = val - key + j;                 
         char ch = val;
         fprintf(f, "%c", ch);
         i = i + n + 1;
         j++;
      }else if(n==2){
         printf("2");
         ascarray[0] = *(array+i+1);
         ascarray[1] = *(array+i+2);
         ascarray[2] = '\0';
         val = atoi(ascarray);
         val = val - key + j;
         char ch = val;
         fprintf(f, "%c", ch);
         i = i + n + 1;
         j++;
      }else if(n==3){
         printf("3");
         ascarray[0] = *(array+i+1);
         ascarray[1] = *(array+i+2);
         ascarray[2] = *(array+i+3);
         ascarray[3] = '\0';
         val = atoi(ascarray);
         val = val - key + j;
         char ch = val;
         fprintf(f, "%c", ch);
         i = i + n + 1;
         j++;
      }else{
         printf("4");
         fputs("\n\n UNABLE TO DECRYPT FILE. FILE MAY BE CORRUPTED! \n\n", f);
      }
      
      }while(!feof(e));
      
   fclose(e);
    fclose(f);
   }
   
   
   





int main(){
   char key;
   
   printf("This is the PBytes Decryption programm.\n");
   printf("Please name the encrypted text file to << encfile >> and place it to Desktop.\n");
   printf("Please write the key that has been used for the encryption: ");
   scanf("%c", &key);
   
   infile(key);
}
J-Progress
New User
New User
 
Posts: 5
Joined: Thu Nov 05, 2015 4:54 pm
Blog: View Blog (0)


Re: decryptor

Post by xorcist_re on Sat Apr 02, 2016 3:36 am
([msg=92068]see Re: decryptor[/msg])

I haven't written anything in C in a while, but it looks to me like you are counting the characters in the first file using the fgetc() function in order to allocate memory for the array to hold the contents. Then you are trying to read each character from the file into the array you allocate, but you didn't reset the pointer to point back to the beginning of the file. The next time you are using the fgetc() to read the contents of the file the pointer is already at the EOF so it won't read anything.
xorcist_re
New User
New User
 
Posts: 2
Joined: Sat Mar 19, 2016 2:55 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests