/* Author: Pate Williams (c) 1997 Exercise 1.10 "Decrypt the following ciphertext, obtained from the Autokey Cipher, by using exhaustive key search: MALVVMAFBHBUQPTSOXALTGVWWRG" -Douglas R. Stinson- See "Cryptography: Theory and Practice" by Douglas R. Stinson page 43. */ #include #include #include int main(void) { char answer[256]; char ciphertext[] = "MALVVMAFBHBUQPTSOXALTGVWWRG"; int found = 0; long count = strlen(ciphertext), i, j, x, y; for (i = 0; i < count; i++) ciphertext[i] -= (char) 'A'; for (i = 0; !found && i < 26; i++) { x = (ciphertext[0] - i) % 26; if (x < 0) x += 26; printf("%c", x + 'A'); for (j = 1; j < count; j++) { y = (ciphertext[j] - x) % 26; if (y < 0) y += 26; printf("%c", y + 'A'); x = y; } printf("\n"); printf("another key = %c (n or y)? ", i + 'A'); scanf("%s", answer); found = tolower(answer[0]) == 'n'; } return 0; }