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

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

C Program:

File: libcipher.h

#ifndef LIBCIPHER_H_INCLUDED
#define LIBCIPHER_H_INCLUDED

//Function prototypes
void encrypt(char str[], int shift);
void decipher(char str[], int shift);

#endif // LIBCIPHER_H_INCLUDED

File: libcipher.c

#include <stdlib.h>
#include <stdio.h>
#include "libcipher.h"

void encrypt(char str[], int shift)
{
    int i, len, val;

    len = strlen(str);

    //Looping over each character
    for(i=0; i<len; i++)
    {
        //Getting Ascii values
        val = (int)(str[i]);

        //For Upper case letters
        if(val >= 65 && val <= 90)
        {
            //Boundary crossing condition
            if( (val + shift) > 90 )
                val = 65 + ( (val + shift) - 90 ) - 1;
            else
                val = val + shift;

            //Encrypting data
            str[i] = (char)(val);
        }
        else if(val >= 97 && val <= 122)
        {
            //For Lower case letters
            if( (val + shift) > 122 )
                val = 97 + ( (val + shift) - 122 ) - 1;
            else
                val = val + shift;

            //Encrypting
            str[i] = (char)(val);
        }
        else
            str[i] = str[i];
    }
}


void decipher(char str[], int shift)
{
    int i, len, val;

    len = strlen(str);

    //Looping over each character
    for(i=0; i<len; i++)
    {
        //Getting Ascii values
        val = (int)(str[i]);

        //For Upper case letters
        if(val >= 65 && val <= 90)
        {
            //Boundary crossing condition
            if( (val - shift) < 65 )
                val = 90 + ( val - shift - 65 ) + 1;
            else
                val = val - shift;

            //Decrypting
            str[i] = (char)(val);
        }
        else if(val >= 97 && val <= 122)
        {
            //Lower case
            if( (val - shift) < 97 )
                val = 122 + ( val - shift - 97 ) + 1;
            else
                val = val - shift;

            //Decrypting
            str[i] = (char)(val);
        }
        else
            str[i] = str[i];
    }
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

CTCicipher bin Debuglcipher.exe nter the shift parameter: ncrypting message iphertext: iphertext: Plaintext: ABCDEFGHIJKLMNOP

Add a comment
Know the answer?
Add Answer to:
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
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
  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • 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];                                                      ...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

  • 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); } //_________________________________________________________________________________________________________________________________________________...

  • Programming in C. Name this program schwifty.c - This program reads a text file and makes...

    Programming in C. Name this program schwifty.c - This program reads a text file and makes it schwifty, but the user determines the schwiftiness. The user supplies the filename to schwift and a string containing a sequence of the following characters to determine the schwiftiness via command line arguments: L - Left shift each character in a word: hello --> elloh R - Right shift each character in a word: elloh --> hello I - Shift the letters' and digits'...

  • Python program Use the provided shift function to create a caesar cipher program. Your program s...

    python program Use the provided shift function to create a caesar cipher program. Your program should have a menu to offer the following options: Read a file as current message Save current message Type in a new message Display current message "Encrypt" message Change the shift value For more details, see the comments in the provided code. NO GLOBAL VARIABLES! Complete the program found in assignment.py. You may not change any provided code. You may only complete the sections labeled:#YOUR...

  • 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();...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

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