Hi all,

I kind of new in C++ and my assignment require me to do some encryption on data. Found the AES file online can anyhow guide me how to use it ? Because when i compile the test.cpp it keep giving me exception not match error. And btw how do i make use of his file ? just by using Rijndael.h at every cpp file that require the encryption ?


Main program

//Test.cpp



#include "Rijndael.h"

#include <iostream>



using namespace std;



//Function to convert unsigned char to string of length 2

void Char2Hex(unsigned char ch, char* szHex)

{

	unsigned char byte[2];

	byte[0] = ch/16;

	byte[1] = ch%16;

	for(int i=0; i<2; i++)

	{

		if(byte[i] >= 0 && byte[i] <= 9)

			szHex[i] = '0' + byte[i];

		else

			szHex[i] = 'A' + byte[i] - 10;

	}

	szHex[2] = 0;

}



//Function to convert string of length 2 to unsigned char

void Hex2Char(char const* szHex, unsigned char& rch)

{

	rch = 0;

	for(int i=0; i<2; i++)

	{

		if(*(szHex + i) >='0' && *(szHex + i) <= '9')

			rch = (rch << 4) + (*(szHex + i) - '0');

		else if(*(szHex + i) >='A' && *(szHex + i) <= 'F')

			rch = (rch << 4) + (*(szHex + i) - 'A' + 10);

		else

			break;

	}

}    



//Function to convert string of unsigned chars to string of chars

void CharStr2HexStr(unsigned char const* pucCharStr, char* pszHexStr, int iSize)

{

	int i;

	char szHex[3];

	pszHexStr[0] = 0;

	for(i=0; i<iSize; i++)

	{

		Char2Hex(pucCharStr[i], szHex);

		strcat(pszHexStr, szHex);

	}

}



//Function to convert string of chars to string of unsigned chars

void HexStr2CharStr(char const* pszHexStr, unsigned char* pucCharStr, int iSize)

{

	int i;

	unsigned char ch;

	for(i=0; i<iSize; i++)

	{

		Hex2Char(pszHexStr+2*i, ch);

		pucCharStr[i] = ch;

	}

}



void main()

