Header File Errors...

This is a discussion on Header File Errors... within the C++ Programming forums, part of the General Programming Boards category; I'm getting a bunch of errors with this header file i made. I wrote an encryption scheme and instead of ...

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

    Header File Errors...

    I'm getting a bunch of errors with this header file i made.

    I wrote an encryption scheme and instead of copy and pasting all the prototypes and such into any other program i want to use the algorithm in i decided to do the normal thing and put it in a header file.

    however, i set it up the way i "thought" it was supposed to be setup and the way i've used it before but i get a bunch of errors:
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\tdea\tdea\dea.h(3) : error C2065: 'string' : undeclared identifier
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\tdea\tdea\dea.h(3) : error C2065: 'text' : undeclared identifier
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\tdea\tdea\dea.h(3) : error C2182: 'appendString' : illegal use of type 'void'
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\tdea\tdea\dea.h(3) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Main.cpp
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\tdea\tdea\dea.h(3) : error C2065: 'string' : undeclared identifier
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\tdea\tdea\dea.h(3) : error C2065: 'text' : undeclared identifier
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\tdea\tdea\dea.h(3) : error C2182: 'appendString' : illegal use of type 'void'
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\tdea\tdea\dea.h(3) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    If you guys need the source code just lemme know.


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

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    probably std::string should be

    but the posting code that causes errors may help
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Oaky Doaky...

    Main.cpp:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include "dea.h"
    
    #define		VERSION		"1.0.0"
    
    using namespace std;
    
    void divyKey(string key, string &key0, string &key1, string &key2);
    
    void encryptTDEA(string &ptext, string key, string &ctext);
    void decryptTDEA(string ctext, string key, string &ptext);
    
    int main()
    {
    	//infinite loop
    	while(1)
    	{
    		//title
    		system("cls");
    		//title
    		cout<<"**************************************"<<endl
    			<<"*  Triple Daly Encryption Algorithm  *"<<endl
    			<<"*       T.D.E.A.			V. "<<VERSION<<"    *"<<endl
    			<<"**************************************"<<endl<<endl;
    		//menu
    		cout<<"(1) Encrypt"<<endl
    			<<"(2) Decrypt"<<endl
    			<<"(3) View File"<<endl
    			<<"(4) Write File"<<endl
    			<<"(5) Delete File"<<endl
    			<<"(6) Convert File"<<endl
    			<<"(7) About"<<endl
    			<<"(8) Help"<<endl
    			<<"(9) Exit"<<endl<<endl;
    		string mchoice;
    		cout<<"Choice: ";
    		getline(cin, mchoice);
    		//evaluate choice
    		if(mchoice == "1")
    		{
    			//Encrypt
    			continue;
    		}
    		else if(mchoice == "2")
    		{
    			//Decrypt
    			continue;
    		}
    		else if(mchoice == "3")
    		{
    			//view File
    			continue;
    		}
    		else if(mchoice == "4")
    		{
    			//Write File
    			continue;
    		}
    		else if(mchoice == "5")
    		{
    			//Delete File
    			continue;
    		}
    		else if(mchoice == "6")
    		{
    			//Convert File
    			continue;
    		}
    		else if(mchoice == "7")
    		{
    			//About
    			continue;
    		}
    		else if(mchoice == "8")
    		{
    			//Help
    			continue;
    		}
    		else if(mchoice == "9")
    		{
    			//Quit
    			break;
    		}
    		else{
    			//Wrong choice
    			cout<<endl<<"Incorrect Choice!";
    			cin.ignore();
    			continue;
    		}
    	}
    	return 0;
    }
    
    void divyKey(string key, string &key0, string &key1, string &key2)
    {
    	//Divide key into 3 64-bit chunks
    	// (assumes bounds checking beforehand)
    	for(int i=0; i<8; i++)
    	{
    		key0.push_back(key.at(i));
    	}
    	for(int i=8; i<16; i++)
    	{
    		key1.push_back(key.at(i));
    	}
    	for(int i=16; i<24; i++)
    	{
    		key2.push_back(key.at(i));
    	}
    }
    
    void encryptTDEA(string &ptext, string key, string &ctext)
    {
    	//pad
    	appendString(ptext);
    	//divide key
    	string k1;
    	string k2;
    	string k3;
    	divyKey(key, k1, k2, k3);
    	//ENCRYPT
    	string c1;
    	string c2;
    	encryptDEA(ptext, k1, c1);
    	encryptDEA(c1, k2, c2);
    	encryptDEA(c2, k3, ctext);
    	//DONE
    }
    
    void decryptTDEA(string ctext, string key, string &ptext)
    {
    	string k1;
    	string k2;
    	string k3;
    	string d1;
    	string d2;
    	divyKey(key, k1, k2, k3);
    	//DECRYPT
    	decryptDEA(ctext, k3, d1);
    	decryptDEA(d1, k2, d2);
    	decryptDEA(d2, k1, ptext);
    	//DONE
    }
    dea.h:
    Code:
    #define EXTENSION	".dea"
    
    void appendString(string &text);
    void divyBlocks(string ptext, string &block, int &rem);
    void ePermutation(string &block);
    void dPermutation(string &block);
    void divideText(string block, string &first, string &second, string &third, string &fourth);
    void divideKey(string key, string &left, string &right);
    void xor(string &text, string hKey);
    void eSubstitution(string &text);
    void dSubstitution(string &text);
    void clearAppend(string &text);
    
    void writeFile(string file, string text);
    void readFile(string file, string &text);
    void convertFile(string file, string ext);
    void deleteFile(string file);
    
    void encrypt(string &block, string key);
    void decrypt(string &block, string key);
    
    void encryptDEA(string &ptext, string key, string &ctext);
    void decryptDEA(string ctext, string key, string &ptext);
    dea.cpp:
    Code:
    #include "dea.h"
    
    void xor(string &text, string hKey)
    {
    	if(text.size() == hKey.size())
    	{
    		for(int i=0; i<text.size(); i++)
    		{
    			text.at(i) ^= hKey.at(i);
    		}
    	}
    }
    
    void appendString(string &text)
    {
    	int size = text.size();
    	size = size &#37; 16;
    	for(int i = (16-size); i > 0; i--)
    	{
    		text.push_back('x');
    	}
    }
    
    void divyBlocks(string ptext, string &block, int &rem)
    {
    	int size = (ptext.size() / 16);
    	int mult = size-rem;
    	int start = (mult * 16);
    	block.clear();
    	if(rem > 0)
    	{
    		for(int i=0; i<16; i++)
    		{
    			block.push_back(ptext.at(start + i));
    		}
    		rem--;
    	}
    }
    
    void divideText(string block, string &first, string &second, string &third, string &fourth)
    {
    	first.clear();
    	second.clear();
    	third.clear();
    	fourth.clear();
    	for(int i=0; i<4; i++)
    	{
    		first.push_back(block.at(i));
    	}
    	for(int i=4; i<8; i++)
    	{
    		second.push_back(block.at(i));
    	}
    	for(int i=8; i<12; i++)
    	{
    		third.push_back(block.at(i));
    	}
    	for(int i=12; i<16; i++)
    	{
    		fourth.push_back(block.at(i));
    	}
    }
    
    void ePermutation(string &block)
    {
    	char temp;
    	char temp2;
    	for(int i=0; i<3; i++)
    	{
    		//shift everything one to the left
    		temp = block.at(0);
    		for(int j=3; j>=0; j--)
    		{
    			temp2 = block.at(j);
    			block.at(j) = temp;
    			temp = temp2;
    		}
    	}
    }
    
    void dPermutation(string &block)
    {
    	char temp;
    	char temp2;
    	for(int i=0; i<3; i++)
    	{
    		temp = block.at(3);
    		for(int j=0; j<4; j++)
    		{
    			temp2 = block.at(j);
    			block.at(j) = temp;
    			temp = temp2;
    		}
    	}
    }
    
    void divideKey(string key, string &left, string &right)
    {
    	left.clear();
    	right.clear();
    	for(int i=0; i<4; i++)
    	{
    		left.push_back(key.at(i));
    	}
    	for(int i=4; i<8; i++)
    	{
    		right.push_back(key.at(i));
    	}
    }
    
    void encrypt(string &block, string key)
    {
    	string first, second, third, fourth;
    	divideText(block, first, second, third, fourth);
    	string kLeft, kRight;
    	divideKey(key, kLeft, kRight);
    	//Run the encryption
    	//ROUND 1
    	// xor left key and first block
    	xor(first, kLeft);
    	// substitute
    	eSubstitution(second);
    	// xor right key and 3rd block
    	xor(third, kRight);
    	// permutate
    	ePermutation(fourth);
    	//ROUND 2
    	xor(fourth, kLeft);
    	eSubstitution(first);
    	xor(second, kRight);
    	ePermutation(third);
    	//ROUND 3
    	xor(third, kLeft);
    	eSubstitution(fourth);
    	xor(first, kRight);
    	ePermutation(second);
    	//ROUND 4
    	xor(second, kLeft);
    	eSubstitution(third);
    	xor(fourth, kRight);
    	ePermutation(first);
    	//FINISHED ONE BLOCK
    	block.clear();
    	//reset the block
    	block = (first + second + third + fourth);
    	//DONE
    }
    
    void decrypt(string &block, string key)
    {
    	string first, second, third, fourth;
    	divideText(block, first, second, third, fourth);
    	string kLeft, kRight;
    	divideKey(key, kLeft, kRight);
    	//Run the decryption
    	//ROUND 1
    	dPermutation(first);
    	xor(second, kLeft);
    	dSubstitution(third);
    	xor(fourth, kRight);
    	//ROUND 2
    	xor(first, kRight);
    	dPermutation(second);
    	xor(third, kLeft);
    	dSubstitution(fourth);
    	//ROUND 3
    	dSubstitution(first);
    	xor(second, kRight);
    	dPermutation(third);
    	xor(fourth, kLeft);
    	//ROUND 4
    	xor(first, kLeft);
    	dSubstitution(second);
    	xor(third, kRight);
    	dPermutation(fourth);
    	//FINISHED ONE BLOCK
    	block.clear();
    	//reset the block
    	block = (first + second + third + fourth);
    	//DONE
    }
    
    void eSubstitution(string &text)
    {
    	int subTable[256] = {27, 144, 241, 223, 244, 24, 1, 60, 149, 154, 50, 174, 70, 132, 167, 86, 38, 188, 3, 233, 128, 130, 39, 118, 175, 22, 46, 84, 191, 42, 19, 113, 153, 225, 227, 110, 114, 254, 25, 230, 181, 185, 207, 2, 226, 243, 150, 236, 187, 14, 77, 116, 249, 137, 182, 133, 125, 17, 152, 197, 143, 105, 208, 57, 168, 119, 36, 224, 120, 189, 146, 90, 166, 101, 210, 201, 11, 82, 196, 23, 56, 43, 44, 159, 12, 157, 123, 5, 156, 96, 97, 160, 242, 9, 171, 121, 220, 95, 85, 41, 212, 237, 69, 139, 20, 228, 194, 59, 253, 195, 203, 161, 7, 200, 192, 169, 115, 21, 204, 91, 30, 75, 164, 252, 172, 117, 88, 13, 89, 247, 32, 108, 206, 178, 65, 81, 198, 145, 147, 170, 190, 209, 54, 67, 0, 179, 79, 6, 26, 4, 64, 124, 98, 62, 29, 193, 127, 104, 45, 140, 74, 218, 93, 134, 176, 232, 28, 202, 221, 234, 49, 35, 235, 155, 68, 217, 102, 33, 142, 251, 58, 231, 131, 135, 63, 163, 205, 87, 112, 165, 222, 245, 40, 72, 141, 246, 186, 92, 238, 255, 173, 37, 111, 240, 15, 76, 126, 248, 199, 122, 148, 213, 16, 48, 34, 78, 103, 47, 158, 219, 52, 73, 18, 106, 94, 183, 99, 53, 214, 83, 177, 107, 100, 211, 61, 66, 51, 138, 239, 31, 80, 8, 250, 71, 55, 109, 184, 229, 215, 10, 162, 151, 216, 180, 129, 136};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text.at(i) = ((char)subTable[((int)((unsigned char)text.at(i)))]);
    	}
    }
    
    void dSubstitution(string &text)
    {
    	int subTable[256] = {144, 6, 43, 18, 149, 87, 147, 112, 241, 93, 249, 76, 84, 127, 49, 204, 212, 57, 222, 30, 104, 117, 25, 79, 5, 38, 148, 0, 166, 154, 120, 239, 130, 177, 214, 171, 66, 201, 16, 22, 192, 99, 29, 81, 82, 158, 26, 217, 213, 170, 10, 236, 220, 227, 142, 244, 80, 63, 180, 107, 7, 234, 153, 184, 150, 134, 235, 143, 174, 102, 12, 243, 193, 221, 160, 121, 205, 50, 215, 146, 240, 135, 77, 229, 27, 98, 15, 187, 126, 128, 71, 119, 197, 162, 224, 97, 89, 90, 152, 226, 232, 73, 176, 216, 157, 61, 223, 231, 131, 245, 35, 202, 188, 31, 36, 116, 51, 125, 23, 65, 68, 95, 209, 86, 151, 56, 206, 156, 20, 254, 21, 182, 13, 55, 163, 183, 255, 53, 237, 103, 159, 194, 178, 60, 1, 137, 70, 138, 210, 8, 46, 251, 58, 32, 9, 173, 88, 85, 218, 83, 91, 111, 250, 185, 122, 189, 72, 14, 64, 115, 139, 94, 124, 200, 11, 24, 164, 230, 133, 145, 253, 40, 54, 225, 246, 41, 196, 48, 17, 69, 140, 28, 114, 155, 106, 109, 78, 59, 136, 208, 113, 75, 167, 110, 118, 186, 132, 42, 62, 141, 74, 233, 100, 211, 228, 248, 252, 175, 161, 219, 96, 168, 190, 3, 67, 33, 44, 34, 105, 247, 39, 181, 165, 19, 169, 172, 47, 101, 198, 238, 203, 2, 92, 45, 4, 191, 195, 129, 207, 52, 242, 179, 123, 108, 37, 199};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text.at(i) = ((char)subTable[((int)((unsigned char)text.at(i)))]);
    	}
    }
    
    void encryptDEA(string &ptext, string key, string &ctext)
    {
    	//CBC Mode
    	string prevBlock = "";
    	appendString(ptext);
    	string block;
    	int r = ptext.size()/16;
    	//ENCRYPTION
    	for(int i=r; i>0; i--)
    	{
    		divyBlocks(ptext, block, r);
    		xor(block, prevBlock);
    		encrypt(block, key);
    		prevBlock = block;
    		ctext += block;
    	}
    }
    
    void decryptDEA(string ctext, string key, string &ptext)
    {
    	//CBC Mode
    	string block;
    	int j;
    	string prevBlock = "";
    	string xblock = "";
    	int r = ctext.size()/16;
    	//DECRYPTION
    	j = r;
    	for(int i=0; i<j; i++)
    	{
    		divyBlocks(ctext, block, r);
    		prevBlock = block;
    		decrypt(block, key);
    		xor(block, xblock);
    		xblock = prevBlock;
    		ptext += block;
    	}
    	clearAppend(ptext);
    }
    
    void writeFile(string file, string text)
    {
    	file += EXTENSION;
    	ofstream fout(file.c_str());
    	for(int i=0; i<text.size(); i++)
    	{
    		fout<<((int)text.at(i))<<" ";
    	}
    	fout.close();
    }
    
    void readFile(string file, string &text)
    {
    	file += EXTENSION;
    	ifstream fin(file.c_str());
    	text.clear();
    	int temp;
    	vector<int> v;
    	while(fin>>temp)
    	{
    		v.push_back(temp);
    	}
    	fin.close();
    	for(int i=0; i<v.size(); i++)
    	{
    		text.push_back((char)v.at(i));
    	}
    }
    
    void convertFile(string file, string ext)
    {
    	if(ext == "dea" || ext == "DEA" || ext == "Dea")
    	{
    		//file already converted
    	}else{
    		string filename = file + "." + ext;
    		ifstream fin(filename.c_str());
    		char ch;
    		string contents;
    		while(fin.get(ch))
    		{
    			contents+=ch;
    		}
    		fin.close();
    		writeFile(file, contents);
    		remove(filename.c_str());
    	}
    }
    
    void clearAppend(string &text)
    {
    	int i = text.size();
    	i--;
    	while(text.at(i) == 'x')
    	{
    		text.erase(text.begin()+i);
    		i--;
    	}
    }
    
    void deleteFile(string file)
    {
    	//First wipe it
    	file += EXTENSION;
    	ofstream fout(file.c_str());
    	fout.close();
    	//Then delete the actual file
    	remove(file.c_str());
    }

    Thanks!

    PS--------

    Obviously Main.cpp is still a work in progress
    Last edited by Junior89; 07-07-2007 at 11:00 PM. Reason: PS---
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    You have not included <string> in dea.h.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    also in dea.h all string should be replaced by
    std::string
    because you cannot use
    using namespace in the header (actually can but never should)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks guys! Much appreciated! =)
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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