Programming Practice
A classical cipher is substitution cipher that letters are one-to-one systematically mapped to other letters. The mapping is called the code book of a substitution cipher. For example, the following is a code book mapping uppercase Enghilsh letters to themselves:
Normal Letters | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
Cipher Letters | N | E | I | Q | O | Y | A | R | D | C | S | H | X | Z | B | P | J | T | K | U | F | L | V | G | W | M |
This cipher technique is also called monoalphabetic substitution cipher. With ths code book, "UNIVERSITY" is encoded into "FZDLOTKDUW". Write a C program to perform the following steps:
Input an English text;
Remove white spaces and punctuation symbols;
Convert all lower case letters to upper case letters;
Encode the text using the above code book;
Output the original text and the encoded text;
Generate the decode book, which is an inverse function of the code book;
Decode the encoded text and output the decoding result.
Assume the maximum characters in the input length is 10,000. You may use MDOS pipeline command to input (<) and output (>) the orginal text and the encoded text. Sample input FCU.txt. Sample output:
A more complicated cipher technique is polyalphabetic substitution cipher. It is more difficult to break a polyalphabetic substitution cipher. A Vigenère square with a keyword is used to encode a text of English letters. The first row of a Vigenère square is 26 English letters in the alphabetical order, then each of the following row is the cyclic left rotation of the row right on the top of it. A Vigenère square is given as the following tables:
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A |
C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B |
D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C |
E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D |
F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E |
G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F |
H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G |
I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H |
J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I |
K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J |
L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K |
M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L |
N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M |
O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N |
P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O |
Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P |
R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q |
S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R |
T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S |
U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T |
V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U |
W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V |
X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W |
Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X |
Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y |
In fact, a Vigenère square can be viewed as 26 code books indexed on the letters of the first column. A keyword can be any English word, it will be repeatedly concantenate itself until the same length as the encoded text. The encoded text and the repeated keyword is algined and then a code to book is selected. Selection of the code book is to match the aligned letter of the keywork with the fist letter of the code book. With the selected code book, the letter of the encoded text is then translated to a ciphered letter. For example, if the keyword is "FENGCHIA" and the text is "STRUCTUREPROGRAMMINGDESIGNINC" (Structure Programming Design in C), the encoding of the text is shown as below.
Input Text | S | T | R | U | C | T | U | R | E | P | R | O | G | R | A | M | M | I | N | G | D | E | S | I | H | N | I | N | C |
Keyword | F | E | N | G | C | H | I | A | F | E | N | G | C | H | I | A | F | E | N | G | C | H | I | A | F | E | N | G | C |
Encoded Text | X | X | E | A | E | A | C | R | J | T | E | U | H | Y | A | M | R | M | A | M | F | L | A | I | L | R | V | T | E |
The encoded text is "XXEAEACRJTEUIYIMRMAMFLAILRVTE". Write a C program to perform the following steps:
Input a keyword and an English text;
Remove white spaces and punctuation symbols;
Convert all lower case letters to upper case letters;
Encode the text using the Vigenère square and
Output the original text and the encoded text;
Decode the encoded text and output the decoding result.
Assume the maximum characters in the input length is 10,000. You may use MDOS pipeline command to input (<) and output (>) the original text and the encoded text. Sample input C_Programming.txt and MacArthur_Prayer1.txt. Sample output: