Question

Write helpful comments for the following code: use Vigenere cipher tech to encrypt and decrypt message

The code:

#include<stdio.h>

#include <stdlib.h>

char arr[26][26];

char message[22], key[22], emessage[22], retMessage[22];

int findRow(char);

int findColumn(char);

int findDecRow(char, int);

int main()

{

int i = 0, j, k, r, c;

k = 96;

for (i = 0; i<26; i++)

{

k++;

for (j = 0; j<26; j++)

{

arr[i][j] = k++;

if (k == 123)

k = 97;

}

}

printf("\nEnter message\n");

fgets(message, 22, stdin);

printf("\nEnter the key\n");

fgets(key, 22, stdin);

for (i = 0; key[i] != '\0'; i++) //Use '\0', not NULL

{

c = findRow(key[i]);

r = findColumn(message[i]);

emessage[i] = arr[r][c];

}

emessage[i] = '\0';

printf("\n Encrypted message is:\n\n");

for (i = 0; emessage[i] != '\0'; i++) //Use '\0', not NULL

printf("%c", emessage[i]);

//decryption

for (i = 0; key[i] != '\0'; i++) //Use '\0', not NULL

{

c = findColumn(key[i]);

r = findDecRow(emessage[i], c);

retMessage[i] = arr[r][0];

}

printf("\n\nMessage Retrieved is:\n\n");

for (i = 0; emessage[i] != '\0'; i++) //Use '\0', not NULL

printf("%c", emessage[i]);

//decryption

for (i = 0; key[i] != '\0'; i++) //Use '\0', not NULL

{

c = findColumn(key[i]);

r = findDecRow(emessage[i], c);

retMessage[i] = arr[r][0];

}

retMessage[i] = '\0';

printf("\n\nMessage Retrieved is:\n\n");

for (i = 0; retMessage[i+1] != '\0'; i++)

printf("%c", retMessage[i]);

getchar();

return(0);

}

int findRow(char c)

{

int i;

for (i = 0; i<26; i++)

{

if (arr[0][i] == c)

return (i);

}

}

int findColumn(char c)

{

int i;

for (i = 0; i<26; i++)

{

if (arr[i][0] == c)

return (i);

}

}

int findDecRow(char c, int j)

{

int i;

for (i = 0; i<26; i++)

{

if (arr[i][j] == c)

return (i);

}

}

output should be:

Enter message wikitechy Enter the key technical Encrypted message is: prmmpgrmehj Message Retrieved is wikitechy

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h> //include header file
#include <stdlib.h>//include standard library file
char arr[26][26]; // Assigning alphabets in char array
char message[22], key[22], emessage[22], retMessage[22];
int findRow(char);// declaring variables
int findColumn(char);
int findDecRow(char, int);
int main()
{
int i = 0, j, k, r, c;
k = 96;
for (i = 0; i<26; i++)//Started for loop
{
k++;
for (j = 0; j<26; j++)
{
arr[i][j] = k++;
if (k == 123)
k = 97;
}
}
printf("\nEnter message\n"); //Getting Input message to encrypt
fgets(message, 22, stdin); //used to get the message
printf("\nEnter the key\n");//Getting Key values to encrypt the message
fgets(key, 22, stdin);
for (i = 0; key[i] != '\0'; i++) //Use '\0', not NULL
{
c = findRow(key[i]);
r = findColumn(message[i]);
emessage[i] = arr[r][c];
}
emessage[i] = '\0';
printf("\n Encrypted message is:\n\n");
for (i = 0; emessage[i] != '\0'; i++) //Use '\0', not NULL
printf("%c", emessage[i]); //Displaying the encrypted message
//decryption
for (i = 0; key[i] != '\0'; i++) //Use '\0', not NULL
{
c = findColumn(key[i]);
r = findDecRow(emessage[i], c);
retMessage[i] = arr[r][0];
}
printf("\n\nMessage Retrieved is:\n\n");
for (i = 0; emessage[i] != '\0'; i++) //Use '\0', not NULL
printf("%c", emessage[i]);// Displaying the retrived message
//decryption
for (i = 0; key[i] != '\0'; i++) //Use '\0', not NULL
{
c = findColumn(key[i]);
r = findDecRow(emessage[i], c);
retMessage[i] = arr[r][0];
}
//Here again we are decrypting the encrypted message
retMessage[i] = '\0';
printf("\n\nMessage Retrieved is:\n\n");
for (i = 0; retMessage[i+1] != '\0'; i++)
printf("%c", retMessage[i]);// Printed the message retrived
getchar();
return(0);
}
int findRow(char c)
{
int i;
for (i = 0; i<26; i++)
{
if (arr[0][i] == c)
return (i);
}
}
//Finding which coumn to decrypt
int findColumn(char c)
{
int i;
for (i = 0; i<26; i++)
{
if (arr[i][0] == c)
return (i);
}
}
//Finding decrypted row
int findDecRow(char c, int j)
{
int i;
for (i = 0; i<26; i++)
{
if (arr[i][j] == c)
return (i);
}
}

Add a comment
Know the answer?
Add Answer to:
Write helpful comments for the following code: use Vigenere cipher tech to encrypt and decrypt message...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption                     void SubEncrypt(cha...

    Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption                     void SubEncrypt(char *message, char *encryptKey) { int iteration = 0; printf("Enter Aphabet Encryption Key: \n"); scanf("%s", encryptKey);    for (iteration = 0; iteration < strlen(message); iteration++) { char letter = message[iteration]; if (letter >= 'A' && letter <= 'Z') {    letter = encryptKey[letter - 'A']; } message[iteration] = letter; } printf("CipherText message: %s\n", message); } //_________________________________________________________________________________________________________________________________________________...

  • Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

    Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two functions: encryption and decryption. 2. Use any key you like. (Don't use deceptive in the slides) 3. choose a sentence or a paragraph you like as the plaintext. I have the code I just need the implementation in a different way // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; // This function generates the key in // a cyclic manner until...

  • 12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print...

    12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print ciphertext character correctly. With offset of 3 the output should be ORIGINAL CHARACTERS 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 ENCRYPTED CHARACTERS D E F G H I J K L M N O P Q R S T U V W X Y Z...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • This code should be Runnable on MARS (MIPS Assembler and Runtime Simulator) IDE Convert the following...

    This code should be Runnable on MARS (MIPS Assembler and Runtime Simulator) IDE Convert the following c code into mips #include <stdio.h> #include <string.h> char cipherText[200] = "anything"; int countNumberOfCharInCipher(char*); int countNumberOfCharInCipher(char* cText) { return strlen(cText);    } int countSpaces(int numberOfChar, char input[]) { int spaceCounter =0; for(int i=0; i < numberOfChar; i++) { if(input[i] == 32) spaceCounter++; } return spaceCounter; } void decrypt(int numberOfChar, int key, char * cipherText, char * plainText) { int j = 0; int i =0;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT