Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- char decrypt(int in, int key) {
- in = tolower(in) - 'a'; // convert the letter to a number 0-25
- in = (in - key); // subtract the key
- while(in < 0) in += 26; // Add 26 until we are above 0
- // This is because mod doesn't work properly for negatives
- //in = (in + key) % 26; // Add the key and mod 26 to make sure it's a letter
- return (char)in + 'a'; // Convert the 0-25 number back into a letter and return
- }
- int main(int argc, char *argv[]) {
- if(argc != 2) return 1; // get file in command line
- int a1[] = {1, 3, 1, 3, -1}; // time
- int a2[] = {2, 1, 0, 2, 2, 0, 1, 8, -1}; // date
- int a3[] = {5, 4, 1, 17, 20, 0, 17, 24, -1}; // month
- char c[2048] = {0}; // ciphertext
- char p[2048] = {0}; // plaintext
- int count = 0, read, total = 0;
- int i,j,k; // array counters
- i=j=k= 0; // Set all counters to 0
- FILE *file = fopen(argv[1], "r"); // open the file
- if(file == NULL) return 1; // Make sure file opened
- while( (read = fread(c + total, 1, 2048-total, file)) > 0) // read the file into buffer
- total += read; // add number of bytes read to the total
- fclose(file); // close the file
- while(count < total) { // loop until end of ciphertext
- if(!isalpha(c[count])) { // If the character is non-alphabetic
- p[count] = c[count]; // Set the plaintext
- count ++; // Move to next character
- continue; // keep looping
- }
- i++;
- if(a1[i] == -1) { // check if a1 is at end
- j++;
- if(a2[j] == -1) { // check if a2 is at end
- k++;
- if(a3[k] == -1) { // check if a3 is at end
- k = 0; // reset a3
- }
- j = 0; // reset a2
- }
- i = 0; // reset a1
- }
- // decrypt the ciphertext by multiplying all keys together
- p[count] = decrypt(c[count], a1[i] * a2[j] * a3[k]);
- count++; // Move to next character
- }
- // Print the result
- printf("%s\n", p);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement