Search code examples
ccaesar-cipher

Decrypting opened textfile with unknown key using ASCII in C Caesar Cipher


So I was wondering how I could decrypt an encrypted textfile that is opened through a command line argument using ASCII and with an unknown key and then printing it all out and with the answer key. I seem to have been able to actually print out the encrypted message but don't have a clue on how to determine how to find the key and print it out.

int main( int argc, char *argv[]) {
  FILE *fp = stdin;  // defaults
  int  n = 13;
  int shift;
  // process command line
  switch (argc) {
    default:
      fp = fopen(argv[1], "r"); // should check for problems
      n  = atoi(argv[2]);
      break;
  }
  // rotate text
  int c, key;
  int i;
  while( (c = fgetc(fp)) != EOF) {
  // This is where I have managed to make C an integer 
  // and print out the encrypted message using the printf function.

Solution

  • Usually, decryption without knowing the key is impossible. Luckily, your message is encrypted with one of the simplest methods possible...
    Caesar cipher encryption works like so:
    * Choose offset K
    * For every letter in message do
    ** Letter = Letter+K

    So if we wanted to break that code, we could just go over all possible values of K (255) and rule out every possibility that generates ASCII codes that are not letters or numbers (assuming the original message is in plain English).

    You might still need some user interaction to decide if there is more than one option, but the options will be limited.