0

Im working on a encryption-decryption program, that uses a modified Vigenére cipher. Namely i switch out the Vigenere square after encrypting/decrypting 5 letters. It works well if the text is short but with longer texts fgetc() starts returning invalid values, as if not the values that are actually present in the file im trying to decrypt. At times it starts returning -1 and at times just invalid characters. Invalid being in the sense that if you check the file with a text editor there's something else there. If you run the code the file creates 2 debug files in which you can compare what the one file outputted and what the other got from it. This brakedown seems to usually happen around 250+ characters or so but it doesnt seem to happen at an exact same point. (ive had files where i get over 300 in and it still works). The password also seems to affect when it breaks down.

Code to encrypt:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>




main()
{
FILE* kuhu;
FILE* kust;
FILE* debug;

int i, pikktekst, sisse, o, u, passwpikk, passk, kus, valija, n;
char failinimi[100], passw[80];
char  matr2 [154][154], matr3[154][154];
int matr1[257][257];

debug = fopen("debugsisse.txt", "w");
//maatriks 1
//creates matrix for Vigenere Square
o = 0;
u=0;
for(i=0; o<=255; i++)
{
     matr1[o][i]= i+u;

     if(matr1[o][i]<0){matr1[o][i]= matr1[o][i]+255 ;
     }

           if(matr1[o][i]> 255)
                  {matr1[o][i]=matr1[o][i]-255;}
                  if(i==256)
               {o++; i=0; u++;}
}
   algusest:
   printf("Tere, Kui soovite teksti programmi sisestada vajutage s kui     failist\nsisestada vajutage f, juhul kui soovite v%cljuda vajutage x\n press f from file, s to enter here and x to quit\n", 132);
   sisse = getch();
   if(sisse == 'x'){exit(0);}
   if(sisse != 's' & sisse != 'f'){goto algusest;} //again if invalid input
   //juhul kui sisestatakse siia
   else if(sisse=='s'){kus =0;
        printf("Sisestage faili nimi kuhu soovite kr%cptida\n file where to encrypt:\n", 129); //file where to encrypt
        scanf("%s", &failinimi);
        kuhu = fopen(failinimi, "w");

                printf("Sisestage tekst mida soovite kr%cptida:\n text to encrypt\n", 129); //text to encrypt
                //is copied to file to get the length
                sisse=0;
                pikktekst = 0;
                for(i=0; sisse!=13; i++)
                                   {
                                   sisse=getch();
                                   printf("%c", sisse);
                                   pikktekst = i;
                                   fprintf(kuhu, "%c", sisse);
                                   }
                                fclose(kuhu);
            kuhu = fopen (failinimi, "r");

                                }




    //juhul kui failist if from wile
   else if(sisse == 'f'){
       kus=1;
        printf("\nPalun kontrollige et fail asub samas asukohas \nkus programm ja sisestage selle nimi:\n enter name of file:");
        scanf("%s", &failinimi);
        kuhu=fopen(failinimi, "r");
        i=0;


        sisse = fgetc(kuhu);
        for(pikktekst=0; sisse!=EOF ; pikktekst++) //checks the length of file to declare variables
        {sisse = fgetc(kuhu);
        }

        }

        //failist sisse
        int kryptikssisse[pikktekst+2], valjund[pikktekst+2]; //declares variables for input/output

                                   rewind(kuhu);

                                   for(i=0; i<pikktekst+1; i++)
                                   {
                                   kryptikssisse[i] = fgetc(kuhu);
                                   }
                                   fclose(kuhu);

   //prindib krüptitava asja ning pikkuse
   //prints the length of text to encrypt and what it is.
 // printf("\nkrüptikssisse on %s\n", kryptikssisse);
 // printf("pikkus on %d\n", pikktekst);

//kus toimub krüptimine
//encryption happens here

printf("%d  Palun sisestage oma v%chemalt viie t%chem%crgi pikkune salas%cna kr%cptimiseks:\n enter password (min 5)\n",kus, 132, 132, 132, 228, 129);
scanf("%s", passw);
passwpikk =  strlen(passw);
while(passwpikk<5)
{printf("Palun sisestage oma v%chemalt viie t%chem%crgi pikkune salas%cna kr%cptimiseks:\n", 132, 132, 132, 228, 129);
scanf("%s", passw);
passwpikk =  strlen(passw);
}
//if its from file the output file gets asked here
if(kus != 0){
printf("Sisestage faili nimi kuhu soovite kr%cptida\n", 129);
scanf("%s", &failinimi);}
kuhu = fopen(failinimi, "w");

//maatrikikirjutus
//prints out the matrix
/*
o=0; u=0;
for(i=0; o<=256; i++)
{        if( matr1[o][i] != 13 && matr1[o][i] != 10 ) { fprintf(kuhu, "%d ", matr1[o][i]); }
     else { fprintf(kuhu, "ENTER"); }
     if(i==257){fprintf(kuhu," %d \n", o); o++; u++; i=0;}
     }exit(0);

     */




n=0;
valija=0;
passk = 0;
for(o=0; o<pikktekst; o++)
{
n++;
if(n%5==0) //changes Square after 5 letters
{int x=0, y=0, z=0; // variables to make table
valija++; 
if(valija > 255)
    {
    while(valija>255)
        {
        valija = valija - 255;
        }
    }
for(z=0; x<=255; z++) //creates new square, checks that nothing is higher or lower than 255
    {
    matr1[x][z]= z+y+valija;

    if(matr1[x][z]<0)
        {matr1[x][z]= matr1[x][z]+255 ;}

    if(matr1[x][z]> 255)
        {matr1[x][z]=matr1[x][z]-256;}
    if(z==256)
        {x++; z=0; y++;}
    }

}
//gives value to output
valjund[o]=matr1[passw[passk]][kryptikssisse[o]] /*+valija*/;
//checks that output isnt too little or large
while(valjund[o] < 0)
{valjund[o]= valjund[o]+256;
                 }
while(valjund[o] > 256)
{valjund[o]= valjund[o]-256; }
//prints output into debug file, with extra info AND the file the user specified
fprintf(kuhu, "%c", valjund[o]);
fprintf(debug, "%d :  %c (%d) --> %c (%d)\n",n, kryptikssisse[o], kryptikssisse[o], valjund[o], valjund[o]);
passk++;//checks password and resets if neccesary
if(passk>=passwpikk) {passk=0;}

}

fprintf(debug, "%s", kryptikssisse);
system("pause");

fclose(kuhu);
exit(0);
}

