Question

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 the cipher key is 3, the clear text “HELLO THERE” becomes “KHOOR WKHUH”. Here, we restrict ourselves to encrypting/decrypting digits (0…9), lowercase, and uppercase alphabetic characters. As you know from the ASCII table, the set of these characters correspond to the integers 30 to 122. Hint: The following formula can be used to encrypt a character C using the Caesar cipher algorithm: E = (C – ‘0’ + k) % 93 + ‘0’ [where k is the cipher key] You need to figure out the formula for decrypting text on your own. NOTE: Use cipher key = 7. Write a C++ program for encrypting and decrypting a given string. Since this program performs two different functionalities (encryption and decryption), prompt the user to select the type of cryptographic technique as shown below: Welcome to Cryptographic Techniques Program Please enter your selection: Encrypt Decrypt When the user selects 1 or 2, s/he will be asked to specify an input and output message. Here is an example: Assume that the user enters the following message: HOW ARE YOU DOING? If the user selects to encrypt the message (i.e. option 1), the program will encrypt the original message and outputs an encrypted text. If the user selects to decrypt a message (i.e. option 2), the program will decrypt the encrypted text and outputs the decrypted text on the screen. The program should give the user the option whether to continue with encrypting/decrypting messages (i.e. entering letter ‘C’) or exit the program (i.e. entering the letter ‘E’).

IMPORTANT: Can you also put comments on your code? As much as I want to get the right answer, I also want to understand the code, and where I went wrong.

Homework Answers

Answer #1

I have written the code which is simple to understand and well commented and to encrypt and decrypt the string using cyper key=7 , i have used the properties of mod operator to have the effect of curcular array. It is advised to see the ASCII table while reffering this code.

CODE:

#include<bits/stdc++.h>
using namespace std;
#define c_key 7 //in question given to use cipher key=7;

string encrypt(string input_str){
    string output_str;
    for(int i=0;i<input_str.size();i++){
        if(input_str[i]>='0' && input_str[i]<='9'){ //if a number
            /*since there are 10 numbers so using mod 10 also get the number and add c_key=7 and take mod by 10 this will give you the offset then just add '0';
            */
            output_str+=(input_str[i]-'0'+c_key)%10+'0'; //mod 10 used to circularly enrcypt
        }
        else if(input_str[i]>='A' && input_str[i]<='Z'){ //if uppercase letter
            /*since there are 26 uppercase letters so using mod 26 also get the letter and add c_key=7 and take mod by 26 this will give you the offset then just add 'A';
            */
            output_str+=(input_str[i]-'A'+c_key)%26+'A';
        }
        else if(input_str[i]>='a' && input_str[i]<='z'){ //if lowercase letter
            /* since there are 26 lowercase letters so using mod 26 also get the letter and add c_key=7 and take mod by 26 this will give you the offset then just add 'a';
            */
            output_str+=(input_str[i]-'a'+c_key)%26+'a';
        }
    }
    return output_str;
}

string decrypt(string input_str){
    string output_str;
    for(int i=0;i<input_str.size();i++){
        if(input_str[i]>='0' && input_str[i]<='9'){ //if a number
            /*same logic as in encryption in this instead of adding just subtract c_key and add 10(because 10 numbers) because result can be negative when subtracting c_key and then take mod by 10
            */
            output_str+=(input_str[i]-'0'-c_key+10)%10+'0';
        }
        else if(input_str[i]>='A' && input_str[i]<='Z'){ //if uppercase letter
            /*same logic as in encryption in this instead of adding just subtract c_key and add 26(because 26 uppercase letter) because result can be negative when subtracting c_key and then take mod by 26
            */
            output_str+=(input_str[i]-'A'-c_key+26)%26+'A';
        }
        else if(input_str[i]>='a' && input_str[i]<='z'){ //if lowercase letter
            /*same logic as in encryption in this instead of adding just subtract c_key and add 26(because 26 lowercase letter) because result can be negative when subtracting c_key and then take mod by 26
            */
            output_str+=(input_str[i]-'a'-c_key+26)%26+'a'; 
        }
    }
    return output_str;
}


int main(){
    string input_str;
    int choice;
    char ch='C';
    cout<<"Welcome To Cryptographic Techniques\n";
    while(ch!='E'){
        cout<<"Enter the string\n";
        cin>>input_str;
        cout<<"Enter 1 to encrypt the above entered string\n";
        cout<<"Enter 2 to decrypt the above entered string\n";
        cin>>choice;
        if(choice==1){
            string output_str=encrypt(input_str);
            cout<<"Encrypted string is: "<<output_str<<"\n";
        }
        if(choice==2){
            string output_str=decrypt(input_str);
            cout<<"Decrypted string is: "<<output_str<<"\n";
        }
        cout<<"If You want to continue press 'C'\n";
        cout<<"If you want to exit press 'E'\n";
        cin>>ch;
    }
    return 0;
}

Input and 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
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Alice is sending message “HIDE” to Bob. Perform encryption and decryption using RSA algorithm, with the...
Alice is sending message “HIDE” to Bob. Perform encryption and decryption using RSA algorithm, with the following information: Parameters p q e 11 5 7 Present all the information that you will need to encrypt and decrypt only the first letter from text.
QUESTION 1 What does DK(EK(P)) correspond to? Plain Text Cypher Text Gibberish Encryption Key None of...
QUESTION 1 What does DK(EK(P)) correspond to? Plain Text Cypher Text Gibberish Encryption Key None of these QUESTION 2 What is the goal of Cryptanalysis? Attempt to break a single message Attempt to recognize patterns in encrypted messages Attempt to deduce the key Attempt to find weaknesses in the implementation Attempt to break the algorithm All of these QUESTION 3 What is the maximum possible keys in Ceaser cipher? QUESTION 4 Choose ALL that are true for Symmetric Key Encryption...
C Program Type a program in C to encode the message with Caesar's cipher. Your program...
C Program Type a program in C to encode the message with Caesar's cipher. Your program will receive a text message, and the key value will then generate an encryption text for the given message. example: Enter a message to encrypt: axzd Enter key: 4 Encrypted message: ebdh **Make sure your program can check that the given message is not empty AND Your program must include a helper function that accepts the input message and checks that it contains only...
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...
NWS620S Tutorial 1: Symmetric Encryption - DES Encryption is the translation of data into a secret...
NWS620S Tutorial 1: Symmetric Encryption - DES Encryption is the translation of data into a secret code so that only authorised entities can read it. Encrypting data is considered a very effective way of achieving data security. To access encrypted data, you must have access to a secret key that enables you to decrypt it. Unencrypted data is called plain text; encrypted data is referred to as cipher text. There are two types of encryption: • Symmetric encryption • Asymmetric...
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...
Write a program in Java that allows users to decrypt files. The file encryption technique that...
Write a program in Java that allows users to decrypt files. The file encryption technique that was used to encrypt the file is a Caesar cipher. A Caesar Cipher works by shifting all the letters in the file by a certain amount. So for a file with an encryption key of 1 an “a” will be represented by a “b”, and a “b” will be represented by a “c” in the encrypted file. With an encryption key of 2, an...
C++ Pig Latin Lab This assignment uses pointers to perform something done often in computer applications:...
C++ Pig Latin Lab This assignment uses pointers to perform something done often in computer applications: the parsing of text to find “words” (i.e., strings delineated by some delimiter). Write a program that encodes English language phrases into Pig Latin. Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form Pig Latin phrases. Use the following algorithm: to form a Pig Latin phrase from an English language phrase, tokenize...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT