Question
Can someone convert this program to parallel programing using open mp Parallelize Implement one round of cryptography LIGHT including encryption and decryption and test it
Can someone convert this program to parallel programing using open mp
Parallelize Implement one round of cryptography LIGHT including encryption and decryption and test it for a string of 32 bits for the multicore computer using OpenMP and test it. Compare the running time for 1, 2, 4 cores, respectively.
#include "pch.h"
#include
#include
#include
#include
using namespace std;
//DECLARING DYNAMIC CHARACTER ARRAYS FOR encyrptionKey.txt
char *characterArray;
char *codeArray;
//MAIN METHOD
int main() {
int number;
string getInput;
//GET THE NUMBER OFLINES IN THE encyptionKey.txt
number = numberOfLines("encyptionKey.txt");
//ALLOCATE SPACE FOR THE DYNAMIC CHARACTER ARRAY
characterArray = new char[number];
codeArray = new char[number];
ifstream readFromFile("encryptionKey.txt");
int k1 = 0;
if (!readFromFile.fail()) {
cout << "Copying key file: ";
//STORE CHARACTER AND CODEIN THE ARRAY
while (readFromFile >> characterArray[k1] >> codeArray[k1]) {
cout << endl << characterArray[k1] << "-" << codeArray[k1];
k1++;
}
}
else {
cout << "Sorry! encryptionKey reading error ";
}
//CLOSE THE FILE
readFromFile.close();
//GET THE CHOICE FROM THE USER
cout << "Please enter 'en' for encyption and 'de' for decryption" << endl;
cin >> getInput;
//ENCRYPTION
if (getInput == "en") {
cout << "Files encryptionKey.txt and text.txt are being processed..." << endl;
encrypt("text.txt");
cout << "Please open the output file textEncrypted.txt" << endl;
}
//DECRYPTION
else if (getInput == "de") {
cout << "Files encryptionKey.txt and textEncrypted.txt are being processed..." << endl;
decrypt("textEncrypted.txt");
cout << "Please open the output file textDecrypted.txt" << endl;
}
system("pause");
return 0;
}//END MAIN
//FUNCTION FIND THE NUMBER OF LINES IN THE GIVEN FILE
int numberOfLines(string fileName) {
string line;
int number = 0;
//OPEN THE FILE FOR READING
ifstream readFromFile(fileName);
//UNTIL THERE IS A LINE IN THE FILE READ THE LINE
while (getline(readFromFile, line)) {
//INCREASE THE NUMBER BY 1
number++;
}
//CLOSE THE FILE
readFromFile.close();
//RETURN THE NUMBER
return number;
}
//PERFORMS THE ENCRYPTION OF THE GIVEN FILE
void encrypt(string fileName) {
//DYNAMIC ARRAY TO HOLD THE FILE LINES
string *line;
//GET THE NUMBER OF LINES IN THE FILE
int number = numberOfLines(fileName);
//ALLOCATE SPACE FOR THE DYNAMIC ARRAY
line = new string[number];
string tempLine;
int k = 0;
//OPEN THE text.txt FOR READING
ifstream readFromFile(fileName);
//OPEN THE textEncryption.txtFORWRITING THE ENCRYPTED LINE
ofstream writeToFile;
writeToFile.open("textEncrypted.txt");
if (writeToFile.fail()) {
cout << " error to write ";
system("pause");
exit(1);
}
//Find the number of characters in characerArray
int len = 0;
while (characterArray[len] != NULL) {
len++;
}
//UNTIL THERE IS A LINE IN THE FILE
while (getline(readFromFile, tempLine)) {
//STORE THE READ LINE IN STRING ARRAY
line[k] = tempLine;
//DO THE ENCRYPTION
//FOR EACH CHARACTER IN THE LINE IF CODE IS AVAILBLE FOR THE //CORRESPONDING CHRACTER THEN REPLACE THE CHRACTER WITH THE CODE
for (int k1 = 0; k1 < tempLine.length(); k1++) {
for (int k2 = 0; k2 < len; k2++) {
if (line[k][k1] == characterArray[k2]) {
line[k][k1] = codeArray[k2];
}
}
}
//WRITE THE ENCRYPTED LINE
writeToFile << line[k] << endl;
k++;
}
//CLOSE THE FILES
writeToFile.close();
readFromFile.close();
}
//PPERFORMS THE DECRYPTION
void decrypt(string fileName) {
//DYNAMIC ARRAY TO HOLD THE FILE LINES
string *line;
//GET THE NUMBER OF LINES IN THE FILE
int number = numberOfLines(fileName);
//ALLOCATE SPACE FOR THE DYNAMIC ARRAY
line = new string[number];
string tempLine;
int k = 0;
//OPEN THE textEncrypted.txt FOR READING
ifstream readFromFile(fileName);
//OPEN THE textDecryption.txtFOR WRITING THE DECRYPTED LINE
ofstream writeToFile("textDecrypted.txt");
//Find the number of characters in characerArray
int len = 0;
while (characterArray[len] != NULL) {
len++;
}
//UNTIL THERE IS A LINE IN THE FILE
while (getline(readFromFile, tempLine)) {
//STORE THE READ LINE IN STRING ARRAY
line[k] = tempLine;
//DO THE DECRYPTION
//FOR EACH CODE IN THE LINE IF CHRACTER IS AVAILBLE FOR THE //CORRESPONDING CODE THEN REPLACE THE CODE WITH THE CHARACTER
for (int k1 = 0; k1 < tempLine.length(); k1++) {
for (int k2 = 0; k2 < len; k2++) {
if (line[k][k1] == codeArray[k2])
line[k][k1] = characterArray[k2];
}
}
//WRITE TOFILE
writeToFile << line[k] << endl;
k++;
}
//CLOSE THE FILES
writeToFile.close();
readFromFile.close();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started