Options
Another C program
Hello again,
So next programming assignment was for the autokey cipher, I have it working sort of...If I test my own strings it works and well as the example in my book. However when I try to complete an exhaustive key search on a piece of ciphertext from the review problems this string specifically doesn't produce the proper output. Below is my code, I'm just trying to see if I'm missing something or if I'm just losing it.
Ok so the working one I tried from the book is 'RENDEZVOUS' with k = I = ciphertext: ZVRQHDUJIM
The book ciphertext: MALVVMAFBHBUQPTSOXALTGVWWRG
Output when I try k = A ; M, k = B ; L so it's only producing the first char for output.
Output below
Let me know if anyone has any idea? Slowhand here's some good practice for you
So next programming assignment was for the autokey cipher, I have it working sort of...If I test my own strings it works and well as the example in my book. However when I try to complete an exhaustive key search on a piece of ciphertext from the review problems this string specifically doesn't produce the proper output. Below is my code, I'm just trying to see if I'm missing something or if I'm just losing it.
/* Autokey Cipher Joe Hlasnik */ #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXCHARS 1024 #define MAXS 4 void encrypt(); void decrypt(); char *letters[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; int main() { char response[MAXS],plaintext[MAXCHARS]; printf("Autokey Cipher Program\n\nWould you like to (E)ncrypt or (D)ecrypt a message or (Q)uit. "); scanf("%s",response); while (response[0] != 'q' && response[0] != 'Q') { switch (response[0]) { case 'e': case 'E': encrypt(); break; case 'd': case 'D': decrypt(); break; default: printf("error: invalid command\n"); break; }//end of switch printf("Would you like to (E)ncrypt or (D)ecrypt a message or (Q)uit. "); scanf("%s",response); }//end of while return 0; }//end of main void encrypt() { int i=0,k,z,tempk; char msg[MAXCHARS]; char letter[MAXS]; printf("Please enter the plain text to encrypt in all CAPS and press enter\n"); scanf ("%s",msg); printf("Please enter the alpha key(k) in CAPS you would like to use "); scanf ("%s",letter); for (k=0;k<26;k++) if (strcmp(letters[k],letter)==1) //compare to convert k break; k--;//step back one for (z=0;z<strlen(msg);z++) msg[z] = msg[z] - 'A'; while (msg[i]!= '\0') { tempk = msg[i]; if ((msg[i] + k)>26) msg[i] = (msg[i] + k) - 26; else msg[i] = msg[i] + k; k = tempk; i++; } for (k=0;k<strlen(msg);k++) msg[k] = msg[k] + 'A'; printf("Encrypted Text: %s\n",msg); } void decrypt() { int i=0,k,z; char msg[MAXCHARS]; char letter[MAXS]; printf("Please enter the ciper text to decrypt in all CAPS and press enter\n"); scanf ("%s",msg); printf("Please enter the alpha key(k) in CAPS you would like to use "); scanf ("%s",letter); for (k=0;k<26;k++) if (strcmp(letters[k],letter)==1) //compare to convert k break; k--;//step back one for (z=0;z<strlen(msg);z++) msg[z] = msg[z] - 'A'; //printf("%s\n",msg); while (msg[i]!= '\0') { if ((msg[i] - k)<0) msg[i] = (msg[i] - k) + 26; else msg[i] = msg[i] - k; k = msg[i]; i++; } for (k=0;k<strlen(msg);k++) msg[k] = msg[k] + 'A'; printf("Decrypted Text: %s\n",msg); //MALVVMAFBHBUQPTSOXALTGVWWRG }
Ok so the working one I tried from the book is 'RENDEZVOUS' with k = I = ciphertext: ZVRQHDUJIM
The book ciphertext: MALVVMAFBHBUQPTSOXALTGVWWRG
Output when I try k = A ; M, k = B ; L so it's only producing the first char for output.
Output below
Autokey Cipher Program Would you like to (E)ncrypt or (D)ecrypt a message or (Q)uit. E Please enter the plain text to encrypt in all CAPS and press enter RENDEZVOUS Please enter the alpha key(k) in CAPS you would like to use I Encrypted Text: ZVRQHDUJIM Would you like to (E)ncrypt or (D)ecrypt a message or (Q)uit. D Please enter the ciper text to decrypt in all CAPS and press enter ZVRQHDUJIM Please enter the alpha key(k) in CAPS you would like to use I Decrypted Text: RENDEZVOUS Would you like to (E)ncrypt or (D)ecrypt a message or (Q)uit. D Please enter the ciper text to decrypt in all CAPS and press enter MALVVMAFBHBUQPTSOXALTGVWWRG Please enter the alpha key(k) in CAPS you would like to use C Decrypted Text: K Would you like to (E)ncrypt or (D)ecrypt a message or (Q)uit. D Please enter the ciper text to decrypt in all CAPS and press enter MALVVMAFBHBUQPTSOXALTGVWWRG Please enter the alpha key(k) in CAPS you would like to use B Decrypted Text: L Would you like to (E)ncrypt or (D)ecrypt a message or (Q)uit. E Please enter the plain text to encrypt in all CAPS and press enter JOEJOE Please enter the alpha key(k) in CAPS you would like to use X Encrypted Text: GXSNXS Would you like to (E)ncrypt or (D)ecrypt a message or (Q)uit. D Please enter the ciper text to decrypt in all CAPS and press enter GXSNXS Please enter the alpha key(k) in CAPS you would like to use X Decrypted Text: JOEJOE Would you like to (E)ncrypt or (D)ecrypt a message or (Q)uit.
Let me know if anyone has any idea? Slowhand here's some good practice for you
Comments
-
Optionstiersten Member Posts: 4,505It is because there is an A as the second character of MALVVMAFBHBUQPTSOXALTGVWWRG.
Your code is subtracting 'A' from each character of the message. 'A' - 'A' = 0 which therefore null terminates the string and stops further processing later on. Rewrite your code to avoid doing that.