Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need a code as very easy way! http://www.chegg.com/homework-help/questions-and

ID: 3606841 • Letter: I

Question

i need a code as very easy way!
http://www.chegg.com/homework-help/questions-and-answers/c-please-type-code-show-run-also-upload-output-q19924771
using a similar function like a above of link. its not same question above the link, a little bit different.

CSCE 1030: Homework 4 Due: 11:59 PM on Friday, November 3, 2017 BACKGROUND INFORMATION: A cryptosystem is a system for encryption and decryption. Encryption, also called encoding or enciphering, involves changing the original message into a secret message, while decryption, also called decoding or deciphering, involves just the opposite - changing the secret message back into its original readable form. In cryptography, a plaintext file is a file containing data in its original, or readable, form while a ciphertext file is a file containing data in coded, or scrambled, form In this assignment, you will be creating a modified stream cipher that shifts each original letter or digit to a new, encrypted letter or digit, respectively, corresponding to individual, randomly generated integral key. For example, consider the plaintext to be encrypted is "ABC 1.", without the quotes. Then, for each letter and digit, we generate a random integer between 3 and 277 that serves as the key for the corresponding letters and digits. Consider the following example Plaintext: Key: Ciphertext: ABC 1 76 97 74 75 YUY 6 Thus, the A would be shifted 76 places in the alphabet, which means that it "rolls over twice, and then gets shifted 24 places in the alphabet to the ciphertext Y. Notice that there are only four keys generated for the six characters. Whitespace (and any unsupported characters) should be discarded, so no key is generated in this case. Note that the whitespace has been removed in the ciphertext. Additionally, punctuation should be kept as is, meaning that it is not discarded, but left in its original form (e.g., the"." in the above example remains a ".") in the ciphertext. Mathematically, each letter of the plaintext and key can be given a number and a formula can be derived to encrypt for c (i.e., ciphertext) and m (i.e., plaintext) using k (i.e., key) as follows C- (m + k) % 26 m-(26 + c-k) % 26 Note that formula assumes A is 0, B is 1, and so forth with nothing to distinguish between uppercase A and lowercase a. Since you will most likely be using the character ASCII values, you will have to modify these formulas to fit your needs (e.g., the letter A has an ASCIl value of 65), but this should give you a place to start. One other item to consider is that characters (i.e., letters and digits in this case) roll over. For example, in shifting the letter z, then next letter would be the letter A (both uppercase)

Explanation / Answer

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;

void banner();
char select(char& choice);
void ceasar(ifstream& in_stream, ofstream& out_stream);
void caesar(ifstream& in_stream, ofstream& out_stream);


int main()
{
    char choice;
    char input_file[33];
    char output_file[33];
    ifstream fin;
    ofstream fout;

    banner(); // display info

    select(choice); // prompt choice

    if (choice == 'E' || choice == 'e') // if E then prompt for file to encrypt and output file to write to
    {
        cout << "Enter the name of your input file that you want to encrypt: ";
        cin >> input_file;
        cout << "Enter the name of the output file to write the ciphertext to: ";
        cin >> output_file;

        fin.open(input_file); // open files
        fout.open(output_file);

        if (fin.fail()) // ERROR check if file doesnt open properly
        {
            cout << "Input file failed to open. " << endl;
            exit(1);
        }

        ceasar(fin, fout); // process files and functions
        fout << endl;

        fin.close();
        fout.close(); // close files
    }
    if (choice == 'D' || choice == 'd') // same as above just for decryption
    {
        cout << "Enter the name of your input file that you want to decrypt: ";
        cin >> input_file;
        cout << "Enter the name of the output file to write the plaintext to: ";
        cin >> output_file;

        fin.open("input.txt");
        fout.open("output.txt");

        if (fin.fail())
        {
            cout << "Input file failed to open. " << endl;
            exit(1);
        }

        caesar(fin, fout);
        fout << endl;

        fin.close();
        fout.close();
    }

    return 0;
}

void banner() // personal info function
{
    cout << endl
         <<" +--------------------------------------------------+ "
         <<" |      Computer Science and Engineering         | "
         <<" |      CSCE 1030 - Computer Science I           | "
         <<" +--------------------------------------------------+ "
         << endl;
    return;
}

char select(char& choice) // choice func
{

    cout << "Would you like to ENCRYPT OR DECRYPT a file (E or D)? ";
    cin >> choice;

    switch (choice)
    {
        case 'E':
        case 'e':
            //Encryption
            break;
        case 'D':
        case 'd':
            //Decryption
            break;
        default:
            // loop back if E or D is not choosen
            return select(choice);
    }
}

void ceasar(ifstream& in_stream, ofstream& out_stream)
{
    int key; // key should be positive int
    char text; //specified by instructions to not use string array

    cout << "Enter the numerical key (i.e., an integer) used to encrypt: ";
    cin >> key;

    in_stream.get(text); // process input file text into function

    while (! in_stream.eof()) // read every character until EOF
    {

        if (isalpha(text)) // check for alphabet
        {
            if (text >= 'A' && text <= 'Z') // if upper
            {
                text = (((text - 'A') + key + 26) % 26) + 'A'; // function to cipher each character determined by key entered
                out_stream.put(text);
            }
            else if (text >= 'a' && text <= 'z') // if lower
            {
                text = (((text - 'a') + key + 26) % 26) + 'a'; // function to cipher each character determined by key entered
                out_stream.put(text);
            }
        }
        else if (isdigit(text)) // check for digit and cipher determined by key entered
        {
            if (text >= '0' && text <= '9')
            {
                text = (((text - '0') + key + 10) % 10) + '0';
                out_stream.put(text);
            }
        }

        in_stream.get(text); // process text in file

    }
    return;
}

void caesar(ifstream& in_stream, ofstream& out_stream) // process same as above just for decryption
{
    int key;
    char text;

    cout << "Enter the numerical key (i.e., an integer) used to decrypt: ";
    cin >> key;

    in_stream.get(text);

    while (! in_stream.eof())
    {

        if (isalpha(text))
        {
            if (text >= 'A' && text <= 'Z')
            {
                text = (((text - 'A') - key + 26) % 26) + 'A'; // function to cipher each character determined by key entered
                out_stream.put(text);
            }
            else if (text >= 'a' && text <= 'z')
            {
                text = (((text - 'a') - key + 26) % 26) + 'a'; // function to cipher each character determined by key entered
                out_stream.put(text);
            }
        }
        else if (isdigit(text)) // check for digit
        {
            if (text >= '0' && text <= '9')
            {
                text = (((text - '0') + key + 14) % 10) + '0';
                out_stream.put(text);
            }
        }

        in_stream.get(text);

    }
    return;
}