Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encrypt/Decrypt files by XOR

Status
Not open for further replies.

ahasanen

Technical User
May 22, 2005
1
EG
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;
}

 
I think you might be limiting the result in your char definitions...

Code:
[red]
    char file_array[32];
    char enc_dec_array[32];

[/red]
[green]//Try increasing these to 64 or 128...[/green]
[blue]
    char file_array[128];
    char enc_dec_array[128];

[/blue]


Hope that helps,

Ron

typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
I actually can't figure out why it's decrypting that much. It should never decrypt more than 32 bites of data because that is the maximum password size and all your loops only go that far. an alternate solution that didn't include so many temp variable might also look like this
Code:
void xorCrypt(std::string &infile,const std::string &pass)
{
for (int i=0;i<infile.size();i++)
{
    infile[i]^=pass%i;
}
}
of course I don't have a compiler in front of me so take it with a grain of salt




WR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top