Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting o

ID: 3708471 • Letter: C

Question

C program (Not C++, or C#)

Viginere Cipher

1)Ask the user if we are encrypting or decrypting.

2) Ask the user to enter a sentence to be transformed.

3) Ask the user to enter a sentence that will be used as the encryption or decryption key.

4) The sentences (array of characters) should end with a NULL terminator ''.

5) The range of values will be all printable characters on the ASCII chart starting with a SPACE - Value 32, up to and including a ~ - Value 126. Therefore, the number of symbols is 95 (0 to 94), i.e. Mod 95.

6) If the key is shorter than the message, make sure the key repeats until it matches the length of the text to be transformed. 7) Print out the transformed (encrypted or decrypted) message

Explanation / Answer

#include<stdio.h>

#include<string.h>

char key[100],text[100],encrypt[100],decrypt[100];

void extendKey(){

int len_text = strlen(text);

int len_key = strlen(key);

if(len_key < len_text){

int i=0;

for(int j=0;j<len_text-len_key;j++){

key[len_key+j] = key[i];

i++;

if(i == len_key)

i = 0;

}

}

}

void Encryption(char *str){

int len_text = strlen(str);

for(int i=0;i<len_text;i++){

int x = (str[i] + key[i]) % 95;

x += 32;

encrypt[i] = x;

}

encrypt[len_text] = '';

}

void Decryption(char *str){

int len_text = strlen(str);

for(int i=0;i<len_text;i++){

int x = (str[i] - key[i] + 95) % 95;

x += 32;

decrypt[i] = x;

}

decrypt[len_text] = '';

}

int main(){

int z = 0;

char c = 'a';

printf("Enter 1.Encryption 2.Decryption ");

scanf("%d",&z);

printf("Enter sentence to be transformed ");

int i=0;

scanf("%c",&c);

while(1){

scanf("%c",&c);

if(c == '\'){

scanf("%c",&c);

if(c == '0')

break;

else{

text[i++] = '\';

text[i++] = c;

}

}

else

text[i++] = c;

}

printf("Enter sentence to be used as Encryption or Decryption Key ");

i = 0;

scanf("%c",&c);

while(1){

scanf("%c",&c);

if(c == '\'){

scanf("%c",&c);

if(c == '0')

break;

else{

key[i++] = '\';

key[i++] = c;

}

}

else

key[i++] = c;

}

extendKey();

switch(z){

case 1:

Encryption(text);

printf("Encrypted message is %s",encrypt);

break;

case 2:

Decryption(text);

printf("Decrypted message is %s",decrypt);

break;

default:

printf("Enter a valid choice ");

}

return 0;

}

output:

Enter 1.Encryption 2.Decryption
1
Enter sentence to be transformed
this is great
Enter sentence to be used as Encryption or Decryption Key
wow
Encrypted message is M9BLPBLP@K6:M