USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A sh
ID: 3807618 • Letter: U
Question
USE C programming (pls label which file is libcipher.h and libcipher.c)
Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is replaced by ’D’, etc. Note that the shift wraps around from the end to the beginning of the alphabet such that, with a right shift of 2, ’Y’ is replaced by ’A’ and ’Z’ is replaced by ’B’. Deciphering works in the same way, but shifts to the left and wraps around from the beginning of the alphabet to the end.
Write two functions encrypt and decipher.
encrypt takes a NULL terminated string and a shift parameter as inputs, and right shifts all alphabetic characters (i.e., ’a’-’z’ and ’A’-’Z’) by the shift parameter. Non- alphabetic characters should not be changed by encrypt.
decipher takes a NULL terminated string and a shift parameter as inputs, and left shifts all alphabetic characters (i.e., ’a’-’z’ and ’A’-’Z’) by the shift parameter. Non- alphabetic characters should not be changed by encrypt.
Your functions can be written assuming that the shift parameter will never be less than 0 or greater than 26.
You must use the following function prototypes:
Note: You must write your function prototypes in a header file named libcipher.h and you must write your function definitions in a source file named libcipher.c. We are providing you the main source file cipher.c, which you can use to test your functions. Do not change anything in cipher.c.
cipher.c
#include <stdlib.h>
#include <stdio.h>
#include "libcipher.h"
void printEncrypt(char str[], int shift);
void printDecipher(char str[], int shift);
int main(void)
{
int shift, i;
char msg1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
char msg2[] = "The Quick Brown Fox Jumps Over The Lazy Dog. ";
printf("Enter the shift parameter: ");
scanf("%d", &shift);
if (shift < 0 || shift > 26) {
printf("Shift must be between 0 and 26. ");
return 0;
}
printEncrypt(msg1, shift);
printDecipher(msg1, shift);
printEncrypt(msg2, shift);
printDecipher(msg2, shift);
return 0;
}
void printEncrypt(char str[], int shift)
{
printf(" Encrypting message... ");
printf("Plaintext: %s", str);
encrypt(str, shift);
printf("Ciphertext: %s", str);
}
void printDecipher(char str[], int shift)
{
printf(" Deciphering message... ");
printf("Ciphertext: %s", str);
decipher(str, shift);
printf("Plaintext: %s", str);
}
Explanation / Answer
Following is the complete code of both encrypt and decipher methods :
void encrypt(char str[], int shift)
{
int n = strlen(str); // stores length of the sentence
int i;
for(i=0;i<n;i++)
{
if(str[i]>='a' && str[i]<='z') // operates for encryting lowercase alphabets
{
str[i] = (char)(str[i]+shift); // shift
if(str[i]>'z') str[i] = (char)(str[i]-26); // wrap around from end to beginning
}
else if(str[i]>='A' && str[i]<='Z') // operates for encrypting uppercase alphabets
{
str[i] = (char)(str[i]+shift); // shift
if(str[i]>'Z') str[i] = (char)(str[i]-26);// wrap around from end to beginning
}
}
}
void decipher(char str[], int shift)
{
int n = strlen(str); // stores length of the sentence
int i;
for(i=0;i<n;i++)
{
if(str[i]>='a' && str[i]<='z')// operates for decryting lowercase alphabets
{
str[i] = (char)(str[i]-shift); // shift
if(str[i]<'a') str[i] = (char)(str[i]+26); // wrap around from beginning to end
}
else if(str[i]>='A' && str[i]<='Z') // operates for decrypting uppercase alphabets
{
str[i] = (char)(str[i]-shift); // shift
if(str[i]<'A') str[i] = (char)(str[i]+26); // wrap around from beginning to end
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.