Simon
Simon

Reputation: 3

Changing array size changes the behaviour of STRCMP

My program checks a given 4 bit hash from file HashedPassword.txt and compares it to all possible 4 upper-case letter possibilities which is supposed to be in temp.txt.

My issue is that my program works fine if my array "alpha" is of size 13 and under, in this case my program will hit the STRCMP if statement and give me the correct password that corresponds to the hash given in HashedPasswords.txt, anything above size 13 array will cause STRCMP to print out none stop "1" therefore not finding a match.'

How can I fix this?

P.S I tried changing the sizes of line and line2, making them larger changes the output of strcmp as well.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void E(char *in, char *out);

int main()
{
    FILE* file,*fp2,*fp3;
    char input[5];
    char pass[5];
    char alpha[15]="ABCD";//EFGHIJKLMNOPQRSTUVWXYZ";

    char compare;
    char string[5];
    int i,j,k,l,m,a;

    size_t lsize=0;
    char line[5];
    char line2[5];
    char output[5];
    char newline[2]="\n";
    char test[5];
    char *r=NULL;

    fp2=fopen("HashedPassword.txt","r");

    fgets(line2,sizeof(line2),fp2);

    printf("%s\n",line2);

    fclose(fp2);
    file=fopen("temp.txt","w");
    for(i=0;i<4;i++)
    {

        for(j=0;j<4;j++)
        {
            for(k=0;k<4;k++)
            {
                for(l=0;l<4;l++)
                {
                    fprintf(file,"%c%c%c%c\n",alpha[i],alpha[j],alpha[k],alpha[l]);
                }
            }
        }
    }

    fclose(file);

    fp3=fopen("temp.txt","r");

    while(getline(&r,&lsize,fp3))
    {
        E(r,output);

        printf("%d\n",strcmp(output,line2));

        if(!strcmp(output,line2))
        {
            printf("This is your password: %s\n",r);
            break;
    }

    }

    fclose(fp3);
}


void E(char *in, char *out)
{
    out[0]=(in[0]&0x80)^(((in[0]>>1)&0x7F)^((in[0])&0x7F));
    out[1]=((in[1]&0x80)^((in[0]<<7)&0x80))^(((in[1]>>1)&0x7F)^((in[1])&0x7F));
    out[2]=((in[2]&0x80)^((in[1]<<7)&0x80))^(((in[2]>>1)&0x7F)^((in[2])&0x7F));
    out[3]=((in[3]&0x80)^((in[2]<<7)&0x80))^(((in[3]>>1)&0x7F)^((in[3])&0x7F));
}

Upvotes: 0

Views: 347

Answers (1)

ChrisWue
ChrisWue

Reputation: 19020

For one thing: output is of length 5 but you only ever set the first four characters in E() which means that output[4] contains random garbage (whatever happened to be in that particular byte of memory). strcmp compares two null terminated strings so you should make sure that ouput[4] = '\0' is done at least once. It would also be slightly better to use strncmp and provide the length (e.g. strncmp(output, line2, sizeof(output)) to make sure it doesn't go completely wild in case of missing null termination.

Changing the size of the local variables around will affect where exactly on the stack output is located which will affect what the content of output[4] is which will affect the result of the strcmp.

Upvotes: 3

Related Questions