A new project i'm undertaking...

This is a discussion on A new project i'm undertaking... within the C++ Programming forums, part of the General Programming Boards category; DWKS: Is this the right way to go about this CBC thing? Cause it wont work right =( Code: //*****************ENCRYPT ...

  1. #16
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    DWKS:

    Is this the right way to go about this CBC thing? Cause it wont work right =(

    Code:
    //*****************ENCRYPT FILE FUNCTION*******************//
    void encryptFile(string filename)
    {
    	//Get the contents of the file
    	ifstream fin(filename.c_str());
    	char ch;
    	string temp;
    	while(fin.get(ch))
    	{
    		temp += ch;
    	}
    	fin.close();
    	//Encrypt
    	string encrypted;
    	string hPassword;
    	//Get the hash value of the password
    	unsigned int hPass;
    	ifstream fin2(SETUP_FILE);
    	string getTemp;
    	while(fin2.get(ch))
    	{
    		getTemp += ch;
    	}
    	fin2.close();
    	int j;
    	for(int i=0; i<getTemp.size(); i++)
    	{
    		if(getTemp.at(i) == '\n')
    			j=(i+1);
    	}
    	for(j; j<getTemp.size(); j++)
    	{
    		hPassword += getTemp.at(j);
    	}
    	hPass = stringToInt(hPassword);
    	//Encryption Algorithm Start
    	encrypted.push_back(temp.at(0)^hPass);
    	for(int i=1; i<temp.size(); i++)
    	{
    		encrypted.push_back(encrypted.at(i-1)^hPass);
    	}
    	//Encryption Algorithm End
    
    	//Save to file
    	ofstream fout(filename.c_str());
    	fout<<encrypted;
    	fout.close();
    }
    
    //**********************DECRYPT FILE FUNCTION************************//
    void decryptFile(string filename)
    {
    	//Get the contents of the file
    	ifstream fin(filename.c_str());
    	char ch;
    	string temp;
    	while(fin.get(ch))
    	{
    		temp += ch;
    	}
    	fin.close();
    	//Encrypt
    	string decrypted;
    	string hPassword;
    	//Get the hash value of the password
    	unsigned int hPass;
    	ifstream fin2(SETUP_FILE);
    	string getTemp;
    	while(fin2.get(ch))
    	{
    		getTemp += ch;
    	}
    	fin2.close();
    	int j;
    	for(int i=0; i<getTemp.size(); i++)
    	{
    		if(getTemp.at(i) == '\n')
    			j=(i+1);
    	}
    	for(j; j<getTemp.size(); j++)
    	{
    		hPassword += getTemp.at(j);
    	}
    	hPass = stringToInt(hPassword);
    	//Encryption Algorithm Start
    	decrypted.push_back(temp.at(temp.size()-1)^hPass);
    	for(int i=1; i < temp.size()-1; i++)
    	{
    		decrypted.push_back(decrypted.at(i-1)^hPass);
    	}
    	//Encryption Algorithm End
    
    	//Save to file
    	ofstream fout(filename.c_str());
    	fout<<decrypted;
    	fout.close();
    }
    How do i implement the CBC correctly?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #17
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    DWKS or anyone for that matter haha Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sorry, I didn't get on CBoard yesterday . . .

    No, it isn't, because it isn't working. I didn't explain CBC very well, because I didn't actually expect you to use it.

    With CBC, each character is XORed with the IV (initialization vector) as well as the key. The IV is just some number -- you can pick it -- and for subsequent character, the previous enciphered character is the IV.

    This is a great example -- a picture is worth a thousand words: http://upload.wikimedia.org/wikipedi...encryption.png
    It's from this wikipedia entry, which is worth reading: http://en.wikipedia.org/wiki/Block_c...s_of_operation

    [edit] Sections in for loops can be empty:
    Code:
    for(j; j<getTemp.size(); j++)
    You don't really need to keep all of the information in a string. You could process it character by character, writing each character to the file once you're done with it. [/edit]
    Last edited by dwks; 04-15-2007 at 08:05 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Okay, well this works but i know its not the CBC:

    Code:
    //*****************ENCRYPT FILE FUNCTION*******************//
    void encryptFile(string filename)
    {
    	//Get the contents of the file
    	ifstream fin(filename.c_str());
    	char ch;
    	string temp;
    	while(fin.get(ch))
    	{
    		temp += ch;
    	}
    	fin.close();
    	//Encrypt
    	string encrypted;
    	string hPassword;
    	//Get the hash value of the password
    	unsigned int hPass;
    	ifstream fin2(SETUP_FILE);
    	string getTemp;
    	while(fin2.get(ch))
    	{
    		getTemp += ch;
    	}
    	fin2.close();
    	int j;
    	for(int i=0; i<getTemp.size(); i++)
    	{
    		if(getTemp.at(i) == '\n')
    			j=(i+1);
    	}
    	for(j; j<getTemp.size(); j++)
    	{
    		hPassword += getTemp.at(j);
    	}
    	hPass = stringToInt(hPassword);
    	
    	//Encryption Algorithm Start
    	encrypted += (temp.at(0)^5)^hPass;
    	for(int r=1; r<temp.size(); r++)
    	{
    		encrypted += temp.at(r) ^ hPass;
    	}
    	//Encryption Algorithm End
    
    	//Save to file
    	ofstream fout(filename.c_str());
    	fout<<encrypted;
    	fout.close();
    }
    
    //**********************DECRYPT FILE FUNCTION************************//
    void decryptFile(string filename)
    {
    	//Get the contents of the file
    	ifstream fin(filename.c_str());
    	char ch;
    	string temp;
    	while(fin.get(ch))
    	{
    		temp += ch;
    	}
    	fin.close();
    	//Encrypt
    	string decrypted;
    	string hPassword;
    	//Get the hash value of the password
    	unsigned int hPass;
    	ifstream fin2(SETUP_FILE);
    	string getTemp;
    	while(fin2.get(ch))
    	{
    		getTemp += ch;
    	}
    	fin2.close();
    	int j;
    	for(int i=0; i<getTemp.size(); i++)
    	{
    		if(getTemp.at(i) == '\n')
    			j=(i+1);
    	}
    	for(j; j<getTemp.size(); j++)
    	{
    		hPassword += getTemp.at(j);
    	}
    	hPass = stringToInt(hPassword);
    	//Decryption Algorithm Start
    	decrypted += (temp.at(0)^5) ^ hPass;
    	for(int r=1; r<temp.size(); r++)
    	{
    		decrypted += temp.at(r) ^ hPass;
    	}
    	//Decryption Algorithm End
    
    	//Save to file
    	ofstream fout(filename.c_str());
    	fout<<decrypted;
    	fout.close();
    }

    Thoughts?
    Last edited by Junior89; 04-15-2007 at 08:51 PM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #20
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    How can i make a variable global (editable by ALL functions -- including those in other .cpp files)? I broke my project up into different header files, one for menu functions, one for secure functions, one for setup functions, etc. But there's this one variable i need some of those things in different header files to access. How do i do it?

    I have one header file, called global.h which holds all of the the other include files and header files. I put the variable there and it compiles at first but it gets a linker error saying there's unresolved externals i think =(

    Can anyone help me out here?

    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #21
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Perhaps you can do it as link your global to the header file which first uses the global, then so on and so forth.

    Something like; first.h ->include globals.h, second.h-> include first.h, main.cpp->include second.h.

    It's actually rather difficult to visualize....

  7. #22
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    yeah... ha, you lost me there.

    There's a header called global.h, it holds All of the other header files and includes, as well as the using namespace std; bit. Now i also put the global variable (unsigned int hp in there as well, hoping that all of the other files that include that file (global.h) would have access to it.


    I dunno if i'm coming across well either =P haha

    EDIT-----------

    Here's the code for global.h:
    Code:
    //Standard Include Files
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <vector>
    #include <string>
    
    //Global Variables
    unsigned int hp;
    
    //Namespaces
    using namespace std;
    
    //Custom Include Files
    #include "setup.h"
    #include "secure.h"
    #include "menus.h"
    #include "grades.h"
    Last edited by Junior89; 04-15-2007 at 10:03 PM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  8. #23
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Unfortunately, you may need someone smarter than I to help you. About your code above though, is it valid to declare variables before including your header files? For some reason, that seems a rather odd thing to do. Or is that simply a typed-in typo?

  9. #24
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Junior89 View Post
    Here's the code for global.h:
    Code:
    //Standard Include Files
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <vector>
    #include <string>
    
    //Global Variables
    unsigned int hp;
    
    //Namespaces
    using namespace std;
    
    //Custom Include Files
    #include "setup.h"
    #include "secure.h"
    #include "menus.h"
    #include "grades.h"
    That kinda looks like the classic "what not to do" approach. Seems dandy when you've gone beyond the simple; looks, well, like what not to do, later on.

    Quote Originally Posted by Junior89 View Post
    I've been really bored lately and i want some programming to do! haha so i asked my mom if she wanted me to make her a program to keep track of her grades at school. She uses a gradebook and spends hours calculating final grades and such so i figured i'd make her life easier.
    [aside]Spreadsheets do such things nicely.[/aside]
    Last edited by Dave_Sinkula; 04-15-2007 at 11:00 PM. Reason: Consecutive posts.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #25
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I've tried the spreadsheet thing before but the grading system at her school requires a more 'powerful' scripting, to put it that way.

    A c++ program is the way to go. And what's wrong with my approach with the global.h? This is the first time i've ever used my own header files and such cause this project is so big.

    What should i do instead of that?

    EDIT--------

    I moved the global variable down after the includes and i still get the same errors =(
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  11. #26
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Junior89 View Post
    And what's wrong with my approach with the global.h?
    In general you shouldn't be putting using namespace std; in any header ever*. And each header should only contain the necessary #includes to declare such things as the header does.

    The panacea approach always introduces more problems than it is worth, IMO.

    *Yeah, yeah, yeah.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,733
    And what's wrong with my approach with the global.h? This is the first time i've ever used my own header files and such cause this project is so big.
    1. Generally, it is better to avoid using such a global variable. Also, if you do insist on using it, I think it should be declared extern in the header file.
    2. "using namespace std;" in a header file defeats the point of namespaces.
    3. You may include header files that you do not need for a given source file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    If you are interested in a more in-depth look at block cipher modes of operation, I've written a tutorial about it not so long ago. There's also a section dedicated to CBC. All you have to do is remove the call to aes_encrypt(), since you don't apply a block cipher to your plaintext.

  14. #29
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    So what you guys are saying is to take all the stuff in global.h and move it to the main?

    My only question is that wouldn't i then have to put all those includes in the other includes as well? Cause some of the header files functions rely on other header file functions =(

    Probably not the way header files are supposed to work but i used this to keep all my functions sorted neatly so i knew if i needed to look up the encrypt() function for example i could do so in the secure.h and secure.cpp header files.


    And thanks for that article KONI, it was interesting, my problem is that i'm always the one who wants to 'reinvent the wheel' by writing my own algorithm. =( haha
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  15. #30
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Junior89 View Post
    I've tried the spreadsheet thing before but the grading system at her school requires a more 'powerful' scripting, to put it that way.

    A c++ program is the way to go.
    That's like saying "I need to get to the second floor of this building. A Saturn rocket is the way to go." I think it's great that you want to write a C++ program for this task, and I think you should for the sake of experience, but be realistic: what you are doing is WAY overkill.

    I highly doubt the grading system requires any processing more complicated than could be provided by VBA.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undertaking Website Building Project and Application Software Development
    By Programmer168 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-21-2009, 04:44 AM
  2. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21