TheFuzzyFish

caesarCracker.c

Sep 11th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. /*
  2. Caesar Cracker:
  3. This program prints every possible Caesar Cipher
  4. for a given input. It's primary use case is in
  5. CTF or other "hacking" related competitions. Simply
  6. look through the output for real words.
  7.  
  8. Written by Zachary Kline (www.ZachKline.us)
  9. September 11, 2019
  10. */
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15.  
  16. void main(void) {
  17.     char str[1024];
  18.     int i, x;
  19.  
  20.     /* Accepts user input */
  21.     printf("====================\nCaesar Cipher Cracker\nWritten by Zachary Kline\n====================\n");
  22.     printf("Please enter a ciphered string (max 1024 characters): ");
  23.     scanf("%[^\n]s", str); // The "[^\n] ensures that spaces are scanned in as well, so that you can paste in a whole sentence
  24.     printf("\n");
  25.  
  26.     for (i = 1; i <= 26; ++i) {
  27.         for (x = 0; str[x] != '\0'; ++x) {
  28.             /*
  29.             I had to make 2 separate if statements for
  30.             whether the character was between a-z or A-Z
  31.             so that we didn't accidentally start overflowing
  32.             into numbers or symbols. This also had the added
  33.             benefit of leaving numbers and symbols from the
  34.             input untouched in the output.
  35.             */
  36.             if (str[x] >= 'a' && str[x] <= 'z') {
  37.                 str[x] = str[x] + 1;
  38.                 /*
  39.                 Next if statement corrects the output in
  40.                 case we accidentally fell outside of the
  41.                 alphabet by adding 1
  42.                 */
  43.                 if (str[x] > 'z') {
  44.                     str[x] = str[x] + 'a' - 'z' - 1;
  45.                 }
  46.             }
  47.             /*
  48.             This next if statement is just a copy of
  49.             the previous one, but for the capital alphabet
  50.             */
  51.             if (str[x] >= 'A' && str[x] <= 'Z') {
  52.                 str[x] = str[x] + 1;
  53.                 if (str[x] > 'Z') {
  54.                     str[x] = str[x] + 'A' - 'Z' - 1;
  55.                 }
  56.             }
  57.         }
  58.         printf("+%d:\t%s\n", i, str);
  59.     }
  60. }
Add Comment
Please, Sign In to add comment