Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "c_proto.h"
- int v[] = {-4,5};
- int strlen_(char* str)
- {
- int ctr = 0;
- while (str[ctr] != '\0')
- ctr++;
- return ctr;
- }
- int index_of(char* str, char ch, int start_index)
- {
- const int STR_LEN = strlen_(str);
- if(start_index >= STR_LEN || start_index < 0)
- return -1;
- int i = start_index;
- while(i < STR_LEN)
- {
- if(str[i] == ch)
- return i;
- i++;
- }
- return -1;
- }
- int to_lowercase(char* str)
- {
- const int STR_LEN = strlen_(str);
- int case_count = 0;
- for(int i = 0; i < STR_LEN; i++)
- {
- int is_upper = 65 <= str[i] && str[i] <= 90;
- str[i] = str[i] + 32 * is_upper;
- case_count += is_upper;
- }
- return case_count;
- }
- int generate_ciphertext_alphabet(char* ciphertext_alphabet, char* keyphrase)
- {
- ciphertext_alphabet[62] = '\0';
- int num_chars_drawn = 0;
- const int KEYPHRASE_LEN = strlen_(keyphrase);
- const int CIPHERTEXT_LEN = 61;
- int ptr = 0;
- for(int i = 0; i < KEYPHRASE_LEN; i++)
- {
- if(is_alnum(keyphrase[i]) && index_of(ciphertext_alphabet, keyphrase[i], 0) == -1)
- {
- ciphertext_alphabet[ptr] = keyphrase[i];
- ptr++;
- }
- }
- num_chars_drawn = ptr;
- for(int i = 0; i < 26; i++)
- {
- if(index_of(ciphertext_alphabet, 'a' + i, 0) == -1)
- {
- ciphertext_alphabet[ptr] = 'a' + i;
- ptr++;
- }
- }
- for(int i = 0; i < 26; i++)
- {
- if(index_of(ciphertext_alphabet, 'A' + i, 0) == -1)
- {
- ciphertext_alphabet[ptr] = 'A' + i;
- ptr++;
- }
- }
- for(int i = 0; i < 10; i++)
- {
- if(index_of(ciphertext_alphabet, '0' + i, 0) == -1)
- {
- ciphertext_alphabet[ptr] = '0' + i;
- ptr++;
- }
- }
- return num_chars_drawn;
- }
- int count_lowercase_letters(int counts[], char* message)
- {
- int total_count = 0;
- int i = 0;
- for(i = 0; i < 26; i++)
- {
- counts[i] = 0;
- }
- i = 0;
- while(message[i] != '\0')
- {
- if('a' <= message[i] && message[i] <= 'z')
- {
- total_count++;
- counts[message[i] - 'a'] += 1;
- }
- i++;
- }
- return total_count;
- }
- void sort_alphabet_by_count(char* sorted_alphabet, int counts[])
- {
- int ptr = 0;
- sorted_alphabet[26] = '\0';
- for(int i = 0; i < 26; i++)
- sorted_alphabet[i] = 'a' + i;
- for(int i = 0; i < 26; i++)
- {
- for(int j = i; j < 26; j++)
- {
- if(counts[i] < counts[j])
- {
- int _c = counts[i];
- counts[i] = counts[j];
- counts[j] = _c;
- char _s = sorted_alphabet[i];
- sorted_alphabet[i] = sorted_alphabet[j];
- sorted_alphabet[j] = _s;
- }
- }
- }
- }
- void generate_plaintext_alphabet(char* plaintext_alphabet, char* sorted_alphabet)
- {
- int ptr = 0;
- for(int i = 0; i < 26; i++)
- {
- int char_location = index_of(sorted_alphabet, i + 'a', 0);
- if(char_location < 8)
- {
- for(int j = 0; j < 8 - char_location + 1; j++)
- {
- plaintext_alphabet[ptr] = sorted_alphabet[char_location];
- ptr++;
- }
- }
- else
- {
- plaintext_alphabet[ptr] = sorted_alphabet[char_location];
- ptr++;
- }
- }
- plaintext_alphabet[62] = '\0';
- }
- int encrypt_letter(char plaintext_letter, int letter_index, char* plaintext_alphabet, char* ciphertext_alphabet)
- {
- if(plaintext_letter < 'a' || plaintext_letter > 'z')
- return -1;
- int n = 0;
- int target_index = index_of(plaintext_alphabet, plaintext_letter, 0);
- int i = target_index;
- while (plaintext_alphabet[i + 1] == plaintext_letter)
- {
- n++;
- i++;
- }
- return ciphertext_alphabet[target_index + letter_index % (n + 1)];
- }
- int* encrypt(char* ciphertext, char* plaintext, char* keyphrase, char* corpus)
- {
- v[0] = 0;
- v[1] = 0;
- to_lowercase(plaintext);
- int counts[26]; // "Allocate atleast 26 words on the stack to store the counts array" - Step 4 of part 9 of HW
- to_lowercase(corpus);
- count_lowercase_letters(counts, corpus);
- char lowercase_letters[27]; // MAKE SURE YOU DEALLOCATE ALL OF THIS WHEN THE METHOD IS COMPLETE. THIS PSEUDOCODE DOES NOT TAKE THIS INTO ACCOUNT
- sort_alphabet_by_count(lowercase_letters, counts);
- char plaintext_alphabet[63];
- generate_plaintext_alphabet(plaintext_alphabet, lowercase_letters);
- char ciphertext_alphabet[63];
- generate_ciphertext_alphabet(ciphertext_alphabet, keyphrase);
- int i = 0;
- while(plaintext[i] != '\0')
- {
- if('a' <= plaintext[i] && plaintext[i] <= 'z')
- {
- ciphertext[i] = encrypt_letter(plaintext[i], i, plaintext_alphabet, ciphertext_alphabet); // Guessing what letter index should be. I have no idea.
- v[0]++;
- }
- else
- {
- ciphertext[i] = plaintext[i];
- v[1]++;
- }
- i++;
- }
- ciphertext[i] = '\0';
- return v;
- }
- int* decrypt(char* plaintext, char* ciphertext, char* keyphrase, char* corpus)
- {
- v[0] = 0;
- v[1] = 0;
- to_lowercase(corpus);
- int counts[26]; // This prototype does not compensate for the fact that you have to allocate and deallocate this from the stack
- count_lowercase_letters(counts, corpus);
- char lowercase_letters[27];
- sort_alphabet_by_count(lowercase_letters, counts);
- char plaintext_alphabet[63];
- generate_plaintext_alphabet(plaintext_alphabet, lowercase_letters);
- char ciphertext_alphabet[63];
- generate_ciphertext_alphabet(ciphertext_alphabet, keyphrase);
- int i = 0;
- while(ciphertext[i] != '\0')
- {
- if(is_alnum(ciphertext[i]))
- {
- plaintext[i] = plaintext_alphabet[index_of(ciphertext_alphabet, ciphertext[i], 0)];
- v[0]++;
- }
- else
- {
- plaintext[i] = ciphertext[i];
- v[1]++;
- }
- i++;
- }
- plaintext[i] = '\0';
- return v;
- }
- int is_alnum(char c)
- {
- return ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement