Basic Encryption changes file size O.o

I was creating a basic encryption Console program. and i found that after encrypting the file size was changed. it just increases the value by 3 of any character and writes to file as you can see it doesn't adds or removes any character. when decrypting the data the file size comes to normal the difference is before encrypting = 239 bytes after encrypting = 224 total difference = 15 bytes.
here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
simple and basic file encryption technique
*/

#include<stdio.h>

void Encfile(FILE *infile,FILE *outfile)
{
        char c;
        int i;
        while(!feof(infile))
        {
            c = fgetc(infile);
            if(!feof(infile))
            {
                fputc(c+3,outfile);
            }
            else
                break;
        }
        fclose(outfile);
        fclose(infile);
}

void UnEncfile(FILE *encfile)
{
    char c;
    FILE *Decodedfile;
    Decodedfile = fopen("d:\\x\\Decodedfile.txt","w");
    fclose(encfile);
    freopen("d:\\x\\encfile.txt","r",encfile);

    if(encfile && Decodedfile)
    {
        while(!feof(encfile))
        {
            c = fgetc(encfile);
            if(!feof(encfile))
            {
                fputc(c-3,Decodedfile);
            }
        }
    }

    else
    {
        perror("error");
    }

    fclose(Decodedfile);
    fclose(encfile);
}

int main()
{
    char infile[256];
    char outfile[256];
    int choice;
    FILE *inf,*outf;

    printf("What you want to do\n\n1.) Encrypt a file. \n2.)Decrypt a file\n\nSo what you decided = ");
    scanf("%d",&choice);

    if(choice == 1)
    {
        inf = fopen("d:\\x\\xxx.txt","r");
        outf = fopen("d:\\x\\encfile.txt","w");

        if(inf && outf)
        {
            printf("ok");
            Encfile(inf,outf);
        }
        else
            perror("error ");

    }

    else if(choice == 2)
    {
        FILE *decrypt;
        decrypt = fopen("d:\\x\\encfile.txt","r");
        if(decrypt)
        {
             UnEncfile(decrypt);
        }
        else
            perror("\nerror ");

    }
    return 0;
}


beside the main question if you have any advice for improving coding style then it's always welcome.
I am not sure why the file size is different either, but I can offer some advice for improving your code.

In main, use a switch statement instead of a load of else if's. Also it's probably better style to put the code for each case: into functions. It's not a problem at the moment, but it will be if you have lots of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
switch (choice) {
    case 1:
          DoEncryption();
          break;
    case 2:
          DoDecryption();
          break;
     default:
         printf("Bad Menu Option");
         break;  

}


Of course you will have to do forward declarations for you new functions and send them the arguments they need.

It's better style to do forward declarations for your functions rather than define them before main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>

void Encfile(FILE *infile,FILE *outfile);
void UnEncfile(FILE *encfile);
void  DoEncryption();
void  DoDecryption();

int main() {

}

void Encfile(FILE *infile,FILE *outfile) 
{
/* function definition here */
}

void UnEncfile(FILE *encfile)
{
/* function definition here */
}

void  DoEncryption()
{
/* function definition here */
}

void  DoDecryption()
{
/* function definition here */
}



Hope this helps

TheIdeasMan
1
2
inf = fopen("d:\\x\\xxx.txt","r");
outf = fopen("d:\\x\\encfile.txt","w");

maybe use "wb" and "rb" instead of "w" and "r".. that will make sure the data is interpretated as binary data..

beside the main question if you have any advice for improving coding style then it's always welcome.


1
2
3
4
5
        while(!feof(infile))
        {
            c = fgetc(infile);
            fputc(c+3,outfile);
        }


do not use a double feof check ;) You read the char and you check if reached the end after that, when the end of the file is reached the last character isn't written to the outfile....
Last edited on
maybe use "wb" and "rb" instead of "w" and "r".. that will make sure the data is interpretated as binary data..

thanks for advice. but i didn't used binary read and write options because i was dealing with text files. using binary messes up with the original text.
i also forgot to mention that i was using windows (my bad) . windows treats binary and text file differently. i will definitely use that binary option when i have to deal with binary files.

do not use a double feof check ;)

well i tried that thing and if i don't double check the position of the file it added 1 extra byte to the file. because fgetc moves to next offset (please fix me if i am wrong) after getting data.
it was going out side of file buffer so i needed that double check :P

and thanks TheIdeasMan for coding style it helps.
well i am still waiting for a solid answer about file size.
i tested your code and with a small file it just works fine... can you give us your test file?
Last edited on
here it is.
i just copied and pasted the contents of the file.
[Main]
Top=50
Left=50
Page=0
Extra=0
XML=0
InjectonLastSecond=0
DisplayProgress=0
ExtractOnly=0
keyframes=0
InFile=C:\Users\xxxxxxxx\AppData\Local\Temp\VideoCacheViewTemp.wmv
OutFile=d:\x.flv
InFolder=
OutFolder=
ExtraData=



and don't get confused i am asking size difference between encrypted file and decrypted file.
Last edited on
I also think that you should open the files in binary mode. Binary mode doesn't mean it can't be text. If you are not using binary mode on Windows it will automatically convert \r\n to \n when reading and \n to \r\n when writing. So what happens is that when your program reads a newline sequence from the file \r\n (2 characters) the program only receives a \n character. If you didn't encrypt the \n character it would just be converted to \r\n automatically when it was written to file and the file size would stay the same, but you encrypt it so it's no longer a \n character so no automatic conversion will take place so only one character is written to the file. That is why you get a different file size.
Last edited on
Thanks for explanation. that solved my problem.
Topic archived. No new replies allowed.