the code for the decryption is :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <sys/stat.h>





main()
{ FILE* kust;
  FILE* kuhu;
  FILE* debug;
  debug = fopen("debug valja.txt", "w");
  fpos_t position;
int i, sisse, passwpikk, u, o, kord, nord, k;
char pw[50], sissef[80], valjaf[80];
int matr1[257][257];
printf("Mis failist soovite dekr%cptida?\n input file\n", 129);
scanf("%s", &sissef);
kust= fopen(sissef, "r");


struct stat st;
stat(sissef, &st);
int size = st.st_size;
printf("Size =%d", size);


printf("Kuhu soovite teksti panna?\n output file\n");
scanf("%s", &valjaf);
kuhu = fopen(valjaf, "wb");



//maatriks 1
//makes Vigenere Table
o = 0;
u=0;
for(i=0; o<=255; i++)
{
     matr1[o][i]= i+u;

     if(matr1[o][i]<0){matr1[o][i]= matr1[o][i]+255 ;
     }

           if(matr1[o][i]> 255)
                  {matr1[o][i]=matr1[o][i]-255;}
                  if(i==256)
               {o++; i=0; u++;}
}

//maatriksikirjutus
//prints Vigenere table
/*o=0; u=0;
for(i=0; o<=256; i++)
{if( matr1[o][i] != 13 && matr1[o][i] != 10 ) { fprintf(kuhu, "%d ", matr1[o][i]); }
     else { fprintf(kuhu, "ENTER"); }
     if(i==257){fprintf(kuhu," %d \n", o); o++; u++; i=0;}
     }exit(0);*/


printf("Palun sisestage oma salas%cna!\npassword\n", 228);
scanf("%s", &pw);
passwpikk =  strlen(pw);


//starts encrypting
int valjund=0;
sisse = 0;
sisse = fgetc(kust);

kord=0;
nord=1;
for(o=0; nord<size; nord++){

if(nord%5==0) //changes table
{int x=0, y=0, z=0;

kord++;
if(kord>255)
    {while(kord>255)
        {
        kord= kord - 255;
        }
    }

for(z=0; x<=255; z++)
    {
    matr1[x][z]= z+y/*+kord*/;

    if(matr1[x][z]<0)
        {matr1[x][z]= matr1[x][z]+255 ;}

    if(matr1[x][z]> 255)
        {matr1[x][z]=matr1[x][z]-256;}
    if(z==256)
        {x++; z=0; y++;}
    }
    //prints all matrixes that it uses; please not file can get very large and the programs uses A LOT of time.
    /*
    FILE* matr;
    matr = fopen("maatriksid", "a");
    fprintf(matr, "\n\n");
    z=0; x=0;
    for(i=0; z<=255; i++)
    {if( matr1[z][i] != 13 && matr1[z][i] != 10 ) { fprintf(matr, "%d ", matr1[z][i]); }
     else { fprintf(matr, "ENTER"); }
     if(i==256){fprintf(matr," %d \n", z); z++; x++; i=0;}
     }
    fclose(matr);
*/

}

i=pw[o];
for( u=0; sisse != matr1[i][u]; u++)
{
}
if(u==0)
{
 for( u=1; sisse != matr1[i][u]; u++)
 {
 }
}

//show when bad things happen
if(matr1[0][u]-kord<0)
{fprintf(debug, "shiiet");}

if(u>256){fprintf(debug, "  shit went to hell  ");}



valjund = matr1[0][u] - kord ;


/*
while(valjund < 0)
{
valjund= valjund+255;
}


while(valjund > 255)
{
valjund = valjund - 255;
}
*/

//prints stuff out

// note that the debug file prints the int sisse which shows exactly the letter that the program took from the file
fprintf(kuhu ,"%c", valjund);
fprintf(debug,"%d : %c (%d) --> %c (%d) u oli %d \n",nord, sisse, sisse, valjund, valjund, u);


//hoped reopening the file would help
//it didnt
fgetpos(kust, &position);
sisse = fgetc(kust);
if(sisse==-1)
{
fclose(kust);
kust = fopen(sissef, "r");
fsetpos(kust, &position);
sisse= fgetc(kust);
}


//checks password
o++;
if(o>=passwpikk){o=0;}



}
//prints the password at the end of debug file
fprintf(debug,"\n%s", pw);
printf("valmis\n done\n");
system("pause");
exit(0);
}

0 Answers0