Encryption program only encrypting part of the file.

My program asks for a file input opens the file encrypts the text with a special key then send the encrypted text to another file. the problem is that it only encrypts part of the file and not the whole thing and i figured out that it has something to do with the length of the key i used david and the file was only two bytes and when i used haloreachbeta the file is 12 bytes can someone please help me figure out what is wrong?

code:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
bool restart;
char ans;
restart = true;

do{

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(),ios::binary);
if(source.fail()){
cout << "input file opening failed.\n";
system("pause");
exit(1);
}
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(),ios::trunc|ios::binary);
if (target.fail()){
cout << "output file opening failed.\n";
exit(1);
}

// 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),";
cout<<"[Note: if a space is entered the program will consider the code/password to be only what before the";
cout<<"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[100];
for(int i=0; i<codeNO; i++)

code_array[i] = code[i];

// END CODE INPUT INTO AN ARRAY ==================================

int j=0;
char file_array[100], enc_dec_array[100];
while(!source.eof())
{

// START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========


j=0;
while(!source.eof() && j<codeNO)
{
source.get(c);
file_array[j] = c;
//At this point, you can test for eof and influence codeNO's resulting value
if(source.eof())
codeNO = j;
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]);
}
source.close();
target.close();
// END OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ============

cout << "would you like to run the program again?\n"
<< "type y/n,\n"
<< "then hit return: ";
cin >> ans;
if (ans =='n');{
cout << "Good-Bye";
restart = false;
}
}while(restart);
system ("pause");
return (0);
}
oh and i cant seem to figure out how to make it loop through the program either
Topic archived. No new replies allowed.