This program is to receive a .txt file from the HDD and encrypt it by 'XOR' (eXclusive Or) to another file throught a user defined secret key, and this program can decrypt the encrypted file as well into a new file using the same secret key..
The problem is that it does everything as it is supposed to do but it returns the file in the final case missing, like for example I used this text file "info.txt" to be encrypted to a file called "enc.txt", then I ran the program again and decrypted the file "enc.txt" into a file called "dec.txt".. And the secret key I used was "amy" The three files look like the following:
1. "info.txt" (The original file):
This program is desinged by: Amy
This program is to encrypt/decrypt a certain file from the drive to another file on the drive as well
2. "enc.txt" (The encrypted file):
5 A IA
ASR c& I
ARM N
IA I
I I M
IA R RMA MA
3. "dec.txt" (The decrypted file):
This program is desinged by: Amy
This program is to encrypt/deq
Which is MISSING (Not like the original info.txt one)
The Program Code:
================
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
char c;
ifstream source;
ofstream target;
string source_fname;
string target_fname;
cout<<"Please enter the path of the file to be encrypted/decrypted: "<<endl;
cin>>source_fname;
source.open(source_fname.c_str());
cout<<endl;
cout<<"Please enter the path and name of the new file, that the first file is to be encrypted/decrypted to: "<<endl;
cin>>target_fname;
target.open(target_fname.c_str());
// START CODE INPUT INTO AN ARRAY =============================
string code;
cout<<"\n\nPlease input a code/password of not greater than 32 characters (including no spaces/blanks), [Note: if a space is entered the program will consider the code/password to be only what before the space/blank]: "<<endl;
cin>>code;
int codeNO; // number of characters of the code
codeNO = code.length();
while(codeNO>32)
{
cout<<"The code entered is greater than 32 characters, please reenter a new code: ";
getline(cin,code);
codeNO = code.length();
}
char code_array[32];
for(int i=0; i<codeNO; i++)
{
code_array = code;
}
// END CODE INPUT INTO AN ARRAY ==================================
char file_array[32];
char enc_dec_array[32];
while(!source.eof())
{
// START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========
int j=0;
while(!source.eof() && j<codeNO)
{
source.get(c);
file_array[j] = c;
j++;
if(source.eof())
codeNO = j;
}
// END PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY =============
// ************************************************
// START OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ==========
for(int k=0; k<codeNO; k++)
{
enc_dec_array[k] = file_array[k] ^ code_array[k];
target.put(enc_dec_array[k]);
}
// END OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ============
}
return 0;
}
The problem is that it does everything as it is supposed to do but it returns the file in the final case missing, like for example I used this text file "info.txt" to be encrypted to a file called "enc.txt", then I ran the program again and decrypted the file "enc.txt" into a file called "dec.txt".. And the secret key I used was "amy" The three files look like the following:
1. "info.txt" (The original file):
This program is desinged by: Amy
This program is to encrypt/decrypt a certain file from the drive to another file on the drive as well
2. "enc.txt" (The encrypted file):
5 A IA
ASR c& I
ARM N
IA I
I I M
IA R RMA MA
3. "dec.txt" (The decrypted file):
This program is desinged by: Amy
This program is to encrypt/deq
Which is MISSING (Not like the original info.txt one)
The Program Code:
================
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
char c;
ifstream source;
ofstream target;
string source_fname;
string target_fname;
cout<<"Please enter the path of the file to be encrypted/decrypted: "<<endl;
cin>>source_fname;
source.open(source_fname.c_str());
cout<<endl;
cout<<"Please enter the path and name of the new file, that the first file is to be encrypted/decrypted to: "<<endl;
cin>>target_fname;
target.open(target_fname.c_str());
// START CODE INPUT INTO AN ARRAY =============================
string code;
cout<<"\n\nPlease input a code/password of not greater than 32 characters (including no spaces/blanks), [Note: if a space is entered the program will consider the code/password to be only what before the space/blank]: "<<endl;
cin>>code;
int codeNO; // number of characters of the code
codeNO = code.length();
while(codeNO>32)
{
cout<<"The code entered is greater than 32 characters, please reenter a new code: ";
getline(cin,code);
codeNO = code.length();
}
char code_array[32];
for(int i=0; i<codeNO; i++)
{
code_array = code;
}
// END CODE INPUT INTO AN ARRAY ==================================
char file_array[32];
char enc_dec_array[32];
while(!source.eof())
{
// START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========
int j=0;
while(!source.eof() && j<codeNO)
{
source.get(c);
file_array[j] = c;
j++;
if(source.eof())
codeNO = j;
}
// END PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY =============
// ************************************************
// START OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ==========
for(int k=0; k<codeNO; k++)
{
enc_dec_array[k] = file_array[k] ^ code_array[k];
target.put(enc_dec_array[k]);
}
// END OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ============
}
return 0;
}