Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Echo the input: First, you should make sure you can write a program and have

ID: 3770988 • Letter: 1

Question

1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the CS dept Linux network] 2) Simple Substitution Cipher: One of the earliest and simplest types of ciphers was called a "simple substitution cipher". And one of the simplest of these simple substitution ciphers was the cipher where each letter is replaced by the next letter in the alphabet. For example, 'a' is replaced by 'b', 'b' is replaced by 'c', and so on, until you get to 'z', which is replaced by 'a'. Have your program take a single word as input and perform this simple substitution cipher on it, then print out the encrypted text (also known as ciphertext) [2 points] Hint: you can add a numerical value to a character. For example, if you add 1 to the character 'a', it becomes 'b'. If you add 2 to 'a', it becomes 'c', and so on and so forth. Be careful with 'z' - it needs a special case. 3) Polyalphabetic (or Block) Cipher: Simple (or monoalphabetic) substitution ciphers can be easily broken by letter frequency analysis, leading to the development of polyalphabetic ciphers, which are the precursors to block ciphers, which are the basis for modern cryptography. You'll implement a simple block cipher that will work as follows: instead of simply "increasing" each letter by 1, imagine the plaintext being divided into blocks of 8 characters. Then, in each block, you'll increase the first letter by 1, the second letter by 2, the third letter by 3, until you reach the 8th letter, which you'll increase by 8. The 9th letter of the plaintext will start the 2nd block, thus the 9th letter will be increased by 1 again, the 10th letter by 2, and so on and so forth. Example: plaintext: c r y p t o g r a p h y you add: 1 2 3 4 5 6 7 8 1 2 3 4 ciphertext: d t b t y u n z b r k c [4 points for getting this to work in all cases. 2 points for getting this to work for characters that don't don't go past 'z'.] Hint: to get this to "wrap around" from 'z' to 'a', it will require more than just a special case. You must figure out and implement a general rule for what to do when the result of your substitution causes the value of your character to exceed 'z'. Being able to treat characters as their integer values in mathematical expressions may help you here - for example, you can compare characters with < and > (less than and greater than). You can compare 2 characters or you can compare a character's ASCII value to an integer. If you're really stuck, you may want to look at an ASCII chart. Extra Credit: You may complete any (or all) of these you're able to, after completing step 3. [1 extra point for completing at least one of these] 4) Variable-Length Block Cipher with Key: For this step, you'll need the user to enter a key. This key will be a word (no spaces) consisting of only lowercase letters. Then, you'll implement a block cipher as in step 3, except the length of the key will be the size of the block, and you'll add the alphabetic position of each letter in the key to each letter in the block. [1 point] For example, if your key is "crypto", you'll add: 3 to the first letter of the plaintext (c=3) 18 to the 2nd letter of the plaintext (r=18) 25 to the 3rd letter of the plaintext (y=25) 16 to the 4th letter of the plaintext (p=16) 20 to the 5th letter of the plaintext (t=20) 15 to the 6th letter of the plaintext (o=15) 3 to the 7th letter of the plaintext, because we're back to the "c" at the beginning of "crypto". 5) Allow the Plaintext to be a Sentence: Treat spaces as the 27th letter of the alphabet, so that your plaintext can be a sentence (or even a paragraph without punctuation). Encrypt spaces as you would any other character, but note that the "space" character will need to be treated as a special case, since it is not next to the lowercase letters in ASCII. Note that this will require altering your code from steps 2, 3, and 4 if you did it. [1 point] Hint: cin by itself will only read a string until the first space. To read in a sentence, use getline(cin,stringname); 6) Use Command Line Arguments: Allow the user to enter the input (the plaintext, and the key if applicable) through his choice of a command line argument or being prompted for input. The way that this will work is that if the user enters a command line argument, that will be used as the input. If he does not, he will be asked to input his plaintext (and key if applicable) as normal. An example of using command line arguments is included in my "readings and code samples" on Blackboard. [1 point to get it working on an input of a one-word plaintext (and key if applicable), 2 points to get it working on a sentence of plaintext] Note that having a sentence as your plaintext in a command line argument will require using techniques a fair bit beyond the scope of this course, so you can use a command line argument for just a one-word plaintext while allowing a sentence of plaintext to be entered via prompting the user.

Explanation / Answer

Echo the input: First, you should make sure you can write a program and have it compile and run,
take input and give output. So to start you should just echo the input.
This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:".

int main()
{
char ch[30];
printf("Enter your text");
scanf("%s",ch);
printf("this is the plaintext you entered: %s",ch);
return 0;
}

2) Simple Substitution Cipher:

int main()
{
char ch[30];
char ch2[30];
printf("Enter your text");
scanf("%s",ch);
printf("this is the plaintext you entered: %s",ch);
int z;

for (i = 0; ch[i] != ''; i++) {
     z=ch[i];
z=z+1;
ch2[i]=(char) z;
}
printf("Your new Cipher word is : %s"+ ch2);

}

3) Polyalphabetic (or Block) Cipher

int main()
{
char ch[30];
char ch2[30];
printf("Enter your text");
scanf("%s",ch);
printf("this is the plaintext you entered: %s",ch);
int z;
int count=1;

for (i = 0; ch[i] != ''; i++) {
if(i%9==0)
count =1;
count=count+i;
z=z+count;  // Increment z by j here
ch2[i]=(char) z;
}
}
4) Variable-Length Block Cipher with Key

int main()
{
char ch[30];
char ch2[30];
char key[30];
printf("Enter your text");
scanf("%s",ch);
printf("Enter your key");
scanf("%s",key);
printf("this is the plaintext you entered: %s",ch);
int z;
int count;
int keyLength=0;

for (int i = 0; key[i] != ''; i++)
  keyLength=keyLength+1;

for (int i = 0; ch[i] != ''; i++) {
z=ch[i];
j=i;
if(i%(keyLength+1)==0)
{
  j=i%(keyLength+1);
}
count=findKeyNumber(key[j]);
z=z+count;
ch2[i]=(char) z;
  j=j+1;
}
}

int findKeyNumber(char ch)
{
int count;
if(ch=='a')
  count=1;
else if (ch=='b')
  count =2;
  
  //do it so on till z;
  
  return count;

}