-
next wierd thing
i have the following
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int myfunc( char *, char *, int , int , int );
int main()
{
char text[] = "tomorrow never dies";
char pword[] = "hel";
char encrypted[sizeof text];
int len = strlen( text );
int plen = strlen( pword );
for ( int j = 0, i = 0; text[i]; i++, j = (j + 1) % plen )
encrypted[i] = text[i] ^ pword[j];
encrypted[len] = '\0';
int Guesses[ sizeof pword ][26] = { 0 };
// Don't use numbers for letters, e.g., say 'A' instead of 65.
for ( int StartIndex = 0; StartIndex < plen; StartIndex++ )
{
int GuessIndex = 0;
for ( int i = 'a'; i <= 'z'; i++ )
{
char c = i;
if ( myfunc( encrypted, &c, StartIndex, len, plen ) )
{
Guesses[StartIndex][GuessIndex] = i;
GuessIndex++;
//c = 'a';
}
}
//putchar('\n');
}
if ( myfunc(encrypted, pword, 0, len, plen) )
printf("Password found!!\n ");
return 0;
}
int myfunc( char *Encrypted, char *c, int StartIndex, int len, int plen )
{
int Letter = 1;
for ( int i = 0; i < strlen( c ); i++)
for ( int j = StartIndex; j < len; j += plen )
{
int x = Encrypted[j] ^ c[i];
if ( ! ( ( x >= 'A' && x <= 'Z' ) || ( x >= 'a' && x <= 'z' ) || ( x >= ' ' && x <= '@' ) ) )
{
Letter = 0;
break;
}
}
return Letter;
}
however i find the first possible letter of the password with c being one character long then for some reason it remembers the first character found and appends it to all the others so watching c in debug mode i get c= a, b, c..... ,h , ih, jh, kh etc etc.
why is it remembering the first letter found also if i comment out the guess's stuff c becomes a random bunch of letters its not even i
the idea is once i have a list of possible passwords i can test them by making sure all the letters work together as they did singularly
-
c is just a single char, not an array of char.
You pass it's address to myfunc and then use strlen on it, which you definitely shouldn't do! (Strings need to be '\0' terminated and the only way c can be properly terminated is if it is '\0' itself.)
Maybe make c an array of 2 chars, ensuring that the second is '\0'. Then just pass c, not &c.
Code:
char c[2] = { i, '\0' }; // or just { i } since extra elements are zeroed