Oh yes, true.Originally Posted by The Brain
Oh yes, true.Originally Posted by The Brain
It ain't illegal until you get caught..
The Program still gives a missing encrypted/decrypted file !!
It ain't illegal until you get caught..
I am using the original test case:
using the original password: amy and still getting a similar result as in your original post. No problem though, closing in on a solution.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
Last edited by The Brain; 05-22-2005 at 10:40 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
I'm not sure if you're familiar with the new and delete operators, but if it's easier (and allows you to avoid the loop), then what you could do is get the size of the file, create a char array using new for the exact size, and read it in in one fell swoop. Something like this:
Code:std::fstream iFile(filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate); int size = iFile.tellg(); // Get the size of the file. iFile.seekg(0, std::ios::beg); // Seek to the beginning of the file. char* temp = new char[size]; // Create an array of the size of the file. iFile.read(temp, size); // Read in our data to the array. iFile.close();
This is what I got so far.. it's giving me better results.. I can actually get this far after the second xor:
This program is desinged by: Amy
This program is to encrypt/decrypt a
Code:int j=0; char file_array[32], enc_dec_array[32]; 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]); } // END OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ============ }
- "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
Well, I do not think I can use this way, because i Am asked to stick to a certain way; given by the prompt, here is the original prompt of the program:Originally Posted by Lithorien
C++ provides three bitwise (bit-by-bit) operators (bitwise AND (&), bitwise OR ( | ) and the bitwise XOR ( ^ ).
A function is to be implemented to receive the character (C) and the encryption byte (B) and return the encrypted character (D). Notice that this function is also used for decryption. Also, we want another function 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.
We want to use the above functions in a program to receive a file of characters from the HD and to encrypt its characters to form another file using your secret key. The program will also recover the original file using the same secret key.
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..
Looking at that prompt, I can tell you that you'd be allowed to use new and delete. They didn't restrict how large your x[] array could be, only how large the KEY could be. And the x[] array is what I was talking about using new and delete for.
Another hint I can give you is that you can do the encryption and decryption in one for() loop. Just start at 0, go to strlen() of your buffer, and keep a counter for your key array. I'll psuedo-code this, but this should also help you.
Code:for (int x = 0, y = 0; x < strlen(crypt_buffer[]); x++) { //XOR crypt_buffer[x] against key[y].. // If y's value is equal to the last element in key[], set y to 0 and continue.. }
Hope this can help a little.
I've been able to determine that the target file is being written to successfully with the entire xor'd code.. however, when it comes time to read from the encrypted file, I think it's detecting eof prematurely for some reason
Last edited by The Brain; 05-22-2005 at 12:07 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
[QUOTE=Lithorien]Looking at that prompt, I can tell you that you'd be allowed to use new and delete. They didn't restrict how large your x[] array could be, only how large the KEY could be.[/code]
Well they did not restrict it in the prompt, but my instructor restricted that by telling us in class
But thanks a lot this helped me much for my personal knowledge..
And The Brain, Yes.. I don't know why the error comes in this part !
It ain't illegal until you get caught..
After I stopped being bullheaded and took hk_mp5kpdw's advice, you got a working 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(),ios::binary); 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); // 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 i=0; i<codeNO; i++) code_array[i] = code[i]; // END CODE INPUT INTO AN ARRAY ================================== int j=0; char file_array[32], enc_dec_array[32]; 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]); } // END OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ============ } return 0; }
Now if hk_mp5kpdw would like to elaborate on why this needed to be in binary.. I would be willing to listen and learn.
Last edited by The Brain; 05-22-2005 at 01:09 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
Oh, Thanks a lot for that Brain
It ain't illegal until you get caught..
The reason it needed to be in binary is because, when you XOR something, the results of the XOR might be a non-ASCII character. This equals NULL in ascii, and when you detect eof(), that 0 will screw you up. I think, anyway.
Ahh, I got it, thanks a lot, Lithorien..
It ain't illegal until you get caught..