Question

USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...

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);
}

Homework Answers

Answer #1

//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:

Know the answer?
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
Similar Questions
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the...
There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the letters in a message are replaced by the letters of a “shifted” alphabet. A “Caesar Cipher” encodes a message by shifting each letter in a message by a constant amount of k. If k is 5, A becomes F, B becomes G, C becomes H and so on. Let's use Queue to encode and decode the encrypt message. Set k as -5 and decipher...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable...
Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable using gcc -Wall main5C.c vec5C.c Do this: Add function definitions to vec5C.c so that functions are available for dot product, sum, and cross product. Do not change the function prototypes in vec5C.h. Then add code to main to call these functions and display the dot product of u and v, the sum of u and v, and the cross product of u and v....
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...