[SOLVED] CSE220-Programming Assignment 2

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon HW-2-rgo1d9.zip (183.3 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
πŸ”’ Securely Powered by:
Secure Checkout
5/5 - (2 votes)

For this assignment you will be implementing several algorithms for working with ASCII strings, as well as encryption and decryption algorithms for a variant of the homophonic substitution ciphe​              r. In this​              cipher, some of the letters from the plaintext​      ​ (the input message) can be replaced with one of several different possible letters to generate the ciphertext​       ​ (the encrypted output message). This is in contrast with a traditional substitution cipher, wherein there is only one option to substitute for each plaintext letter. If you are unfamiliar with substitution ciphers, we recommend you briefly review the Wikipedi​   a article on the topic.​

Β 

As a concrete example, suppose the plaintext alphabet​ ​ is:

Β 

aaaaaaabcdeeeeeeeeefghhiiiijklmnnnnnoooooopqrrrsttttttttuvwxyz

Β 

and the ciphertext alphabet​ ​ is:

Β 

WhatsSewolfImAOF20BUCVD19ZEdinbcgjkpqruvxyzGHJKLMNPQRTXY345678

Β 

The above alphabets indicate that β€˜a’ can be replaced any of the characters β€œWhatsSe​         ” in the​ ciphertext; β€˜n’ can be replaced with any of the characters β€œcgjkp​               ”, and so on;​

Β 

In our cipher, encryption and decryption require two secret pieces of information: a keyphrase​         ​, which is intended to be an easily-remembered phrase or sentence, and a longer corpus​     ​, which is at least several sentences of text from a document that can be easily obtained (e.g., from a library, the Web, etc.) In a homophonic substitution cipher, the idea is that a letter which occurs frequently in English text can be substituted with any of several possible characters, not just the same character every time. This makes it harder to crack the code. Less-frequently occurring letters might be substituted with only a handful of letters in the ciphertext, or perhaps only even one. The corpus text is used in the frequency analysis to decide how many different possible substitutions could be performed for each plaintext letter. The keyphrase determines the order of the symbols in the ciphertext alphabet, which contains the characters which will be substituted for the plaintext letters. All of this will become clearer as we work through an example.

Β 

Encryption Phase 1: Create the Ciphertext Alphabet using the Keyphrase

Β 

Our ciphertext alphabet contains 62 symbols that will be used to replace the plaintext letters: 26 lowercase letters, 26 uppercase letters and 10 digit characters provide the 62 symbols.

Β 

  1. Initialize an empty ciphertext alphabet consisting of 62 bytes.
  2. Draw letters and digit characters one at a time from the keyphrase, left-to-right, possibly adding each character to the ciphertext alphabet. Ignore spaces and punctuation marks.
    1. If a drawn character has not already been added to the ciphertext alphabet, then add it to the end.
    2. Otherwise, skip that character and move on to the next character of the keyphrase.
  3. After drawing all alphanumeric characters from the keyphrase, determine which lowercase letters do not appear in the ciphertext alphabet. Add these missing letters in alphabetical order to the ciphertext alphabet.
  4. Repeat step 3, but for uppercase letters.
  5. Repeat step 3. but for digit characters.

Β 

As an example, suppose the keyphrase is:

Β 

What’s a Seawolf? I’m a SeAwOlF! Fall 2020 SBU: COVID-19 Zoom Edition

Β 

After step 2, the (incomplete) ciphertext alphabet will consist of these characters:

Β 

WhatsSewolfImAOF20BUCVD19ZEdin

Β 

After step 3, the missing lowercase letters have been appended to yield:

Β 

WhatsSewolfImAOF20BUCVD19ZEdinbcgjkpqruvxyz​

Β 

After step 4, the missing uppercase letters have been appended to yield:

Β 

WhatsSewolfImAOF20BUCVD19ZEdinbcgjkpqruvxyz​  GHJKLMNPQRTXY​

Β 

After step 5, the missing digit characters have been appended to yield the final ciphertext alphabet:

Β 

WhatsSewolfImAOF20BUCVD19ZEdinbcgjkpqruvxyz​  GHJKLMNPQRTXY​                                                                   345678​

Β 

Encryption Phase 2: Compute the Frequency of Letters in the Corpus and Assign Substitutions

Β 

In this phase, we first count the number of times each letter occurs in the corpus, ignoring case. Then, we sort the letters into descending order by count. In other words, the most frequently occurring letters are placed near the front of the sorted alphabet. If two letters have the same frequency, the one with the lower ASCII value is placed in front of the other.

Β 

Consider the following text, used as the corpus:

Β 

Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

Β 

This text generates the following case-insensitive counts:

Β 

a 46Β Β  b 3Β Β  c 14Β Β  d 21Β Β  e 57Β Β  f 9Β Β  g 13Β Β  h 23Β Β  i 32Β Β  j 0Β Β  k

0 l 14Β Β  m 4Β Β  n 35Β Β  o 37Β Β  pΒ  6Β Β  q 1Β Β  r 28Β Β  s 16Β Β  t 52Β Β  u 6Β Β  v

8 wΒ  8Β Β  x 0Β Β  yΒ  3Β Β  zΒ  0

Β 

Sorting the letters according the criteria explained above yields the string:

Β 

etaonirhdsclgfvwpumbyqjkxz

Β 

Next, to generate the plaintext alphabet, we duplicate the most frequently occurring letter eight times, the second most frequently occurring letter seven times, …, and the eighth most frequently occurring letter once. (Note that 8+7+6+5+4+3+2+1 = 36 and that 26+36 = 62.) For our example, we obtain:

Β 

aaaaaaabcdeeeeeeeeefghhiiiijklmnnnnnoooooopqrrrsttttttttuvwxyz

Β 

Note that β€˜e’ is repeated eight times, the β€˜t’ is repeated seven times, the β€˜a’ is repeated six times, etc. The β€˜h’, which is the eighth most frequently occurring letter, is repeated only once. All other letters appear only once each. Note that the plaintext alphabet contains only lowercase letters. This means that any uppercase letters in the input message must be changed to lowercase during the encryption process.

Β 

Encryption Phase 3: Perform Substitutions to Generate the Ciphertext

Β 

Now we lay the plaintext alphabet on top of the ciphertext alphabet:

Β 

aaaaaaabcdeeeeeeeeefghhiiiijklmnnnnnoooooopqrrrsttttttttuvwxyz

WhatsSewolfImAOF20BUCVD19ZEdinbcgjkpqruvxyzGHJKLMNPQRTXY345678

Β 

We see that, in theory, β€˜a’ can be replaced with any of the seven characters from the string β€œWhatsSe”; β€˜b’ can be replaced only with β€˜w’; β€˜c’ can be replaced only with β€˜o’; β€˜d’ can be replaced only with β€˜l’; β€˜e’ can be replaced with any of the nine characters from β€œfImAOF20B”, and so on. To make this selection process deterministic (non-random), we will use the index of a character from the plaintext modulo ​     ​the number of instances of a character to choose the substituted character from the ciphertext alphabet. For instance, suppose there is a letter β€˜e’ at index 12 of the plaintext. Note that there are nine instances of β€˜e’ in the ciphertext alphabet. 12 mod 9 = 3, so we take the character at index 3 of the string

β€œfImAOF20B”, which is β€˜A’. Note that in all computations we assume that indexes strings and substrings are 0-based. Namely, the leftmost character of the plaintext is 0; the characters of the plaintext alphabet and ciphertext alphabet are indexed 0 through 61 (inclusive); substrings drawn from the ciphertext alphabet are indexed starting from 0, etc. Finally, note that only lowercase letters from the plaintext are encrypted; all other characters (punctuation marks, spaces, etc.) are merely copied to the ciphertext unencrypted. We will assume for this assignment that the plaintext message will never contain digits.

Β 

Part 1: Compute the Length of a Null-terminated String

Β 

int strlen(string str)

Β 

This function takes a null-terminated string (possibly empty) and returns its length (i.e., the number of characters in the string). The null-terminator is guaranteed to be present and is not counted in the length.

Β 

The function takes one argument:

  • str:​ the starting address of a null-terminated string

Β 

Returns in $v0:

  • The number of characters in the string, not including the null-terminator.

Β 

Additional requirements:

  • The function must not write any changes to main memory.

Β 

Examples:

Β 

Function Argument Return Value
β€œWolfie Seawolf!!! 2020??” 24
β€œMIPS” 4
β€œβ€Β Β Β Β Β  (​ empty string, containing only \0) 0

Β 

Β 

Part 2: Find the Index of a Character in a Null-terminated String

Β 

int index_of(string str, char ch, int start_index)

Β 

The function returns the index of the first instance of ​printable character​ ​ch​ in the null-terminated string str​, starting the search at index ​start_index​ and moving to the right (towards higher indexes). If the character is not present in the string or if ​start_index​ is not a valid index for ​str​, the function returns -1. It is possible that the string is empty.

Β 

The function takes the following arguments, in this order:

  • str​: the starting address of a null-terminated string
  • ch​: the printable character to search for
  • start_index​: the index at which the search begins

Β 

Returns in $v0:

  • The index of the leftmost occurrence of ​ch​ in ​str​ found at index ​start_index​ or to the right of ​start_index​; or -1 if ​ch​ is not found during the search or if ​start_index​ is not a valid index for ​str​.

Β 

Additional requirements:

  • The function must not write any changes to main memory.
  • The function must call ​strlen​.

Β 

Examples:

Β 

Function Arguments Return Value
β€œCSE 220 COVID-19 Edition”, β€˜V’, 3 10
β€œCSE 220 COVID-19 Edition”, β€˜V’, 13 -1
β€œCSE 220 COVID-19 Edition”, β€˜n’, 5 23
β€œCSE 220 COVID-19 Edition”, β€˜n’, -5 -1
β€œβ€, β€˜z’, 0 -1

Β 

Β 

Part 3: Convert a Null-terminated String to Lowercase

Β 

int to_lowercase(string str)

Β 

This function takes a null-terminated string (possibly empty) and changes all of its uppercase letters to lowercase. All other characters in the string remain unchanged.

Β 

The function takes one argument:

  • str:​ the starting address of a null-terminated string

Β 

Returns in $v0:

  • The number of letters changed from uppercase to lowercase.

Β 

Additional requirements:

  • The function must not write any changes to main memory except for str​ .​

Β 

Examples:

Β 

Function Arguments Return Value
β€œStony Brook University” 3
β€œUNIVERSITY” 10
β€œ2020-2021” 0
β€œβ€ 0

Β 

Β 

Part 4: Generate the Ciphertext Alphabet from a Keyphrase

Β 

int generate_ciphertext_alphabet(string ciphertext_alphabet,Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  string keyphrase)

Β 

This function generates the ciphertext alphabet as described in Encryption Phase 1 in the Preliminaries section of the document above. The function must null-terminate ciphertext_alphabet​        by writing a​      null-terminator at ciphertext_alphabet[62]​            .​

Β 

The function takes the following arguments, in this order:

  • ciphertext_alphabet:​ an uninitialized buffer of 63 contiguous bytes of main memory where the function writes the ciphertext alphabet.
  • keyphrase:​ a non-empty, null-terminated string to serve as the keyphrase

Β 

Returns in $v0:

  • The number of unique, case-sensitive alphanumerical characters drawn from keyphrase​ .​

Β 

Additional requirements:

  • The function must not write any changes to main memory except for ciphertext_alphabet​ .​ For example, the string keyphrase​               must remain unchanged.​

Β 

Example #1:

Β 

keyphrase = β€œStony Brook University” Resulting ciphertext_alphabet:​

β€œStonyBrkUivesabcdfghjlmpquwxzACDEFGHIJKLMNOPQRTVWXYZ0123456789

”

Β 

Returns in $v0: 13​

Β 

Example #2:

Β 

keyphrase = β€œMonday, September 21st, 2020 4:39 PM EDT” Resulting ciphertext_alphabet​    :​

β€œMondaySeptmbr21s0439PEDTcfghijklquvwxzABCFGHIJKLNOQRUVWXYZ5678

”

Β 

Returns in $v0: 24​

Β 

Example #3:

Β 

keyphrase = β€œsuPeRcalIfrAgiListICexPiaLIdoCIOus” Resulting ciphertext_alphabet​   :​

β€œsuPeRcalIfrAgiLtCxdoObhjkmnpqvwyzBDEFGHJKMNQSTUVWXYZ0123456789”

Β 

Returns in $v0: 21​

Β 

Β 

Part 5: Count the Occurrences of Each Lowercase Letter in a String

Β 

int count_lowercase_letters(int[] counts, string message)

Β 

This function counts the number of times each lowercase letter occurs in ​message​, storing those counts in ​counts​. Specifically, ​counts[0]​ stores the number of instances of β€˜a’ in ​message​, counts[1]​ the number of instances of β€˜b’ in ​message​, etc. Note that each element in ​counts​ is a 4-byte integer. ​counts​ is not initialized with zeros when the function is called.

Β 

The function takes the following arguments, in this order:

  • counts​: an uninitialized buffer of 26 contiguous words of main memory where the function writes the counts of the lowercase letters.
  • message​: a null-terminated string (possibly empty) that could consist of any characters with ASCII codes 32 through 126, inclusive

Β 

Returns in $v0:

  • The total number of lowercase letters in ​message​.

Β 

Additional requirements:

  • The function must not write any changes to main memory except for ​counts​. For example, the string ​message​ must remain unchanged.

Β 

Example #1:

Β 

message = β€œThe specialization in artificial intelligence and data science emphasizes modern approaches for building intelligent systems using machine learning.”

Β 

Resulting ​counts​: ​12 1 7 4 16 2 5 4 18 0 0 8 4 14 4 4 0 5 9 7 2 0 0 0 1 2

Β 

which means:

Β 

a:12Β Β  b: 1Β Β  c: 7Β Β  d: 4Β Β  e:16Β Β  f: 2Β Β  g: 5Β Β  h: 4Β Β  i:18Β Β  j: 0Β Β  k: 0 l: 8Β Β  m: 4Β Β  n:14Β Β  o: 4 Β Β p: 4Β Β  q: 0Β Β  r: 5Β Β  s: 9Β Β  t: 7Β Β  u: 2Β Β  v: 0 w: 0Β Β  x: 0Β Β  y: 1Β Β  z: 2

Β 

Return value in $v0: ​129

Β 

Example #2:

Β 

message = β€œWe can only see a short distance ahead, but we can see plenty there that needs to be done. -Alan Turing”

Β 

Resulting ​counts​: ​8 2 3 4 15 0 1 4 2 0 0 3 0 9 4 1 0 3 5 8 2 0 1 0 2 0

Β 

which means:

Β 

a: 8Β Β  b: 2Β Β  c: 3Β Β  d: 4Β Β  e:15Β Β  f: 0Β Β  g: 1Β Β  h: 4Β Β  i: 2Β Β  j: 0Β Β  k: 0 l: 3Β Β  m: 0Β Β  n: 9Β Β  o: 4Β Β  p: 1Β Β  q: 0Β Β  r: 3Β Β  s: 5Β Β  t: 8Β Β  u: 2Β Β  v: 0 w: 1Β Β  x: 0Β Β  y: 2Β Β  z: 0

Β 

Return value in $v0: ​77

Β 

Β 

Part 6: Sort the Letters of the Alphabet by Frequency

Β 

void sort_alphabet_by_count(string sorted_alphabet, int[] counts)

Β 

This function’s purpose is to sort the lowercase letters of the Latin alphabet using the numbers given in the ​counts​ array as the sorting key, storing that sorted alphabet in ​sorted_alphabet​.Β  The buffer sorted_alphabet​ is guaranteed to be at least 27 bytes in size. The 26-word ​counts ​array provides a non-negative integer count associated with each lowercase letter. Specifically, ​counts[0]​ is the count for β€˜a’, ​counts[1]​ is the count for β€˜b’, etc. The function sorts the contents of sorted_alphabet​ so that the letter with highest count is at index 0, the letter with second-highest count is at index 1, and so. When two or more letters have the same count, the letter with smallest ASCII value is placed before the others in ​sorted_alphabet​, the letter with second-smallest ASCII value immediately follows the letter with smallest ASCII value, etc. The function null-terminates sorted_alphabet​ by writing a null-terminator at ​sorted_alphabet[26]​.

Β 

The function takes the following arguments, in this order:

  • sorted_alphabet​: an uninitialized buffer of 27 contiguous bytes of main memory.
  • counts​: an array of 26 non-negative counts. Each integer is four bytes. Note that this array is not necessarily the output of the ​count_lowercase_letters​ function, so do not assume that when coding this function.

Β 

Additional requirements:

  • The function must not write any changes to main memory except for ​sorted_alphabet​ and

counts​.

Β 

Example #1:

Β 

counts = 28 13 24 2 28 19 12 2 0 10 23 14 3 28 1 2 21 4 4 25 29 0 9 29 13 18

Resulting ​sorted_alphabet​: β€œβ€‹uxaentckqfzlbygjwrsmdhpoiv”

Β 

Example #2:

Β 

counts = 21 17 20 25 21 19 28 26 15 16 21 13 11 16 1 27 24 20 5 23 26 2 29 15 21 8

Resulting ​sorted_alphabet​: β€œwgphudqtaekycrfbjnixlmzsvo”​

Β 

Example #3:

Β 

counts = 23 26 29 1 20 9 15 30 24 20 23 7 17 15 5 4 17 14 12 24 14 1 0 4 14 6

Resulting ​sorted_alphabet​: β€œhcbitakejmqgnruysflzopxdvw”​

Β 

Β 

Part 7: Generate the Plaintext Alphabet from a Sorted Alphabet

Β 

void generate_plaintext_alphabet(string plaintext_alphabet,Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  string sorted_alphabet)

Β 

This function generates the 62-character plaintext alphabet from a sorted lowercase Latin alphabet using the algorithm described in Encryption Phase 2 under Preliminaries earlier in this document. The plaintext alphabet is written into the uninitialized ​plaintext_alphabet​ buffer, which is guaranteed to be at least 63 bytes in size. The function null-terminates ​plaintext_alphabet​ by writing a null-terminator at ​plaintext_alphabet[62]​. Briefly, the plaintext alphabet contains the lowercase letters of the Latin alphabet in alphabetical order, except that ​sorted_alphabet[0]​ is repeated 8 times in ​plaintext_alphabet​, ​sorted_alphabet[1]​ is repeated 7 times in plaintext_alphabet​, etc., all the way to ​sorted_alphabet[7]​, which is repeated once in plaintext_alphabet​. The remaining 18 letters of ​sorted_alphabet​ each appears once in plaintext_alphabet​.

Β 

The function takes the following arguments, in this order:

  • plaintext_alphabet​: an uninitialized buffer of 63 contiguous bytes of main memory where the function writes the 62-character plaintext alphabet, ending with a null-terminator
  • sorted_alphabet​: a null-terminated string containing the 26 lowercase letters of the Latin alphabet, in any order

Β 

Additional requirements:

  • The function must not write any changes to main memory except for ​plaintext_alphabet and ​sorted_alphabet​.

Β 

Example #1:

Β 

sorted_alphabet = β€œegljhotupvfsxawqkrmzdyncib” Resulting ​plaintext_alphabet​:

β€œabcdeeeeeeeeefgggggggghhhhhijjjjjjklllllllmnoooopqrstttuuvwxyz

”

Β 

Example #2:

Β 

sorted_alphabet = β€œeznovrqbdatjghlwmskyipcxfu” Resulting ​plaintext_alphabet​:

β€œabbcdeeeeeeeeefghijklmnnnnnnnoooooopqqqrrrrstuvvvvvwxyzzzzzzzz

”

Β 

Example #3:

Β 

sorted_alphabet = β€œjmhoxqzgityudwsecvfalnkrbp”

Resulting plaintext_alphabet​  :​

β€œabcdefgghhhhhhhijjjjjjjjjklmmmmmmmmnoooooopqqqqrstuvwxxxxxyzzz

”

Β 

Β 

Part 8: Return the Ciphertext Character Substituted for a Letter from the Plaintext

Β 

int encrypt_letter(char plaintext_letter, int letter_index,

string plaintext_alphabet, string ciphertext_alphabet)

Β 

This function computes and returns the substitution of a plaintext letter for the ciphertext, given the plaintext letter itself, that letter’s index in the plaintext message, the plaintext alphabet as generated by generate_plaintext_alphabet and the ciphertext alphabet as generated by​  generate_ciphertext_alphabet. The substitution process is described in Encryption Phase 3 in​   the Preliminaries section of the document, above. Briefly, suppose the plaintext_letter​              (located​ at index letter_index​         of some plaintext) appears at indexes ​            i​ through ​ i+k​       of​          plaintext_alphabet. The function will return the character at location​               ciphertext_alphabet[i+(letter_index mod (k+1))]. Note that it is this function’s​ responsibility to determine the value of k​ for a given letter in ​ plaintext_alphabet​           .​

Β 

The function takes the following arguments, in this order:

  • plaintext_letter:​ a lowercase letter
  • letter_index:​ a non-negative integer
  • plaintext_alphabet:​ the 62-character plaintext alphabet required to encrypt a plaintext message
  • ciphertext_alphabet:​ the 62-character ciphertext alphabet required to encrypt a plaintext message

Β 

Returns in $v0:

  • The encrypted letter or -1 if plaintext_letter​ is not a lowercase letter.​

Β 

Additional requirements:

  • The function must not write any changes to main memory.

Β 

Example #1:

Β 

plaintext_letter = β€˜u’ letter_index = 3 plaintext_alphabet =

β€œabbbbbbbbcdefffffffgggghiiijkklmnopqrstuuuuuvvvvvvwxxxxxxxxxyz

” ciphertext_alphabet =

β€œStonyBrkUivesNwYadfAmcbghjlpquxzCDEFGHIJKLMOPQRTVWXZ0123456789

”

Β 

Return value in $v0: 77​ (ASCII code for ​ β€˜M’​ )​

Β 

Example #2:

Β 

plaintext_letter = β€˜p’ letter_index = 46 plaintext_alphabet =

β€œabcdeeefghijkkkkkkkllllllmmnoppppppppqrstuvvvvvwxyyyyzzzzzzzzz

” ciphertext_alphabet =

β€œStonyBrkUivesNwYadfAmcbghjlpquxzCDEFGHIJKLMOPQRTVWXZ0123456789

”

Β 

Return value in $v0: 70​ Β  (ASCII code for ​ β€˜F’​ )​

Β 

Example #3:

Β 

plaintext_letter = β€˜x’ letter_index = 37 plaintext_alphabet =

β€œabcccccdeeeeeeeeeffffffffgghijkkklmnoppppqrstuvwxxxxxxyzzzzzzz

” ciphertext_alphabet =

β€œStonyBrkUivesNwYadfAmcbghjlpquxzCDEFGHIJKLMOPQRTVWXZ0123456789

”

Β 

Return value in $v0: 87​ (ASCII code for ​ β€˜W’​ )​

Β 

Example #4:

Β 

plaintext_letter = β€˜n’ letter_index = 15 plaintext_alphabet =

β€œabccccccccdddddefghiiiiiijjjjjjjjjklmmnopqrstuvwwwwwwwxyyyyzzz

” ciphertext_alphabet =

β€œStonyBrkUivesNwYadfAmcbghjlpquxzCDEFGHIJKLMOPQRTVWXZ0123456789

”

Β 

Return value in $v0: 73​ (ASCII code for ​ β€˜I’​ )​

Β 

Β 

Part 9: Encrypt a Plaintext Message using a Homophonic Substitution Cipher

Β 

int, int encrypt(string ciphertext, string plaintext, string keyphrase,Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  string corpus)

Β 

This function encrypts the given ​non-empty​ plaintext message using the homophonic substitution cipher described earlier in the document, storing the resulting ciphertext in ​ciphertext​. Assume that the plaintext contains only letters, spaces and punctuation marks; no digits will be present in the plaintext. All arguments are assumed to be valid. The buffer for the ciphertext is guaranteed to be large enough to store the null-terminated ​ciphertext​. Note that the function returns two values.

Β 

The function takes the following arguments, in this order:

  • ciphertext​: an uninitialized buffer to store the encrypted plaintext
  • plaintext​: a ​non-empty​, null-terminated, string to be encrypted using the homophonic substitution cipher
  • keyphrase​: a ​non-empty​, null-terminated keyphrase string used during the encryption process
  • corpus​: a ​non-empty​, null-terminated corpus string used during the encryption process

Β 

The function implements the following algorithm:

  1. Call ​to_lowercase​ on both ​plaintext​ and ​corpus​.
  2. Allocate at least 26 words’ worth of memory on the stack to temporarily store the ​counts​ array needed in the following step. (How? Simply subtract however many bytes you want to allocate from $sp. Make sure the number of bytes you allocate is a multiple of 4.) Be sure to deallocate this memory later.
  3. Call ​count_lowercase_letters(counts, corpus)​.
  4. Allocate at least 27 bytes of memory on the stack to store the ​lowercase_letters​ string needed in the following step. Be sure to deallocate this memory later.
  5. Call ​sort_alphabet_by_count(lowercase_letters, counts)​.
  6. Allocate at least 63 bytes of memory on the stack to temporarily store the plaintext_alphabet​ string needed in the following step. Be sure to deallocate this memory later.
  7. Call ​generate_plaintext_alphabet(plaintext_alphabet, lowercase_letters)​.
  8. Allocate at least 63 bytes of memory on the stack to temporarily store the ciphertext_alphabet​ string needed in the following step. Be sure to deallocate this memory later.
  9. Call ​generate_ciphertext_alphabet(ciphertext_alphabet, keyphrase)​.
  10. In a loop, call ​encrypt_letter​ to encrypt each lowercase letter of ​plaintext​ and write the return value into ​ciphertext​. Each non-lowercase letter of ​plaintext​ should not be passed as an argument to ​encrypt_letter​, but should instead simply be copied to ​ciphertext​.
  11. Null-terminate the ​ciphertext​

Β 

Returns in $v0:

  • The number of lowercase letters that were encrypted during the encryption process.

Β 

Returns in $v1:

  • The number of characters from the plaintext that were not encrypted and simply copied to ciphertext.​

Β 

Additional requirements:

  • The function must not write any changes to main memory except as needed.
  • The function must call the functions to_lowercase​ (twice), ​ count_lowercase_letters​ ,​ sort_alphabet_by_count, ​ generate_ciphertext_alphabet​ ,​ generate_plaintext_alphabet, and ​ encrypt_letter​ .​

Β 

Example #1:

Β 

plaintext = β€œNever trust a computer you can’t throw out a window. -Steve Wozniak”  keyphrase = β€œI’ll have you know that I stubbed my toe last week and only cried for 20 minutes.”

corpus = β€œWhen in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature’s God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.”

Β 

Resulting ciphertext​     :​ β€œDk5wQ 1Q4RW h yLCO4XnQ 8H4 yaE’3 VpQH6 L4V a 6qGoJ6.​

-R3n5t 6J9GqIA”

Β 

Return value in $v0: 53​

Β 

Return value in $v1: 14​

Β 

Example #2:

Β 

plaintext = β€œThe trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it. -Terry Pratchett”

keyphrase = β€œWhat’s the difference between ignorance and apathy? I don’t know and I don’t care.”

Β 

corpus = β€œCall me Ishmael. Some years ago – never mind how long precisely – having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation.”

Β 

Resulting ciphertext​           :​ β€œXjc 1UN4dDn 6xXj jW5vJk WJ ORcK GmKf, PI iO4TVn, mV​

0jW3 RwQREy 6mDE xKVlV1 NJ iMGqLk aDPJk aJf 2U8uHk 2Q R40 2jlJkV xK lZ.

-3rUT8 RThYijc23”

Β 

Return value in $v0: 111​

Β 

Return value in $v1: 29​

Β 

Example #3:

Β 

plaintext = β€œIf debugging is the process of removing software bugs, then programming must be the process of putting them in. -Edsger Dijkstra”

keyphrase = β€œWhat is the sum of 12 and 37? The answer, CLEARLY, is 49!”

corpus = β€œIt was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way – in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.”

Β 

Resulting ciphertext​            :​ β€œL7 e1iXTTAkT 9K MCu zFxsnGH x7 F2jxZEkT Gy7P0hDo iXTI,​

VCfk zFqTDhjj4kT jXKP in Mwm zFqsdKG q7 zXNOEkT VCnj Rk. -2eGTuD eAbcKSDt”

Β 

Return value in $v0: 105​

Β 

Return value in $v1: 23​

Β 

Β 

Part 10: Decrypt Ciphertext that was Encrypted using a Homophonic Substitution Cipher

Β 

int, int decrypt(string plaintext, string ciphertext, string keyphrase,Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  string corpus)

Β 

This function decrypts a ciphertext message that was encrypted using the homophonic substitution cipher described earlier in the document. The decryption algorithm is very similar to the encryption algorithm. All arguments are assumed to be valid. The buffer for the ciphertext is guaranteed to be large enough to store the null-terminated plaintext​            . Note that the function returns two values. The​          generated plaintext letters will all be in lowercase.

The function takes the following arguments, in this order:

  • plaintext​: an uninitialized buffer to store the decrypted ciphertext
  • ciphertext​: a ​non-empty​, null-terminated string to be decrypted using the homophonic substitution cipher
  • keyphrase​: a ​non-empty​, null-terminated keyphrase string used during the decryption process
  • corpus​: a ​non-empty​, null-terminated corpus string used during the decryption process

Β 

The function implements the following algorithm:

  1. Call ​to_lowercase​ on ​corpus​.
  2. Allocate at least 26 words’ worth of memory on the stack to temporarily store the ​counts​ array needed in the following step. Be sure to deallocate this memory later.
  3. Call ​count_lowercase_letters(counts, corpus)​.
  4. Allocate at least 27 bytes of memory on the stack to store the ​lowercase_letters​ string needed in the following step. Be sure to deallocate this memory later.
  5. Call ​sort_alphabet_by_count(lowercase_letters, counts)​.
  6. Allocate at least 63 bytes of memory on the stack to temporarily store the plaintext_alphabet​ string needed in the following step. Be sure to deallocate this memory later.
  7. Call ​generate_plaintext_alphabet(plaintext_alphabet, lowercase_letters)​.
  8. Allocate at least 63 bytes of memory on the stack to temporarily store the ciphertext_alphabet​ string needed in the following step. Be sure to deallocate this memory later.
  9. Call ​generate_ciphertext_alphabet(ciphertext_alphabet, keyphrase)​.
  10. In a loop, decrypt each alphabetical character of ​ciphertext​ and write the decrypted character into ​plaintext​. Each ​non-alphanumerical​ character of ​ciphertext​ should simply be copied to ​plaintext​.
  11. Null-terminate the ​plaintext​

Β 

Returns in $v0:

  • The number of lowercase letters that were written into the ​plaintext​ buffer during the decryption process.

Β 

Returns in $v1:

  • The number of non-letters that were written into the ​plaintext​ buffer during the decryption process.

Β 

Additional requirements:

  • The function must not write any changes to main memory except as needed.
  • The function must call the functions ​to_lowercase​, ​count_lowercase_letters​, sort_alphabet_by_count​, ​generate_ciphertext_alphabet​, generate_plaintext_alphabet​, and ​index_of​.

Β 

Example #1:

ciphertext = β€œDk5wQ 1Q4RW h yLCO4XnQ 8H4 yaE’3 VpQH6 L4V a 6qGoJ6. -R3n5t 6J9GqIA”  keyphrase = β€œI’ll have you know that I stubbed my toe last week and only cried for 20 minutes.”

corpus = β€œWhen in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature’s God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.”

Β 

Resulting plaintext​ :​ β€œnever trust a computer you can’t throw out a window.​

-steve wozniak”

Β 

Return value in $v0: 53​

Β 

Return value in $v1: 14​

Β 

Example #2:

Β 

ciphertext = ​ β€œXjc 1UN4dDn 6xXj jW5vJk WJ ORcK GmKf, PI iO4TVn, mV 0jW3​

RwQREy 6mDE xKVlV1 NJ iMGqLk aDPJk aJf 2U8uHk 2Q R40 2jlJkV xK lZ. -3rUT8

RThYijc23”

keyphrase = β€œWhat’s the difference between ignorance and apathy? I don’t know and I don’t care.”

Β 

corpus = β€œCall me Ishmael. Some years ago – never mind how long precisely – having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation.”

Β 

Resulting plaintext​ :​ β€œthe trouble with having an open mind, of course, is that​    people will insist on coming along and trying to put things in it. -terry pratchett”

Β 

Return value in $v0: 111​

Β 

Return value in $v1: 29​

Β 

Example #3:

ciphertext = β€œL7 e1iXTTAkT 9K MCu zFxsnGH x7 F2jxZEkT Gy7P0hDo iXTI, VCfk zFqTDhjj4kT jXKP in Mwm zFqsdKG q7 zXNOEkT VCnj Rk. -2eGTuD eAbcKSDt”

keyphrase = β€œWhat is the sum of 12 and 37? The answer, CLEARLY, is 49!” corpus = β€œIt was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way – in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.”

Β 

Resulting plaintext​ :​ β€œif debugging is the process of removing software bugs,​       then programming must be the process of putting them in. -edsger dijkstra”

Β 

Return value in $v0: 105​

Β 

Return value in $v1: 23​

  • HW-2-rgo1d9.zip