1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
//function prototypes
void encryption(fstream &, char [], int, int);
void decryption(fstream &, char [], int, int);
int main()
{
//Declare variables
int key, userChoice;
const int ARRAY_SIZE = 10; //Array size
char characters[ARRAY_SIZE]; //declaring the array
//creating a fstream object
fstream dataFile;
//Ask the user to type make a choice between decrypt/encrypt key
cout << "Please type in your encryption/decryption key. An integer from 1 to 10: " << endl;
cin >> key;
//Validates that the key is from 1 to 10.
while (key < 1 || key > 10)
{
cout << "This is an invalid key. Please enter an integer from 1 to 10: " << endl;
cin >> key;
}
//Ask the user if they want to decrypt or encrypt
cout << "Please type in 0 to indicate encryption, or 1 to indicate decryption: " << endl;
cin >> userChoice;
//Validate that userChoice is 0 or 1.
while (userChoice < 0 || userChoice > 1)
{
cout << "This is an invalid choice. Please enter 0 for encryption or 1 for decryption " << endl;
cin >> userChoice;
}
//Determing what function to call. If 0 encrypt or if 1 decrypt.
if (userChoice == 0)
{
//call the encryption function
encryption(dataFile, characters[], ARRAY_SIZE, key);
}
else
{
//call the decryption function
decryption(dataFile, characters[], ARRAY_SIZE, key);
}
return 0;
}
void encryption (fstream &file, char chars[], int size, int key)
{
//opening the file
file.open("plaintext.txt", ios::in);
//initalizing a counter
int count = 0;
//Read the numbers from the file into the array.
while (count < size && file.get(chars[count]))
count++;
//close file
file.close();
//open file to write cipher text to
file.open("ciphertext.txt", ios::out);
//We are adding the key to each index of the array and then writing that to the ciphertext file.
for (count = 0; count < size; count++)
{
file.put(chars[count] + key);
}
//close file
file.close();
//display whats read
cout << "The characters read: ";
for (count = 0; count < size; count++)
cout << chars[count] << " ";
cout << endl;
}
void decryption (fstream &file, char chars[], int size, int key)
{
//open file to read the cipher text
file.open("ciphertext.txt", ios::in);
//initialize the counter variable
int count = 0;
//Read the numbers from the file into the array.
while (count < size && file.get(chars[count]))
count++;
//close file
file.close();
//opens the file to write the plain text
file.open("plaintext.txt", ios::out);
//We are subtracting the key to each index of the array and then writing that to the plaintext file.
for (count = 0; count < size; count++)
{
file.put(chars[count] - key);
}
//close file
file.close();
//display whats read
cout << "The characters read: ";
for (count = 0; count < size; count++)
cout << chars[count] << " ";
cout << endl;
}
|