In: Computer Science
Also please add comments on the code and complete in C and also please use your last name as key.
The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message.
Test Cases
The following messages will be given as the input as the test case to your program to check the accuracy of your program
-A HEALTHY ATTITUDE IS CONTAGIOUS BUT DONT WAIT TO CATCH IT FROM OTHERS.
-IF YOU CARRY YOUR CHILDHOOD WITH YOU YOU NEVER BECOME OLDER.
-FROM PRINCIPLES IS DERIVED PROBABILITY BUT TRUTH OR CERTAINTY IS OBTAINED ONLY FROM FACTS.
#include<stdio.h>
int stringSize(char *str){
// string length function
int len;
for(len=0;str[len]!='\0';len++);
return len;
}
void removeSpaces(char *initalString, char *str){
// remove spaces from given string
int len,spaces=0;
for(len=0;initalString[len]!='\0';len++){
if (initalString[len]==' '){
spaces++;
continue;
}
str[len-spaces]=initalString[len];
}
str[len-spaces]=initalString[len];
}
void generateKey(char* str, char* keyword, char*key)
{
// generated repeated keyword of length str
int size = stringSize(str);
int keysize = stringSize(keyword);
for (int i = 0; i<size ; i++)
{
key[i]=keyword[i%keysize];
}
}
void cipherText(char* str, char* key,char* cipher_text)
{
int size = stringSize(str);
int spaces=0;
for (int i = 0; i < size; i++)
{
// converting in range 0-25
int x = (str[i] + key[i])
%26;
// convert into
alphabets(ASCII)
x += 'A';
cipher_text[i-spaces]=x;
}
}
// This function decrypts the encrypted text
// and returns the original text
void originalText(char* cipher_text, char* key,char*
decrypted)
{
int size = stringSize(cipher_text);
for (int i = 0 ; i < size; i++)
{
// converting in range 0-25
int x = (cipher_text[i] - key[i] +
26) %26;
// convert into
alphabets(ASCII)
x += 'A';
decrypted[i]=x;
}
}
int main()
{
char initalString[1000] = "A HEALTHY ATTITUDE IS
CONTAGIOUS BUT DONT WAIT TO CATCH IT FROM OTHERS";
char str[1000];
char keyword[1000] = "ANONYMOUS";
char key[1000];
char cipher_text[1000];
char decrypted[1000];
// Since its mentioned in quesion
// You also have to skip the space between the words,
while replicating the key to cover the entire message.
removeSpaces(initalString, str); // remove spaces from
initial string
printf("String : %s\n", str);
generateKey(str, keyword, key); // generate key of
same length
printf("Keyword: %s\n", key);
cipherText(str, key, cipher_text); // generate
cipher text
printf("Ciphertext : %s\n", cipher_text);
originalText(cipher_text, key, decrypted); //
decrypted message
printf("Decrypted Text : %s\n", decrypted);
return 0;
}