Code:
int myfunc( char *, int, int , int , int );
void PrintGuesses( int [][26], int );
//void TestPossPword( char *, char *, int [][26], int );
int main()
{
char text[] = "The quick brown fox jumps, over the lazy dog!";
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 - 1 ][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++ )
{
if ( myfunc( encrypted, i, StartIndex, len, plen ) )
{
Guesses[StartIndex][GuessIndex] = i;
GuessIndex++;
}
}
//putchar('\n');
}
PrintGuesses( Guesses, plen );
//printf("%s\t%s\t%s\n", "1st", "2nd", "3rd");
//TestPossPword( encrypted, pword, Guesses, plen );
for ( int i = 0; Guesses[0][i] != 0; i++ ) //first letter
for ( int j = 0; Guesses[1][j] != 0; j++ ) // second letter
for ( int k = 0; Guesses[2][k] != 0; k++ ) //third letter
{
char GuessedPword[sizeof pword ] = { '\0' };
GuessedPword[0] = Guesses[0][i];
GuessedPword[1] = Guesses[1][j];
GuessedPword[2] = Guesses[2][k];
printf("%d\t%d\t%d = %s\n", i, j, k, GuessedPword);
char Decrypted[sizeof encrypted] = { '\0' };
for ( int y = 0, x = 0; x < sizeof encrypted - 1; x++, y = (y + 1) % plen )
{
char c = encrypted[x] ^ GuessedPword[y];
Decrypted[x] = c;
printf("%c", Decrypted[x]);
}
putchar('\n');
if ( ( strstr( Decrypted, "The" ) != NULL ) || ( strstr( Decrypted, "the" ) != NULL ) )
{
printf("Password Found!!\n");
printf("Text = %s\n", Decrypted);
}
}
return 0;
}
int myfunc( char *Encrypted, int i, int StartIndex, int len, int plen )
{
int Letter = 1;
for ( int j = StartIndex; j < len; j += plen )
{
int x = Encrypted[j] ^ i;
if ( ! ( ( x >= 'A' && x <= 'Z' ) || ( x >= 'a' && x <= 'z' ) || ( x >= ' ' && x <= '@' ) ) )
{
Letter = 0;
break;
}
}
return Letter;
}
void PrintGuesses( int Guesses[][26], int plen )
{
for ( int i = 0; i < plen; i++ )
{
printf("%d ", i);
for ( int j = 0; j < 26; j++ )
{
if ( Guesses[i][j] == 0 )
break;
printf("%c ", Guesses[i][j]);
}
putchar('\n');
}
}