Question: Complete the following TODO, Debug code, create an empty array for our cyphertext. Instructions are found in the code. Submit a screenshot of your shell

Complete the following TODO, Debug code, create an empty array for our cyphertext. Instructions are found in the code. Submit a screenshot of your shell window with the output of your program to canvas, as well as your caesar.php code.

#!/usr/bin/php -d display_errors

class Caesar { public static $numbersToLetters = array( 0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U', 21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z' ); public static $lettersToNumbers = array( 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25 );

/** * The shift */ public $shift_key = 3;

/** * Encrypts the plaintext using the shift key, and returns the ciphertext (in all caps). */ public function encrypt($plaintext) { //Capitalize all the characters $plaintext = strtoupper($plaintext); // see https://www.php.net/manual/en/function.strtoupper.php //convert the string to an array, and then loop over the $plaintext = str_split ($plaintext); //see https://www.php.net/manual/en/function.str-split.php //also see https://www.php.net/manual/en/function.explode.php //var_dump($plaintext); //Debug code uncomment to see the array $cyphertext = array(); //create an empty array for our cyphertext //temporary variables for our script. This is inelegant, but easy to follow. $tmp_plain_num = 0; $tmp_crypt_num = 0; foreach ($plaintext as $char) { //check to make sure we have a letter. If we do not, just stick it in the cyphertext unencrypted. if(ctype_alpha($char)) { $tmp_plain_num = self::$lettersToNumbers[$char]; //gets the number /** * In php, mod takes precedence over addition, so we need parenthesis. What would happen without them? */ $tmp_crypt_num = ($tmp_plain_num + $this->shift_key) % 26; // See https://www.php.net/manual/en/language.operators.precedence.php for php's PEMDAS /** * In php (as opposed to some other languages), we can append an element to the end of an array * by simply leaving the index empty. */ $cyphertext[] = self::$numbersToLetters[$tmp_crypt_num]; //encrypt the letter, and append it to the array. } else { $cyphertext[] = $char; //stick the non-letter in. } } //var_dump($cyphertext); //Debug code Uncomment to see the array $cyphertext = implode($cyphertext); //convert back to a string. See https://www.php.net/manual/en/function.implode.php return($cyphertext); } public function decrypt($cyphertext) {

/** * @TODO Use the above as a model to implement the decrypt function. * Requirements: * - Returns unecrypted text, leaving all punctuation intact. * - Returns the string in all lower-case characters. (You will need to do this last, as the arrays above expect upper case characters.) * - Uses the shift key currently stored as a property of the object. * - Your code must be easy to follow by someone who does not quite understand php (don't be too clever and obscure) * - Your code must be documented. * * Common issues: * - When doing the mod operator, php returns a negative number when given a negative dividen. To fix this, add 26 after subtracting, before modding. * For example, your code should have a line that looks something like: $tmp_decrypt_num = ($tmp_cipher_num - $this->shift_key + 26) % 26; * */ $plaintext=$cyphertext; //DELETE THIS LINE it is there so that the code does not return an error in the beginning. return ($plaintext); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!