{

	try

	{

		char szHex[33];

		//One block testing

		CRijndael oRijndael;

		oRijndael.MakeKey("abcdefghabcdefgh", "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 16);

		char szDataIn[] = "aaaaaaaabbbbbbbb";

		char szDataOut[17] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

		oRijndael.EncryptBlock(szDataIn, szDataOut);

		CharStr2HexStr((unsigned char*)szDataIn, szHex, 16);

		cout << szHex << endl;

		CharStr2HexStr((unsigned char*)szDataOut, szHex, 16);

		cout << szHex << endl;

		memset(szDataIn, 0, 16);

		oRijndael.DecryptBlock(szDataOut, szDataIn);

		CharStr2HexStr((unsigned char*)szDataIn, szHex, 16);

		cout << szHex << endl;

	}

	catch(exception& roException)

	{

		cout << roException.what() << endl;

	}

}
In file included from Test.cpp:4:
Rijndael.h: In member function ‘void CRijndael::Xor(char*, const char*)’:
Rijndael.h:80: error: no matching function for call to ‘std::exception::exception(const char*&)’
/usr/include/c++/4.3/exception:59: note: candidates are: std::exception::exception()
/usr/include/c++/4.3/exception:57: note:                 std::exception::exception(const std::exception&)
Rijndael.h: In member function ‘int CRijndael::GetKeyLength()’:
Rijndael.h:116: error: no matching function for call to ‘std::exception::exception(const char*&)’
/usr/include/c++/4.3/exception:59: note: candidates are: std::exception::exception()
/usr/include/c++/4.3/exception:57: note:                 std::exception::exception(const std::exception&)
Rijndael.h: In member function ‘int CRijndael::GetBlockSize()’:
Rijndael.h:124: error: no matching function for call to ‘std::exception::exception(const char*&)’
/usr/include/c++/4.3/exception:59: note: candidates are: std::exception::exception()
/usr/include/c++/4.3/exception:57: note:                 std::exception::exception(const std::exception&)
Rijndael.h: In member function ‘int CRijndael::GetRounds()’:
Rijndael.h:132: error: no matching function for call to ‘std::exception::exception(const char*&)’
/usr/include/c++/4.3/exception:59: note: candidates are: std::exception::exception()
/usr/include/c++/4.3/exception:57: note:                 std::exception::exception(const std::exception&)
Test.cpp: At global scope:
Test.cpp:65: error: ‘::main’ must return ‘int’
//Rijndael.h

#ifndef __RIJNDAEL_H__
#define __RIJNDAEL_H__

#include <exception>
#include <cstring>

using namespace std;

//Rijndael (pronounced Reindaal) is a block cipher, designed by Joan Daemen and Vincent Rijmen as a candidate algorithm for the AES.
//The cipher has a variable block length and key length. The authors currently specify how to use keys with a length
//of 128, 192, or 256 bits to encrypt blocks with al length of 128, 192 or 256 bits (all nine combinations of
//key length and block length are possible). Both block length and key length can be extended very easily to
// multiples of 32 bits.
//Rijndael can be implemented very efficiently on a wide range of processors and in hardware. 
//This implementation is based on the Java Implementation used with the Cryptix toolkit found at:
//http://www.esat.kuleuven.ac.be/~rijmen/rijndael/rijndael.zip
//Java code authors: Raif S. Naffah, Paulo S. L. M. Barreto
//This Implementation was tested against KAT test published by the authors of the method and the
//results were identical.
class CRijndael
{
public:
	//Operation Modes
	//The Electronic Code Book (ECB), Cipher Block Chaining (CBC) and Cipher Feedback Block (CFB) modes
	//are implemented.
	//In ECB mode if the same block is encrypted twice with the same key, the resulting
	//ciphertext blocks are the same.
	//In CBC Mode a ciphertext block is obtained by first xoring the
	//plaintext block with the previous ciphertext block, and encrypting the resulting value.
	//In CFB mode a ciphertext block is obtained by encrypting the previous ciphertext block
	//and xoring the resulting value with the plaintext.
	enum { ECB=0, CBC=1, CFB=2 };

private:
	enum { DEFAULT_BLOCK_SIZE=16 };
	enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 };

	//Auxiliary Functions
	//Multiply two elements of GF(2^m)
	static int Mul(int a, int b)
	{
		return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0;
	}

	//Convenience method used in generating Transposition Boxes
	static int Mul4(int a, char b[])
	{
		if(a == 0)
			return 0;
		a = sm_log[a & 0xFF];
		int a0 = (b[0] != 0) ? sm_alog[(a + sm_log[b[0] & 0xFF]) % 255] & 0xFF : 0;
		int a1 = (b[1] != 0) ? sm_alog[(a + sm_log[b[1] & 0xFF]) % 255] & 0xFF : 0;
		int a2 = (b[2] != 0) ? sm_alog[(a + sm_log[b[2] & 0xFF]) % 255] & 0xFF : 0;
		int a3 = (b[3] != 0) ? sm_alog[(a + sm_log[b[3] & 0xFF]) % 255] & 0xFF : 0;
		return a0 << 24 | a1 << 16 | a2 << 8 | a3;
	}

public:
	//CONSTRUCTOR
	CRijndael();

	//DESTRUCTOR
	virtual ~CRijndael();

	//Expand a user-supplied key material into a session key.
	// key        - The 128/192/256-bit user-key to use.
	// chain      - initial chain block for CBC and CFB modes.
	// keylength  - 16, 24 or 32 bytes
	// blockSize  - The block size in bytes of this Rijndael (16, 24 or 32 bytes).
	void MakeKey(char const* key, char const* chain, int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE);

private:
	//Auxiliary Function
	void Xor(char* buff, char const* chain)
	{
		if(false==m_bKeyInit)
			throw exception(sm_szErrorMsg1);
		for(int i=0; i<m_blockSize; i++)
			*(buff++) ^= *(chain++);	
	}

	//Convenience method to encrypt exactly one block of plaintext, assuming
	//Rijndael's default block size (128-bit).
	// in         - The plaintext
	// result     - The ciphertext generated from a plaintext using the key
	void DefEncryptBlock(char const* in, char* result);

	//Convenience method to decrypt exactly one block of plaintext, assuming
	//Rijndael's default block size (128-bit).
	// in         - The ciphertext.
	// result     - The plaintext generated from a ciphertext using the session key.
	void DefDecryptBlock(char const* in, char* result);

public:
	//Encrypt exactly one block of plaintext.
	// in           - The plaintext.
    // result       - The ciphertext generated from a plaintext using the key.
    void EncryptBlock(char const* in, char* result);
	
	//Decrypt exactly one block of ciphertext.
	// in         - The ciphertext.
	// result     - The plaintext generated from a ciphertext using the session key.
	void DecryptBlock(char const* in, char* result);

	void Encrypt(char const* in, char* result, size_t n, int iMode=ECB);
	
	void Decrypt(char const* in, char* result, size_t n, int iMode=ECB);

	//Get Key Length
	int GetKeyLength()
	{
		if(false==m_bKeyInit)
			throw exception(sm_szErrorMsg1);
		return m_keylength;
	}

	//Block Size
	int	GetBlockSize()
	{
		if(false==m_bKeyInit)
			throw exception(sm_szErrorMsg1);
		return m_blockSize;
	}
	
	//Number of Rounds
	int GetRounds()
	{
		if(false==m_bKeyInit)
			throw exception(sm_szErrorMsg1);
		return m_iROUNDS;
	}

	void ResetChain()
	{
		memcpy(m_chain, m_chain0, m_blockSize);
	}

public:
	//Null chain
	static char const* sm_chain0;

private:
	static const int sm_alog[256];
	static const int sm_log[256];
	static const char sm_S[256];
    static const char sm_Si[256];
    static const int sm_T1[256];
    static const int sm_T2[256];
    static const int sm_T3[256];
    static const int sm_T4[256];
    static const int sm_T5[256];
    static const int sm_T6[256];
    static const int sm_T7[256];
    static const int sm_T8[256];
    static const int sm_U1[256];
    static const int sm_U2[256];
    static const int sm_U3[256];
    static const int sm_U4[256];
    static const char sm_rcon[30];
    static const int sm_shifts[3][4][2];
	//Error Messages
	static char const* sm_szErrorMsg1;
	static char const* sm_szErrorMsg2;
	//Key Initialization Flag
	bool m_bKeyInit;
	//Encryption (m_Ke) round key
	int m_Ke[MAX_ROUNDS+1][MAX_BC];
	//Decryption (m_Kd) round key
    int m_Kd[MAX_ROUNDS+1][MAX_BC];
	//Key Length
	int m_keylength;
	//Block Size
	int	m_blockSize;
	//Number of Rounds
	int m_iROUNDS;
	//Chain Block
	char m_chain0[MAX_BLOCK_SIZE];
	char m_chain[MAX_BLOCK_SIZE];
	//Auxiliary private use buffers
	int tk[MAX_KC];
	int a[MAX_BC];
	int t[MAX_BC];
};

#endif // __RIJNDAEL_H__

Using Visual Studio the exception class has a constructor overload with a C string as it's parameter. G++/GCC do NOT have that overload.

So blame Microsoft's compiler, then convert your exceptions to std::string.

also this is absolutely WRONG!

void main()


Use instead

int main()

Please read here, on the creator of C++'s website about "void main()"
http://www2.research.att.com/~bs/bs_faq2.html#void-main

I need it for c++ code ... no way to edit the code to make it work ? yeah ...after editing the int main() the exception still exist

Using Visual Studio the exception class has a constructor overload with a C string as it's parameter. G++/GCC do NOT have that overload.

So blame Microsoft's compiler, then convert your exceptions to std::string.

The std::exception doesn't have the overload that has a const char * as a parameter on GCC/G++ compiler.

So if you want your exception throwing code to compile you will have to convert from the std::exception type to std::string or something similar.

Please, please, read what I've written.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.