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:
void encrypt(char str[], int shift); void decipher(char str[], int shift);
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\n";
char msg2[] = "The Quick Brown Fox Jumps Over The Lazy Dog.\n";
printf("Enter the shift parameter:\n");
scanf("%d", &shift);
if (shift < 0 || shift > 26) {
printf("Shift must be between 0 and 26.\n");
return 0;
}
printEncrypt(msg1, shift);
printDecipher(msg1, shift);
printEncrypt(msg2, shift);
printDecipher(msg2, shift);
return 0;
}
void printEncrypt(char str[], int shift)
{
printf("\nEncrypting message...\n");
printf("Plaintext:\t%s", str);
encrypt(str, shift);
printf("Ciphertext:\t%s", str);
}
void printDecipher(char str[], int shift)
{
printf("\nDeciphering message...\n");
printf("Ciphertext:\t%s", str);
decipher(str, shift);
printf("Plaintext:\t%s", str);
}
//libcipher.h
void encrypt( char str[], int shift)
{
unsigned int i;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = (char) ((str[i]-'A'+ shift)%26+'A');
} else if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = (char) ((str[i]-'a'+ shift)%26+'a');
} else {
str[i]=str[i];
}
}
}
void decipher( char str[], int shift)
{
unsigned int i;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = (char) ((str[i]-'A'-shift+26)%26+'A');
} else if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = (char) ((str[i]-'a'- shift+26)%26+'a');
} else {
str[i]=str[i];
}
}
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.