Question

#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;

  

}

string getOriginalText (string hashedText) {

  

string originalText = "";

  

for (int i=0; i//iterate over each letter in the Hashed

  

{

  

if(i%4 == 0) //if the iteration variable is multiple of 4, means every 4th element; then add the char at that position

  

originalText = originalText + hashedText.at(i);

  

}

  

return originalText;

  

}

int main()

{

cout << "--------------------------------------" << endl;

cout << " Encrypted Text File " << endl;

cout << "--------------------------------------" << endl;

cout << "" << endl << endl;

cout << "(Check Encryption file and Decryption file for result)" << endl;

  

   // srand(time(NULL));

srand((unsigned)time(NULL));

  

string hashedText = "";

  

char ch;

  

fstream fin("/Users/Andipc/Desktop/Data file.txt", fstream::in); // Navigate to Data file.txt

  

while (fin >> noskipws >> ch) {

  

hashedText = hashedText + ch + addRandomString(3) ;

  

}

{

ofstream file;

file.open ("/Users/Andipc/Desktop/Encryption file.txt"); // Navigate to Encryption file.txt

file << hashedText << endl;

file.close();

  

ofstream filez;

filez.open ("/Users/Andipc/Desktop/Decryption file.txt"); // Navigate to Decryption file.txt

filez << getOriginalText(hashedText) << endl;

filez.close();

  

}

  

cout << "" << endl << endl << endl;

return 0;

  

}

**I want you to explain any line of codes in the program.**

make it clear so I could understand.

  

  

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

Note: Please find the explanation below.

If you need further help please comment.

Please give a thumbs up. Thanks.

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

Mainly This program is encrypting the data and decrypting it.

Encryption Scheme:

First it reads a file char by char. Now when it character by character it adds three random chars to it as shown below:

It is reading a and add 3 random chars oxx.

Similarly it is reading s and add 3 random chars con and merge with previous text.

Similarly it is reading d and add 3 random chars blk and merge with previous text and so on.

So by this encryption is done and final hashed value is stored in new file “Encryption file.txt”.

Decryption Scheme:

As in encryption we are adding 3 extra digits so here in decryption we are reading every 4th digit and merging with originalText. Finally storing it new file “Decryption file.txt”

Now code explaination:

#include<string>

#include<iostream>

#include<ctime>

#include<fstream>

using namespace std;

const int MAX = 26;

//Method to get random strings of size n

string addRandomString(int n)

{

       //creating array of 26 alphabets

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

       //Creating temporary variable res

       string res = "";

       //Creating random string of length n

       for (int i = 0; i < n; i++)

              res = res + alphabet[rand() % MAX]; //MAX is 26 so getting random number between 0 to 25 and getting char from alphabet array and adding to res.

       //Returning string of length n

       return res;

}

//this method will take hashed text and return original text

string getOriginalText (string hashedText) {

       //taking originalText and empty string

       string originalText = "";

       for (int i=0; i<hashedText.length();i++)//iterate over each letter in the Hashedtext

       {

              //Now as in encryption we are adding 3 extra characters so in decrytion we are taking every 4th letter

              // example -->original is abcd

              //encrypted will add 3 extra characters after every character. aXXXbXXXcXXXd

              //decrypting by reading characters at position 0,4,8,12 and we get original text abcd

              if(i%4 == 0) //if the iteration variable is multiple of 4, means every 4th element; then add the char at that position

                     originalText = originalText + hashedText.at(i);

       }

       //returning original text

       return originalText;

}

int main()

{

       //Displaying output results

       cout << "--------------------------------------" << endl;

       cout << " Encrypted Text File " << endl;

       cout << "--------------------------------------" << endl;

       cout << "" << endl << endl;

       cout << "(Check Encryption file and Decryption file for result)" << endl;

       // srand(time(NULL));

       //this will generate seed to find random numbers

       srand((unsigned)time(NULL));

       //original hashedText is empty

       string hashedText = "";

       //temp character variable

       char ch;

       //opening file to read original text

       fstream fin("/Users/Andipc/Desktop/Data file.txt", fstream::in); // Navigate to Data file.txt

       //reading file character by character

       while (fin >> noskipws >> ch) {

              //adding 3 random character to the read chracter. Also merging with old hashed text

              hashedText = hashedText + ch + addRandomString(3) ;

       }

       {

              //ofstream to output encrypted text in new file

              ofstream file;

              file.open ("/Users/faraz/Desktop/Encryption file.txt"); // Navigate to Encryption file.txt

              //printing hashedText to Encryption file.txt

              file << hashedText << endl;

              //closing file so that data is saved

              file.close();

              //ofstream to output original text in new file

              ofstream filez;

              filez.open ("/Users/faraz/Desktop/Decryption file.txt"); // Navigate to Decryption file.txt

              //printing OriginalText to Decryption file.txt after converting hashed text to original text

              filez<<getOriginalText(hashedText) << endl;

              //closing file to save data

              filez.close();

       }

       //just printing extra lines

       cout << "" << endl << endl << endl;

       return 0;

}

Add a comment
Know the answer?
Add Answer to:
#include #include #include #include #include #include #include using namespace std; const int MAX = 26; string...
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
  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std;...

    Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int...

    I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { string s; cout<< "Enter a string" <<endl; getline (cin,s); cout<< s <<endl; int vowels=0,consonants=0,digits=0,specialChar=0; for (int i=0; i<s.length(); i++) { char ch=s[i]; if (isalpha(s[i])!= 0){ s[i]= toupper(s[i]);    if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u') vowels++; else consonants++; } else if (isdigit(s[i])!= 0) digits++; else specialChar++; } cout<<"Vowels="<<vowels<<endl; cout<<"Consonants="<<consonants<<endl; cout<<"Digits="<<digits<<endl; cout<<"Special Characters="<<specialChar<<endl;...

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