patterncMinor
Shorten this Caesar Cipher cracker
Viewed 0 times
thiscaesarcrackershortencipher
Problem
My program has to decrypt ciphertext. It has to try all possible 26 shifts and store each in an array so that I calculate the frequencies of each array and find the highest frequency, in which that would result in the plain text.
```
#include
#define CIPHERSIZE 42
char decryptCiphertext(char data[], double data2[]);
char likelyPlaintext (char decrypt[], double data2[]);
int main (void)
{
char cipher[43];
printf("\t===========================\n");
printf(" \t DECRYPTING CIPHERTEXT\n");
printf("\t===========================\n");
FILE *ciphertext;
ciphertext = fopen("ciphertext.txt", "r");
if (ciphertext != NULL)
{
while (!feof(ciphertext))
fscanf(ciphertext, "%s", cipher);
fclose(ciphertext);
}
else
printf("Error opening file\n");
printf("Ciphertext : %s\n", cipher);
FILE *frequency;
double freq[26];
double num;
int i;
frequency = fopen("frequencies.dat", "r");
if (frequency == NULL)
printf("Error opening file\n");
for (i=0; i < 26; i++)
{
fscanf(frequency, "%lf", &freq[i]);
//printf("%lf\n", freq[i]);
}
fclose(frequency);
decryptCiphertext(cipher, freq);
}
char decryptCiphertext(char data[], double data2[])
{
int i, convert, shiftingLetter;
char decrypted26[42], decrypted1[42], decrypted2[42], decrypted3[42], decrypted4[42],
decrypted5[42], decrypted6[42], decrypted7[42], decrypted8[42], decrypted9[42], decrypted10[42],
decrypted11[42], decrypted12[42], decrypted13[42], decrypted14[42], decrypted15[42], decrypted16[42],
decrypted17[42], decrypted18[42], decrypted19[42], decrypted20[42], decrypted21[42], decrypted22[42],
decrypted23[42], decrypted24[42], decrypted25[42];
//Try all possible shifts from 1-25 and store each in an array
for (i = 0; data[i] != '\0'; i++)
{
convert = data[i] - 'A'; //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc
convert = (convert + 1); //add shift
shiftingLetter
```
#include
#define CIPHERSIZE 42
char decryptCiphertext(char data[], double data2[]);
char likelyPlaintext (char decrypt[], double data2[]);
int main (void)
{
char cipher[43];
printf("\t===========================\n");
printf(" \t DECRYPTING CIPHERTEXT\n");
printf("\t===========================\n");
FILE *ciphertext;
ciphertext = fopen("ciphertext.txt", "r");
if (ciphertext != NULL)
{
while (!feof(ciphertext))
fscanf(ciphertext, "%s", cipher);
fclose(ciphertext);
}
else
printf("Error opening file\n");
printf("Ciphertext : %s\n", cipher);
FILE *frequency;
double freq[26];
double num;
int i;
frequency = fopen("frequencies.dat", "r");
if (frequency == NULL)
printf("Error opening file\n");
for (i=0; i < 26; i++)
{
fscanf(frequency, "%lf", &freq[i]);
//printf("%lf\n", freq[i]);
}
fclose(frequency);
decryptCiphertext(cipher, freq);
}
char decryptCiphertext(char data[], double data2[])
{
int i, convert, shiftingLetter;
char decrypted26[42], decrypted1[42], decrypted2[42], decrypted3[42], decrypted4[42],
decrypted5[42], decrypted6[42], decrypted7[42], decrypted8[42], decrypted9[42], decrypted10[42],
decrypted11[42], decrypted12[42], decrypted13[42], decrypted14[42], decrypted15[42], decrypted16[42],
decrypted17[42], decrypted18[42], decrypted19[42], decrypted20[42], decrypted21[42], decrypted22[42],
decrypted23[42], decrypted24[42], decrypted25[42];
//Try all possible shifts from 1-25 and store each in an array
for (i = 0; data[i] != '\0'; i++)
{
convert = data[i] - 'A'; //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc
convert = (convert + 1); //add shift
shiftingLetter
Solution
decryptCiphertext can be reduced to
char decryptCiphertext(char data[], double data2[])
{
int i, shift, convert, shiftingLetter;
char decrypted[26][CIPHERSIZE];
for (shift = 1; shift < 26; shift++)
{
//Try all possible shifts from 1-25 and store each in an array
for (i = 0; data[i] != '\0'; i++)
{
convert = data[i] - 'A'; //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc
convert = (convert + shift); //add shift
shiftingLetter = convert % 26; //cycle around
decrypted[shift][i] = 'A' + shiftingLetter; //store new letter in array
}
decrypted[shift][i] = '\0';
likelyPlaintext(decrypted[shift], data2);
}
}Code Snippets
char decryptCiphertext(char data[], double data2[])
{
int i, shift, convert, shiftingLetter;
char decrypted[26][CIPHERSIZE];
for (shift = 1; shift < 26; shift++)
{
//Try all possible shifts from 1-25 and store each in an array
for (i = 0; data[i] != '\0'; i++)
{
convert = data[i] - 'A'; //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc
convert = (convert + shift); //add shift
shiftingLetter = convert % 26; //cycle around
decrypted[shift][i] = 'A' + shiftingLetter; //store new letter in array
}
decrypted[shift][i] = '\0';
likelyPlaintext(decrypted[shift], data2);
}
}Context
StackExchange Code Review Q#24833, answer score: 5
Revisions (0)
No revisions yet.