Advertisement
quantumech

Untitled

Oct 13th, 2020
2,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.45 KB | None | 0 0
  1. #include "c_proto.h"
  2.  
  3. int v[] = {-4,5};
  4.  
  5. int strlen_(char* str)
  6. {
  7.     int ctr = 0;
  8.     while (str[ctr] != '\0')
  9.         ctr++;
  10.    
  11.     return ctr;
  12. }
  13.  
  14. int index_of(char* str, char ch, int start_index)
  15. {
  16.     const int STR_LEN = strlen_(str);
  17.  
  18.     if(start_index >= STR_LEN || start_index < 0)
  19.         return -1;
  20.    
  21.     int i = start_index;
  22.     while(i < STR_LEN)
  23.     {
  24.         if(str[i] == ch)
  25.             return i;
  26.        
  27.         i++;
  28.     }
  29.  
  30.     return -1;
  31. }
  32.  
  33. int to_lowercase(char* str)
  34. {
  35.     const int STR_LEN = strlen_(str);
  36.     int case_count = 0;
  37.  
  38.     for(int i = 0; i < STR_LEN; i++)
  39.     {
  40.         int is_upper = 65 <= str[i] && str[i] <= 90;
  41.         str[i] = str[i] + 32 * is_upper;
  42.         case_count += is_upper;
  43.     }
  44.  
  45.     return case_count;
  46. }
  47.  
  48. int generate_ciphertext_alphabet(char* ciphertext_alphabet, char* keyphrase)
  49. {
  50.     ciphertext_alphabet[62] = '\0';
  51.     int num_chars_drawn = 0;
  52.     const int KEYPHRASE_LEN = strlen_(keyphrase);
  53.     const int CIPHERTEXT_LEN = 61;
  54.     int ptr = 0;
  55.  
  56.     for(int i = 0; i < KEYPHRASE_LEN; i++)
  57.     {
  58.         if(is_alnum(keyphrase[i]) && index_of(ciphertext_alphabet, keyphrase[i], 0) == -1)
  59.         {
  60.             ciphertext_alphabet[ptr] = keyphrase[i];
  61.             ptr++;
  62.         }
  63.     }
  64.  
  65.     num_chars_drawn = ptr;
  66.  
  67.     for(int i = 0; i < 26; i++)
  68.     {
  69.         if(index_of(ciphertext_alphabet, 'a' + i, 0) == -1)
  70.         {
  71.             ciphertext_alphabet[ptr] = 'a' + i;
  72.             ptr++;
  73.         }
  74.     }
  75.  
  76.     for(int i = 0; i < 26; i++)
  77.     {
  78.         if(index_of(ciphertext_alphabet, 'A' + i, 0) == -1)
  79.         {
  80.             ciphertext_alphabet[ptr] = 'A' + i;
  81.             ptr++;
  82.         }
  83.     }
  84.  
  85.     for(int i = 0; i < 10; i++)
  86.     {
  87.         if(index_of(ciphertext_alphabet, '0' + i, 0) == -1)
  88.         {
  89.             ciphertext_alphabet[ptr] = '0' + i;
  90.             ptr++;
  91.         }
  92.     }
  93.  
  94.     return num_chars_drawn;
  95. }
  96.  
  97. int count_lowercase_letters(int counts[], char* message)
  98. {
  99.     int total_count = 0;
  100.     int i = 0;
  101.  
  102.     for(i = 0; i < 26; i++)
  103.     {
  104.         counts[i] = 0;
  105.     }
  106.  
  107.     i = 0;
  108.  
  109.     while(message[i] != '\0')
  110.     {
  111.         if('a' <= message[i] && message[i] <= 'z')
  112.         {
  113.             total_count++;
  114.             counts[message[i] - 'a'] += 1;
  115.         }
  116.         i++;
  117.     }
  118.  
  119.     return total_count;
  120. }
  121.  
  122. void sort_alphabet_by_count(char* sorted_alphabet, int counts[])
  123. {
  124.     int ptr = 0;
  125.  
  126.     sorted_alphabet[26] = '\0';
  127.  
  128.     for(int i = 0; i < 26; i++)
  129.         sorted_alphabet[i] = 'a' + i;
  130.  
  131.     for(int i = 0; i < 26; i++)
  132.     {
  133.         for(int j = i; j < 26; j++)
  134.         {
  135.             if(counts[i] < counts[j])
  136.             {
  137.                 int _c = counts[i];
  138.                 counts[i] = counts[j];
  139.                 counts[j] = _c;
  140.  
  141.                 char _s = sorted_alphabet[i];
  142.                 sorted_alphabet[i] = sorted_alphabet[j];
  143.                 sorted_alphabet[j] = _s;
  144.             }
  145.         }
  146.     }
  147. }
  148.  
  149. void generate_plaintext_alphabet(char* plaintext_alphabet, char* sorted_alphabet)
  150. {
  151.     int ptr = 0;
  152.  
  153.     for(int i = 0; i < 26; i++)
  154.     {
  155.         int char_location = index_of(sorted_alphabet, i + 'a', 0);
  156.  
  157.         if(char_location < 8)
  158.         {
  159.             for(int j = 0; j < 8 - char_location + 1; j++)
  160.             {
  161.                 plaintext_alphabet[ptr] = sorted_alphabet[char_location];
  162.                 ptr++;
  163.             }
  164.         }
  165.         else
  166.         {
  167.             plaintext_alphabet[ptr] = sorted_alphabet[char_location];
  168.             ptr++;
  169.         }
  170.     }
  171.  
  172.     plaintext_alphabet[62] = '\0';
  173. }
  174.  
  175. int encrypt_letter(char plaintext_letter, int letter_index, char* plaintext_alphabet, char* ciphertext_alphabet)
  176. {
  177.     if(plaintext_letter < 'a' || plaintext_letter > 'z')
  178.         return -1;
  179.  
  180.     int n = 0;
  181.     int target_index = index_of(plaintext_alphabet, plaintext_letter, 0);
  182.     int i = target_index;
  183.     while (plaintext_alphabet[i + 1] == plaintext_letter)
  184.     {
  185.         n++;
  186.         i++;
  187.     }
  188.  
  189.     return ciphertext_alphabet[target_index + letter_index % (n + 1)];
  190. }
  191.  
  192. int* encrypt(char* ciphertext, char* plaintext, char* keyphrase, char* corpus)
  193. {
  194.     v[0] = 0;
  195.     v[1] = 0;
  196.  
  197.     to_lowercase(plaintext);
  198.     int counts[26]; // "Allocate atleast 26 words on the stack to store the counts array" - Step 4 of part 9 of HW
  199.     to_lowercase(corpus);
  200.     count_lowercase_letters(counts, corpus);
  201.     char lowercase_letters[27]; // MAKE SURE YOU DEALLOCATE ALL OF THIS WHEN THE METHOD IS COMPLETE. THIS PSEUDOCODE DOES NOT TAKE THIS INTO ACCOUNT
  202.     sort_alphabet_by_count(lowercase_letters, counts);
  203.     char plaintext_alphabet[63];
  204.     generate_plaintext_alphabet(plaintext_alphabet, lowercase_letters);
  205.     char ciphertext_alphabet[63];
  206.     generate_ciphertext_alphabet(ciphertext_alphabet, keyphrase);
  207.  
  208.     int i = 0;
  209.     while(plaintext[i] != '\0')
  210.     {
  211.         if('a' <= plaintext[i] && plaintext[i] <= 'z')
  212.         {
  213.             ciphertext[i] = encrypt_letter(plaintext[i], i, plaintext_alphabet, ciphertext_alphabet); // Guessing what letter index should be. I have no idea.
  214.             v[0]++;
  215.         }
  216.         else
  217.         {
  218.             ciphertext[i] = plaintext[i];
  219.             v[1]++;
  220.         }
  221.         i++;
  222.     }
  223.  
  224.     ciphertext[i] = '\0';
  225.  
  226.     return v;
  227. }
  228.  
  229. int* decrypt(char* plaintext, char* ciphertext, char* keyphrase, char* corpus)
  230. {
  231.     v[0] = 0;
  232.     v[1] = 0;
  233.  
  234.     to_lowercase(corpus);
  235.     int counts[26]; // This prototype does not compensate for the fact that you have to allocate and deallocate this from the stack
  236.     count_lowercase_letters(counts, corpus);
  237.     char lowercase_letters[27];
  238.     sort_alphabet_by_count(lowercase_letters, counts);
  239.     char plaintext_alphabet[63];
  240.     generate_plaintext_alphabet(plaintext_alphabet, lowercase_letters);
  241.     char ciphertext_alphabet[63];
  242.     generate_ciphertext_alphabet(ciphertext_alphabet, keyphrase);
  243.  
  244.     int i = 0;
  245.     while(ciphertext[i] != '\0')
  246.     {
  247.         if(is_alnum(ciphertext[i]))
  248.         {
  249.             plaintext[i] = plaintext_alphabet[index_of(ciphertext_alphabet, ciphertext[i], 0)];
  250.             v[0]++;
  251.         }
  252.         else
  253.         {
  254.             plaintext[i] = ciphertext[i];
  255.             v[1]++;
  256.         }
  257.         i++;
  258.     }
  259.  
  260.     plaintext[i] = '\0';
  261.  
  262.     return v;
  263. }
  264.  
  265. int is_alnum(char c)
  266. {
  267.     return ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement