So i was working with cryptogram in one of my hands-on exercise provided by my professor. My own version of cryptogram is that in encrypt() it should convert the strings into numbers while the decrypt will convert back the encrypt() results into letter. So, I was hoping that in the decrypt part should convert the encrypt() results into letters.
https://onlinegdb.com/MtyVfAcSI
This is what I wrote in decrypt(), this should convert the numbers in alphabet. For example, I input these numbers 75 102 123 106 102 109 thus, it should show the result 'Jeziel'
void decrypt(int numChar[], int random)
{
int x = 0;
char sMessage[21] = {0};
while ( numChar[x] ) {
sMessage[x] = numChar[x] - random;
x++;
} //end loop
sMessage[x] = '\0';
printf("\nDecrypted Message is: ");
//print the decrypted message
while ( sMessage[x] ) {
printf("%c", sMessage[x]);
x++;
} //end loop
} //end decrypt function
in the main() I do this instead;
char myString[21] = {0};
int decryptnum[21] = {0};
int iSelection = 0;
int iRand;
srand(time(NULL));
iRand = (rand() % 4) + 1; // random #, 1-4
while ( iSelection != 4 )
{
printf("\n\n1\tEncrypt Clear Text\n");
printf("2\tDecrypt Cipher Text\n");
printf("3\tGenerate New Key\n");
printf("4\tQuit\n");
printf("\nSelect a Cryptography Option: ");
scanf("%d", &iSelection);
switch (iSelection) {
case 1:
printf("\nEnter one word as clear text to encrypt: ");
scanf("%s", myString);
encrypt(myString, iRand);
break;
case 2:
printf("\nEnter cipher text to decrypt: ");
x = 0;
while (scanf("%d", &decryptnum[x]) == 1) {
x++;
}
break;
case 3:
iRand = (rand() % 4) + 1; // random #, 1-4
printf("\nNew Key Generated\n");
break;
} //end switch
} //end loop
} //end main
I almost forgot the encrypt();
void encrypt(char sMessage[], int random)
{
int x = 0;
int numChar[21] = {0};
while ( sMessage[x] ) {
numChar[x]=sMessage[x] + random;
x++;
} //end loop
x = 0;
printf("\nEncrypted Message is: ");
//print the encrypted message
while ( numChar[x] ) {
printf("%d ", numChar[x]);
x++;
} //end loop
} //end encrypt function