Question
Please answer this problem without using iostream and without strings or arrays.
Just use stdio.h and math.h.


57677 1 Caesars Cipher (70 points) The Caesars cipher is a cryptographic method for encrypting text such that it becomes unreadable to a party with- out access to the cryptographic key. It is named after Julius Caesar, who allegedly used it to protect messages of military significance. The encryption and decryption operations are simple shifts of the alphabet letters in a cyclic fashion. Mathematically, for a key k, the encryption and decryption are defined as: Encryption: c = (1 + k) mod 26 , Decryption: z = (c-k) mod 26. To llustrate the use of the Ceasars cipher, consider the encryption of fry with key k -3. Letters f, r, y correspond to the 5th, 16th, and 24th letter of the alphabet (letter a being the Oth) f:(5 + 3) r: (16 + 3) y: (24 + 3) mod 26 = 8→i mod 26 19 → u mod 26-1 → b Similarly, the decryption of i,u,b follows the reverse process í: (8-3) u: (19-3) b:(1-3) mod 26 mod 26 mod 26 5-1 16 → r 24→ = NOTE: The modulo operation for negative numbers is different from the % arithmetic operator in C Write a C program that decerypts a fle named encrypted.txt and places the decryption outpat to a ile called decrypted.txt. A file encrypted with k 3 is provided with the assignment. You can use it to test your decryption function. You will know when you have succeeded because the text becomes readable. In your program . Ask the user to enter the decryption key . Repeat the request until the right key is entered.
media%2F723%2F7238c041-93bd-4d3d-a791-f7
1 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<stdio.h>
#include<stdlib.h>

int mod(int x, int y) {
        while(x < 0) {
                x += y;
        }
        return x % y;
}

char decrypt_fun(int shift, char l) {
        if(l <= 'z' && l >= 'a') {
                return 'a' + mod((int)(l - 'a') - shift, 26);
        }
        if(l <= 'Z' && l >= 'A') {
                return 'A' + mod((int)(l - 'A') - shift, 26);
        }
        return l;
}

char encrypt_fun(int shift, char l) {
        if(l <= 'z' && l >= 'a') {
                return 'a' + mod((int)(l - 'a') + shift, 26);
        }
        if(l <= 'Z' && l >= 'A') {
                return 'A' + mod((int)(l - 'A') + shift, 26);
        }
        return l;
}

int main(void) {
        int key = 3;

        int input;
        do {
                printf("Enter the decryption key: ");
                scanf("%d", &input);
                if(input != key) {
                        printf("Wrong key!\n");
                }
        } while(input != key);

        // remove the new line in buffer
        while (getchar () != '\n' );


        char code;
        FILE* inputFile = fopen("encrypted.txt", "r");
        FILE* outputFile = fopen("decrypted.txt", "w");

        //exit program if file doesn’t open
        if(inputFile==NULL || outputFile==NULL) {
                printf("File could not be open!!");
                exit(0);
        }

        do {
                code = (char)fgetc(inputFile);
                if(code != EOF) {
                        putc(decrypt_fun(key, code),outputFile);
                }
        } while(code != EOF);
        fclose(inputFile);
        fclose(outputFile);

        printf("File decrypted!\n");
        printf("Do you want to re-encrypt the file(y/n): ");
        scanf("%c", &code);
        if(code == 'y') {
                printf("Enter an encryption key: ");
                scanf("%d", &key);

                inputFile = fopen("decrypted.txt", "r");
                outputFile = fopen("encrypted.txt", "w");

                //exit program if file doesn’t open
                if(inputFile==NULL || outputFile==NULL) {
                        printf("File could not be open!!\n");
                        exit(0);
                }

                do {
                        code = (char)fgetc(inputFile);
                        if(code != EOF) {
                                putc(encrypt_fun(key, code),outputFile);
                        }
                } while(code != EOF);
                fclose(inputFile);
                fclose(outputFile);
                printf("File encrypted!\n");
        }
        printf("Goodbye!\n");
}

Hi. please find the code.. i have given code comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Please answer this problem without using iostream and without strings or arrays. Just use stdio.h and...
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
  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user...

    Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user to specify the key and the text to be encrypted. A Caesar cipher is a simple substitution cipher wherein each letter in the message is shifted a certain number of spaces down the alphabet -- this number is called the key. a b c d e f g h i j k l m n o p q r s t u v w...

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

  • Can someone please help me for this assignment? Cryptography — the science of secret writing —...

    Can someone please help me for this assignment? Cryptography — the science of secret writing — is an old science; the first recorded use was well before 1900 B.C. An Egyptian writer used previously unknown hieroglyphs in an inscription. We will use a simple substitution cypher called rot13 to encode and decode our secret messages. ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in...

  • the w 2. This problem explores the use of a one-time pad version of t In...

    the w 2. This problem explores the use of a one-time pad version of t In this scheme, the key is a stream of random numbers between 0 and example, if the key is 3 19 5..., then the first letter of plaintext is encrypted with a shift of 3 letters, the second with a shift of 19 letters, the third with a shift of 5 letters, and so on. a. Encrypt the plaintext sendmoremoney with the key stream 9...

  • ior new tr k%2005%281 %29, You are just hired by Google as a senior software developer...

    ior new tr k%2005%281 %29, You are just hired by Google as a senior software developer and your supervisor assigns you the following task: You are responsible for the development of a program that encrypts and decrypts text messages using the Caesar shift, which is one of the oldest and widely known forms of encryption Caesar shift is a type of substitution encryption method in which each letter in the text is 'shifted' a fixed mumber of positions down the...

  • Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers...

    Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers (the "backwards" cipher, the Rail Fence cipher, and the Column Transposition cipher) where the ciphertext is created via an altered presentation of the plaintext. The algorithm for each is detailed in the function descriptions in this section. (13 points) def backwards_cipher(plaintext, key): • Parameter(s): plaintext ----- a string; the message to be encrypted key ----- an integer; the number to control this cipher •...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • #include #include #include #include #include #include #include using namespace std; const int MAX = 26; string...

    #include #include #include #include #include #include #include using namespace std; const int MAX = 26; string addRandomString(int n) {    char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',    'h', 'i', 'j', 'k', 'l', 'm', 'n',    'o', 'p', 'q', 'r', 's', 't', 'u',    'v', 'w', 'x', 'y', 'z' };    string res = "";    for (int i = 0; i < n; i++)    res = res + alphabet[rand() % MAX];    return res;...

  • Create C++ program.Convert this string to Tap Code using structures. It is very important to use structures in this program. The tap code is based on a Polybius square using a 5×5 grid of letters repr...

    Create C++ program.Convert this string to Tap Code using structures. It is very important to use structures in this program. The tap code is based on a Polybius square using a 5×5 grid of letters representing all the letters of the Latin alphabet, except for K, which is represented by C. The listener only needs to discriminate the timing of the taps to isolate letters. Each letter is communicated by tapping two numbers the first designating the row (Down) the...

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