Advertisement
Guest User

Amelia Surprise Code!

a guest
Apr 25th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. char decrypt(int in, int key) {
  6.     in = tolower(in) - 'a'; // convert the letter to a number 0-25
  7.  
  8.     in = (in - key); // subtract the key
  9.     while(in < 0) in += 26; // Add 26 until we are above 0
  10.     // This is because mod doesn't work properly for negatives
  11.    
  12.     //in = (in + key) % 26; // Add the key and mod 26 to make sure it's a letter
  13.    
  14.     return (char)in + 'a'; // Convert the 0-25 number back into a letter and return
  15. }
  16.  
  17. int main(int argc, char *argv[]) {
  18.     if(argc != 2) return 1; // get file in command line
  19.  
  20.     int a1[] = {1, 3, 1, 3, -1};                 // time
  21.     int a2[] = {2, 1, 0, 2, 2, 0, 1, 8, -1};     // date
  22.     int a3[] = {5, 4, 1, 17, 20, 0, 17, 24, -1}; // month
  23.  
  24.     char c[2048] = {0}; // ciphertext
  25.     char p[2048] = {0}; // plaintext
  26.     int count = 0, read, total = 0;
  27.     int i,j,k; // array counters
  28.     i=j=k= 0;  // Set all counters to 0
  29.  
  30.     FILE *file = fopen(argv[1], "r"); // open the file
  31.     if(file == NULL) return 1; // Make sure file opened
  32.     while( (read = fread(c + total, 1, 2048-total, file)) > 0) // read the file into buffer
  33.         total += read; // add number of bytes read to the total
  34.     fclose(file); // close the file
  35.  
  36.     while(count < total) { // loop until end of ciphertext
  37.         if(!isalpha(c[count])) { // If the character is non-alphabetic
  38.             p[count] = c[count]; // Set the plaintext
  39.             count ++; // Move to next character
  40.             continue; // keep looping
  41.         }
  42.  
  43.        
  44.         i++;
  45.         if(a1[i] == -1) { // check if a1 is at end
  46.             j++;
  47.             if(a2[j] == -1) { // check if a2 is at end
  48.                 k++;
  49.                 if(a3[k] == -1) { // check if a3 is at end
  50.                     k = 0; // reset a3
  51.                 }
  52.                 j = 0; // reset a2
  53.             }
  54.             i = 0; // reset a1
  55.         }
  56.  
  57.         // decrypt the ciphertext by multiplying all keys together
  58.         p[count] = decrypt(c[count], a1[i] * a2[j] * a3[k]);
  59.         count++; // Move to next character
  60.     }
  61.  
  62.     // Print the result
  63.     printf("%s\n", p);
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement