Thread: Encrypt/Decrypt files by XOR

  1. #1
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question Encrypt/Decrypt files by XOR

    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 imve
    Which is MISSING (Not like the original info.txt one)



    The Program Code:
    ===============

    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";
    cout<<"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),";
    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[32];
    for(int m=0; m<codeNO; m++)
    {
    code_array[m] = code[m];
    }
    // END CODE INPUT INTO AN ARRAY ==================================
     
    while(!source.eof())
    {
     
    // START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========
     
    char file_array[32];
     
    for(int j=0; j<codeNO; j++)
    {
    source.get(c);
    file_array[j] = c;
    }
    // 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 ==========
    char enc_dec_array[32];
    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;
    }
    

    // END OF Program


    If some one can help me with that and tell me why this happens I will be thankful.
    Thanks for time,
    -Amy
    Last edited by amirahasanen1; 05-22-2005 at 08:35 AM.
    It ain't illegal until you get caught..

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    --------------------Configuration: cryptic - Win32 Debug--------------------
    Compiling...
    cryptic.cpp
    f:\c++ source\cryptic.cpp(41) : error C2440: '=' : cannot convert from 'char' to 'char [32]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    Error executing cl.exe.

    cryptic.exe - 1 error(s), 0 warning(s)
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by The Brain
    --------------------Configuration: cryptic - Win32 Debug--------------------
    Compiling...
    cryptic.cpp
    f:\c++ source\cryptic.cpp(41) : error C2440: '=' : cannot convert from 'char' to 'char [32]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    Error executing cl.exe.

    cryptic.exe - 1 error(s), 0 warning(s)
    Sorry, which part is that? I do not get this error and the program runs, so I don't know which part this compiler meant..
    It ain't illegal until you get caught..

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    f:\c++ source\cryptic.cpp(41)

    Indicates that there is a good possiblity there is a problem somewhere in the vicinity of line #41


    Code:
    char code_array[32];
    for(int i=0; i<codeNO; i++)
    {
    code_array = code[i];
    This is good time to start looking at getting a new compiler.. because the one you have now let you get away with assigning 32 elements of an array to a char pointer.

    I think you meant to do something like this:

    Code:
    char code_array[32];
    for(int i=0; i<codeNO; i++)
    {
    code_array[i] = code[i];
    Last edited by The Brain; 05-22-2005 at 08:23 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by The Brain
    Indicates that there is a good possiblity there is a problem in the vicinity of line #41


    Code:
    char code_array[32];
    for(int i=0; i<codeNO; i++)
    {
    code_array = code[i];
    This is the time to start looking at gettting a new compiler.. because the one you have now let you get away with assigning 32 elements of an array to a char pointer.

    I think you meant to do something like this:

    Code:
    char code_array[32];
    for(int i=0; i<codeNO; i++)
    {
    code_array[i] = code[i];
    Ah, well yea, I have no problem about that, it is only the boards that consider the [ i ] as italic so it cancels it and italize the font instead, sorry about not noticing that, but in my .cpp file it has no mistakes, so that is not the error, the main problem is that the file is encrypted/decrypted missing some parts at the end (i.e.; only a part from the beginning of the file is encrypted/decrypted)..
    It ain't illegal until you get caught..

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    i'm on the case now ma'am.. got example files to work.. stepping through the debugger as we speak..
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    still working, but here is one thing i notice:

    Code:
    	while(!source.eof())
    	{
     
    	// START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========
     
    	char file_array[32];
     
    	for(int j=0; j<codeNO; j++)
    	{
    		source.get(c);
    		file_array[j] = c;
    	}
    /	/ 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 ==========
    char enc_dec_array[32];
    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 ============
    }

    I am very picky about what I put inside a loop.. declaring variables inside a loop != good. It's slow. It's something that can be done prior to the loop. It might be contributing to your problem. Basically, you are declaring new arrays with every while loop iteration.
    Last edited by The Brain; 05-22-2005 at 08:54 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by The Brain
    still working, but here is one thing i notice:

    Code:
    while(!source.eof())
    	{
     
    		// START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========
     
    		char file_array[32];
     
    		for(int j=0; j<codeNO; j++)
    		{
    		source.get(c);
    		file_array[j] = c;
    	}

    I am very picky about what I put inside a loop.. declaring variables inside a loop != good. It's slow. It's something that can be done prior to the loop. It might be contributing to your problem. Basically, you are declaring a new array with every while loop iteration.
    I have thought of that too, before I post the code and I got this declaration outside the loop, but still no change, anyway, I will now place it outside the loop, it might be contributing to the problem as you said.. but still the file is missing, I too am trying to figure out what is wrong now, working on that...
    It ain't illegal until you get caught..

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    your arrays are only large enough to handle 31 characters, plus 1 null terminating character. "This program is to encrypt/decrypt a certain file from the drive to another file on the drive as well" has 101 characters.


    [edit] ok.. this is fine because your loops are restricted to codeNO iterations [/edit]
    Last edited by The Brain; 05-22-2005 at 09:46 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  10. #10
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by The Brain
    your arrays are only large enough to handle 31 characters, plus 1 null terminating character. "This program is to encrypt/decrypt a certain file from the drive to another file on the drive as well" has 101 characters.
    Well, I don't want to put a termination character in the array because when I have put:
    Code:
    while(!source.eof())
    I wanted the loop to be repeated to read the next (n) characters everytime, where (n = codeNO.. which is the number of characters in the secret key)..
    It ain't illegal until you get caught..

  11. #11
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    That is part of what the prompt of the program is:

    ...to encrypt/decrypt a “block” of characters of a given length stored in an array x[ ] using an encryption block of characters representing the secret “key” with the same length and to return the encrypted/decrypted block y[ ]. The key length should not exceed 32 characters.
    Notice that the file size might not be an integral multiple of the chosen key length. In this case, the last block on the file will be encrypted/decrypted using only part of the key.
    It ain't illegal until you get caught..

  12. #12
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    ok.. i think i'm closing in on a problem..

    Code:
    while(!source.eof())
    	{
     
    		// START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========
     
    		
     
    		for(int j=0; j<codeNO; j++)
    		{
    			source.get(c);
    			file_array[j] = c;
    		}

    You are executing the loop while !eof.. and then with each loop iteration.. you are reading for example 32 characters into file_array[32]... that's fine.. but what if you are let's say 2 characters from eof.. and then you call to read in the next 32 characters? Not good. So I am thinking, instead of using a hard coded for loop.. why not use a while loop. This will give you the opportunity to break out of the loop if there are less than 32 characters remaining in the file.

    Code:
    	char file_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<32)
    		{
    			source.get(c);
    			file_array[j] = c;
                            j++
    		}
    Last edited by The Brain; 05-22-2005 at 07:36 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need to open the files in binary mode.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  14. #14
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    I am thinking to replace this:
    Code:
    int j=0;
    while(!source.eof() && j<32)
    {
    	 source.get(c);
    	 file_array[j] = c;
    	 j++;
    }
    By this:
    Code:
     
    int j=0;
    while(!source.eof() && j<codeNO)
    {
    	source.get(c);
    	file_array[j] = c;
    	j++;
    }
    if(source.eof())
    	codeNO = j;
    I am using (j<codeNO), because everytime I do not want the program to read 32 characters, but to read an equal number of characters to that number of secret key characters ("codeNO")..
    and I used:
    Code:
    if(source.eof())
    	codeNO = j;
    Because if the key is for example 5 characters, but there are only 3 characters before the (eof), only a part of the key (i.e. first 3 characters) are used later in this part:
    Code:
    for(int k=0; k<codeNO; k++)
    {
    	enc_dec_array[k] = file_array[k] ^ code_array[k];
    	target.put(enc_dec_array[k]);
    }

    But still no change !!
    It ain't illegal until you get caught..

  15. #15
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    int j=0;
    while(!source.eof() && j<codeNO)
    {
    	source.get(c);
    	file_array[j] = c;
    	j++;
    }
    //This condition will always test true
    if(source.eof())
    	codeNO = j;
    Not good. If you want to do it that way, you would have to do something like this:


    Code:
    while(!source.eof() && j<codeNO)
    {
    	source.get(c);
    	file_array[j] = c;
    	j++;
            //At this point, you can test for eof and influence codeNO's resulting value
            if(source.eof())
    	     codeNO = j;
    }
    Last edited by The Brain; 05-22-2005 at 01:44 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM