BACKGROUND INFORMATION A cryptosystem is a system for encryp
BACKGROUND INFORMATION: A cryptosystem is a system for encryption and decryption. Encryption, also called encoding or enciphering, involves changing the original message into a secret message, while decryption, also called decoding or deciphering, involves just the opposite changing the secret message back into its original readable form. In cryptography, a plaintext file is a file containing data in its original, or readable, form while a ciphertext file is a file containing data in coded, or scrambled, form In this assignment, you will be creating a polyalphabetic cipher based on substitution using multiple substitution alphabets In this cipher, instead of a single key, multiple one-letter keys are used to encrypt the original message by shifting the original letter to a new, encrypted letter corresponding to the key For example, consider the following modified case from Wikipedia where the plaintext to be encrypted is: ATTACK AT DAWN. Then, the person sending the message chooses a keyword and repeats it until it matches the length of the plaintext so that the key LEMON becomes LEMONL EM ONLE (accounting for the spaces in the plaintext message which are not encrypted or decrypted). Then, the first letter of the plaintext, A, is paired with L, the first letter of the key. Assuming the shift starts with A etc., we find that s 11, so the plaintext A shifts\" 11 places in the alphabet to ciphertext L. The second letter of the plaintext. T, gets paired with E, the second letter of the key, so it shifts 4 (the shift value of E) places in the alphabet to ciphertext X. However, note that the third letter of the plaintext, T, gets mapped to a different ciphertext because It is paired with M, the third letter of the key. Of interest, is that this ciphertext wraps around to F based on shifting 12 places in the alphabet. The rest of the plaintext is enciphered in a similar fashion: Plaintext ATTACK AT DAWN LEMONT, EM ONTE Key Ciphertext LXFOPV EF RNHR Note that since white space (or any unsupported character) is not encrypted or decrypted, the key should not be used (or incremented) so that the blank space does not take up a letter in the key. Mathematically, each letter of the plaintext and key can be given a number and a formula can be derived to encrypt for c (i.e., ciphertext) and m i.e., plaintext) using k (i.e., key) as follows (m k) 26 (26 c k) 26 Note that formula assumes A is 0, B s 1, and so forth with nothing to distinguish between uppercase A and lowercase a. Since you will most likely be using the character ASCII values, you will have to modify these formulas to fit your needs, but this should give you a place to start #include void cipher(int i, int c); int findMin(); void makeArray(int, int); char arr[22][22], darr[22][22], emessage[111], retmessage[111], key[55]; char temp[55], temp2[55]; int k = 0; int main() { char *message; int i, j, klen, emlen, flag = 0; int r, c, index, rows; printf(\"Enter the key\ \"); fflush(stdin); gets(key); printf(\"\ Enter message to be ciphered\ \"); fflush(stdin); gets(message); strcpy(temp, key); klen = strlen(key); k = 0; for (i = 0;; i++) { if (flag == 1) break; for (j = 0; key[j] != NULL; j++) { if (message[k] == NULL) { flag = 1; arr[i][j] = \'-\'; } else { arr[i][j] = message[k++]; } } } r = i; c = j; for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { printf(\"%c \", arr[i][j]); } printf(\"\ \"); } k = 0; for (i = 0; i < klen; i++) { index = findMin(); cipher(index, r); } emessage[k] = \'\\0\'; printf(\"\ Encrypted message is\ \"); for (i = 0; emessage[i] != NULL; i++) printf(\"%c\", emessage[i]); printf(\"\ \ \"); //deciphering emlen = strlen(emessage); //emlen is length of encrypted message strcpy(temp, key); rows = emlen / klen; //rows is no of row of the array to made from ciphered message j = 0; for (i = 0, k = 1; emessage[i] != NULL; i++, k++) { //printf(\"\ Emlen=%d\",emlen); temp2[j++] = emessage[i]; if ((k % rows) == 0) { temp2[j] = \'\\0\'; index = findMin(); makeArray(index, rows); j = 0; } } printf(\"\ Array Retrieved is\ \"); k = 0; for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { printf(\"%c \", darr[i][j]); //retrieving message retmessage[k++] = darr[i][j]; } printf(\"\ \"); } retmessage[k] = \'\\0\'; printf(\"\ Message retrieved is\ \"); for (i = 0; retmessage[i] != NULL; i++) printf(\"%c\", retmessage[i]); return (0); } void cipher(int i, int r) { int j; for (j = 0; j < r; j++) { { emessage[k++] = arr[j][i]; } } } void makeArray(int col, int row) { int i, j; for (i = 0; i < row; i++) { darr[i][col] = temp2[i]; } } int findMin() { int i, j, min, index; min = temp[0]; index = 0; for (j = 0; temp[j] != NULL; j++) { if (temp[j] < min) { min = temp[j]; index = j; } } temp[index] = 123; return (index); }
Solution
#include