In C Language Introduction Cryptography is the science of making messages secure
ID: 3814269 • Letter: I
Question
In C Language
Introduction Cryptography is the science of making messages secure, of transforming readable messages into unreadable messages and back again. Messages that are unreadable are called ciphertext. The process of turning plaintext into ciphertext is called encryption. The reverse process of turning ciphertext into plaintext is called decryption. One of the easiest ways to encrypt a message is to scramble the letters. For example the word "apple" could be randomly transformed to "lapep" In fact there are 120 different possible arrangements of the word "apple." However, if the encryption algorithm randomly scrambles the letters, the task of the decryption algorithm is pretty hard. Encryption and decryption algorithms must work together in some agreed upon way with the encryption algorithm scrambling letters and the decryption algorithm unscrambling them. A transposition cipher is one way to scramble the letters of a message. The cipher separates the message into two groups of characters: the first group composed of the even-numbered characters and the second group composed of the odd-numbered characters. To produce the ciphertext, the cipher puts together both groups, placing the group of the even-numbered characters first, followed by the group of odd-numbered characters. This encryption results in a string with the characters shuffled to new positions Project: Ask user to enter a string. write a function that takes the string as a parameter, encrypt the message as described above and returns the ciphertext. Then ask user again if she/he wants to continue or not. Finally print all the original messages in alphabetically ascending order Example Please enter a message Fight, Matadors, for Tech The encrypted message is ih, Mtdr, frTc Fgt aaos o eh Do you want to continue (Y/N) Y Please enter a message Bear our banners far and wide The encrypted message is erorbnesfradwd. Ba u anr a n ie The original messages in alphabetical order are: Bear our banners far and wide Fight, Matadors for TechExplanation / Answer
CODE:
#include <stdlib.h>
#include <stdio.h>
#include<string.h>
// encrypts the cipher as given in the problem description.
void railfence_encipher(const char *plaintext){
int length = strlen(plaintext), i,j=0,k=0;
char *oddText = malloc(strlen(plaintext)+1);
char *evenText = malloc(strlen(plaintext)+1);
for(i=0;i<length;i=i+2){
oddText[j] = plaintext[i];
j++;
if(plaintext[i+1]!=''){
evenText[k] = plaintext[i+1];
k++;
}
}
printf(" The encrypted message is: %s%s ",evenText,oddText);
}
int main(int argc, char *argv[]){
char *buffer;
char flag;
size_t bufsize = 50;
size_t characters;
buffer = (char *)malloc(bufsize * sizeof(char));
if( buffer == NULL)
{
perror("Unable to allocate buffer");
exit(1);
}
printf("Please type a message: ");
getline(&buffer,&bufsize,stdin);
railfence_encipher(buffer);
return 0;
}
OUTPUT:
$ gcc transpo.c
$ ./a.out
Please type a message: Bear our banners far and wide.
The encrypted message is: erorbnesfradwd.Ba u anr a n ie