How to use unsigned char data type in this class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Header file for the C++ ICE encryption class.
 *
 * Written by Matthew Kwan - July 1996
 */

#ifndef _IceKey_H
#define _IceKey_H

class IceSubkey;

class IceKey {
    public:
	IceKey (int n);
	~IceKey ();

	void		set (const unsigned char *key);

	void		encrypt (const unsigned char *plaintext,
					unsigned char *ciphertext) const;

	void		decrypt (const unsigned char *ciphertext,
					unsigned char *plaintext) const;

	int		keySize () const;

	int		blockSize () const;

    private:
	void		scheduleBuild (unsigned short *k, int n,
							const int *keyrot);

	int		_size;
	int		_rounds;
	IceSubkey	*_keysched;
};

#endif


First time handling unsigned char*, may i know is there anyway to convert a string type to a unsigned char. strcpy dont seem to work and c_str() can only convert to a const char* datatype.

Last edited on
what will you be using the char* variable for? Are you sure it wouldn't be ok for it to be const? If you are absolutely sure that it cannot be const then you can't use c_str(without casting away the constness, which is bad). What do you mean exactly when you say strcpy doesn't seem to work?
Last edited on
Hi quirky,

Above class is predefine by someone with all the encryption and decryption algorithm. I was trying to make use of his class to work on my prj. I found it quite hard to make use of usigned char. What

unsigned char* plaintext;
string pt;

i mean about strcpy(plaintext,pt);
This is for char but not for unsigned type. As i read up in some forum about making use of this to assign to unsigned char and it just totally wont work. As i believe strcpy is for char ?
I don't think there are any keyboard characters which hold negative values... are there? If there isn't then I can't see why this would be a problem.

I generally only use unsigned char for storing a numeric value < 2^9 - I use unsigned char instead of an int or short int so I don't waste any space.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <cstring>


int main(){

    std::string pt ("Testing string copy");
    unsigned char* plaintext = new unsigned char [pt.size() + 1];

    std::strcpy ( reinterpret_cast<char*>(plaintext), pt.c_str() );

    std::cout << plaintext << std::endl;

    delete[] plaintext;
}



I could easily be missing something here, let me know!
Last edited on
I got a problem now ... Because i want to encrypt some data and the these data are store inside a string variable ...



string cipherText;
char iobuf[LENGTH];

strcpy(iobuf, cipherText);

However, not all single char store inside the cipherText can be store inside iobuf ... Is it char problem that cant store some encrypted data ? Or is there any method to store.

The purpose of char array is because i doing a socket programming and these encrypted data need to be send over using write function.

write function:
write(int s, const void *msg, int len);

And also i did trying using cipherText.data() to replace the iobuf. Some chars will just goes missing just like the what happen in iobuf


Shouldn't in be:

1
2
3
4
5
6
7
string cipherText;
 
cipherText = "some string.";

char iobuf [cipherText.length() + 1]; //Add one to allow space for c-string termination character.

strcpy(iobuf, cipherText.c_str());


By the sounds of it your LENGTH variable is incorrect?

Post some more code - & please use the code tags!

Nick.


Last edited on
Sorry about the code tag

This is a server side code, i was waiting for the client to keep sending request until the last request coming in.

Then i will do some encryption use crypto++ lib aes algorithm. I test out all the encryption and decryption not problem with any of those function. The main problem is when i convert from string to a char* some of the character will be truncates and therefore sending a wrong ciphertext to the client side which require them to decrypt at the end. Is there any better solution to assign all the characters inside string to the char array

1. using strcpy - Wont work
2. using string.data() and send over to the socket - wont work

both method cause the truncation, i try print the string, iobuf (using strcpy) and string.data(), only string will give me the extract ciphertext but truncation occur in both method 1 and 2 i mention previously. And this print out is before the write(), i not too sure whether i can assume the problem is with char data type can support all the encrypted data.

Is a screenshot of my print out
see the ciphertext at the bottom

http://img251.imageshack.us/i/screenshotuserubuntudes.png/

 
#define	LENGTH	10000 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
		/*
		Receving from protocol 1
		request vector
		0. Car
		1. AS
		2. TS
		3. Encryption
		4. Encryption + hash
		*/
		while(rc = read(s, iobuf, sizeof (iobuf)))
		{
			if (rc == -1)
			{
				perror("read");
				close(s);
				pthread_exit(0);
			}

			store_request.assign(iobuf);//convert from char array to string
			if(firstEntry){
				numOfRequest = atoi(store_request.c_str());//get number of request coming in
				firstEntry = false;
			}else{// keep adding the request coming in from client
				request.push_back(store_request);//add all incoming request to vector
				count++;
				
			
				memset(&iobuf, 0, sizeof(iobuf));
			
				if(count == numOfRequest){//finish all the communication
					cout << "Protocol 1: C -> AS" << endl;
					for(int i = 0; i < request.size(); i++)
						cout << i + 1 << " : " << request[i] << endl;
	
					carExist = checkCar(request[0], keyCAS, CBInfro, CBInfroArraySize);
					if(carExist){//if car exist
						CASShareKey = getShareKey(request[0], keyCAS);
						if(intergrityCheck(request, branchNum, CASShareKey) && isOneMinute(request[2]) && checkBranchExist(CBInfro, request[0], branchNum, CBInfroArraySize)){
							store_response = "AS: Client successful authenicate.";
							randomShareKey = genShareKeyCV();
							TS2= getTimeStamp();
							ASVShareKey = getKeyASV(keyASV, atoi(branchNum.c_str()));
							ticket = getTicket(ASVShareKey, randomShareKey, request[0], branchNum, TS2);	
							encrypt_data = 	ASCInfoEncryption(CASShareKey, randomShareKey, request, branchNum, TS2);	
							
												
							//response push_back						
							response.push_back(request[1]);
							response.push_back(branchNum);
							response.push_back(encrypt_data);	
							response.push_back(ticket);
						

							
						}else{//car exist but during integrity checking encounter error
							cout << "[Message] Error found during authentication check\n";							
							store_response = "Error";
							response.push_back(store_response);
						}
					  
					}else{//car does not exist
						cout << "[Message] Car does not exist \n";
						store_response = "Error";
						response.push_back(store_response);
					}//end else
					
					
					
					/*Protocol 2
					AS -> C
					response vector
					0. AS
					1. Branch
					2. Encryption 
					3. Ticket
					*/
					cout << "Protocol 2: AS -> C "<< endl;
					for(int i = 0; i < response.size();i++){
						
						
						
						memset(&iobuf, 0, sizeof(iobuf));//clear buffer	
						strcpy(iobuf, response[i].c_str());
						
						rc = write(s, iobuf, sizeof(iobuf));
						cout << i + 1<< " :" << response[i] << endl;
						cout << "  :" << iobuf << endl;
						if (rc == -1)
						{
							perror("write");
							close(s);
							pthread_exit(0);							
						}//end if
						memset(&iobuf, 0, sizeof(iobuf));//clear buffer	

						rc = read(s, iobuf, sizeof(iobuf));							
						if (rc == -1)
						{
							perror("read");
							close(s);
							pthread_exit(0);
						}

						if(i == response.size()-1){
							store_response="Completed";
							memset(&iobuf, 0, sizeof(iobuf));//clear buffer	
							strcpy(iobuf, store_response.c_str());
							rc = write(s, iobuf, sizeof(iobuf));
						
							if (rc == -1)
							{
								perror("write");
								close(s);
								pthread_exit(0);							
							}//end if
						}
					}//end of for loop
					
		


				}//end of if
			}//end of else
				
			
		}//end of inner while 
Need to see declaration of iobuf.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
void* readMessage(void *sid)
{
	int    s = *((int*)sid);
	int    rc, index;
	char   iobuf[LENGTH];
	bool flag, carExist, firstEntry;


	string store_request, store_response, branchNum, randomShareKey, TS1, TS2, ticket, encrypt_data, CASShareKey, ASVShareKey;
	vector<string> request, response;

	flag = true, carExist = false, firstEntry = true;



	printf("\ns=%d, I am going to read from client\n", s);
	int count = 0; // remove later
	int numOfRequest = 0;

		
		/*
		Receving from protocol 1
		request vector
		0. Car
		1. AS
		2. TS
		3. Encryption
		4. Encryption + hash
		*/
		while(rc = read(s, iobuf, sizeof (iobuf)))
		{
			if (rc == -1)
			{
				perror("read");
				close(s);
				pthread_exit(0);
			}

			store_request.assign(iobuf);//convert from char array to string
			if(firstEntry){
				numOfRequest = atoi(store_request.c_str());//get number of request coming in
				firstEntry = false;
			}else{// keep adding the request coming in from client
				request.push_back(store_request);//add all incoming request to vector
				count++;
				
			
				memset(&iobuf, 0, sizeof(iobuf));
			
				if(count == numOfRequest){//finish all the communication
					cout << "Protocol 1: C -> AS" << endl;
					for(int i = 0; i < request.size(); i++)
						cout << i + 1 << " : " << request[i] << endl;
	
					carExist = checkCar(request[0], keyCAS, CBInfro, CBInfroArraySize);
					if(carExist){//if car exist
						CASShareKey = getShareKey(request[0], keyCAS);
						if(intergrityCheck(request, branchNum, CASShareKey) && isOneMinute(request[2]) && checkBranchExist(CBInfro, request[0], branchNum, CBInfroArraySize)){
							store_response = "AS: Client successful authenicate.";
							randomShareKey = genShareKeyCV();
							TS2= getTimeStamp();
							ASVShareKey = getKeyASV(keyASV, atoi(branchNum.c_str()));
							ticket = getTicket(ASVShareKey, randomShareKey, request[0], branchNum, TS2);	
							encrypt_data = 	ASCInfoEncryption(CASShareKey, randomShareKey, request, branchNum, TS2);	
							
												
							//response push_back						
							response.push_back(request[1]);
							response.push_back(branchNum);
							response.push_back(encrypt_data);	
							response.push_back(ticket);
						

							
						}else{//car exist but during integrity checking encounter error
							cout << "[Message] Error found during authentication check\n";							
							store_response = "Error";
							response.push_back(store_response);
						}
					  
					}else{//car does not exist
						cout << "[Message] Car does not exist \n";
						store_response = "Error";
						response.push_back(store_response);
					}//end else
					
					
					
					/*Protocol 2
					AS -> C
					response vector
					0. AS
					1. Branch
					2. Encryption 
					3. Ticket
					*/
					cout << "Protocol 2: AS -> C "<< endl;
					for(int i = 0; i < response.size();i++){
						
						
						
						memset(&iobuf, 0, sizeof(iobuf));//clear buffer	
						strcpy(iobuf, response[i].c_str());
						
						rc = write(s, iobuf, sizeof(iobuf));
						cout << i + 1<< " :" << response[i] << endl;
						cout << "  :" << iobuf << endl;
						if (rc == -1)
						{
							perror("write");
							close(s);
							pthread_exit(0);							
						}//end if
						memset(&iobuf, 0, sizeof(iobuf));//clear buffer	

						rc = read(s, iobuf, sizeof(iobuf));							
						if (rc == -1)
						{
							perror("read");
							close(s);
							pthread_exit(0);
						}

						if(i == response.size()-1){
							store_response="Completed";
							memset(&iobuf, 0, sizeof(iobuf));//clear buffer	
							strcpy(iobuf, store_response.c_str());
							rc = write(s, iobuf, sizeof(iobuf));
						
							if (rc == -1)
							{
								perror("write");
								close(s);
								pthread_exit(0);							
							}//end if
						}
					}//end of for loop
					
		


				}//end of if
			}//end of else
				
			
		}//end of inner while

	
	

	
	close(s);
	printf("\nThread finished\n");
	pthread_exit(0);
        return 0;
}



Ok. Firstly I'm pretty sure your memset calls will not work. I think this is how you should be calling memset:

 
memset(iobuf, 0, sizeof(char)*LENGTH);//clear buffer	 


sizeof(iobuf) will probably return 4... iobuf is a pointer to the first char of the array. I could be wrong, but check it out. Output sizeof(iobuf). This is probably the reason your read and write functions are stuffing up.

Secondly note the first argument is iobuf, not &iobuf.

Also after line 103 go:

1
2
std::cout << "iobuf: " << iobuf << std::endl;
std::cout << "response[i]: " << response[i] << std::endl;


I'm betting they are the same.

Anyway I may have missed something, and I could be completely wrong. Let me know how that goes.
It still get cut off pretty often and i realize a pattern in the truncation. Which is the front characters remain intact ... 2 possibilities i can think of?

1. Something stuck inside the stream buffer or somewhere that restrict it for taking in more characters.

2. Or character just cant take in that special character. (which i think is unlikely)

i try loop the string and assign it by accessing each index of the string to the iobuf and it just couldn't work.

I remember when i was learning java my lecturer told me that i need to flush everytime we read or write. Is there any function to flush off the iobuf or stream buffer or other stuffs ?

does memset do the reset for the iobuf ? It look to me it just assign 0s to it ?
Last edited on
Post the edited code (with the changes I asked you to make).
Hi nick,

Thanks for being so active answering my query, to make thing simple i create another short function without involve the socket sending to see where is my mistake. I realise the problem really is because char cannot store special characters.

1. print string -> look fine
2. print string.data() -> truncates occur
3. print string.c_str() -> truncates occur
4. send to output file and assign to character -> truncates occur

Look like these confirm the problem is with my character array ?

is there anyway to send my data through the socket using string datatype ... character array cant be dependable after all ... I cant believe is with the complexity with the ciphertext otherwise i have to change my algorithm.

output file image: http://img840.imageshack.us/i/screenshotuserubuntudes.png/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
void prepareCAS(const string key, const string car, const string branch, vector<string>response){

    string str, TS1, ciperText, plainText, hashValue;
    char iobuf[512];

    cout << "What is size of : " << sizeof(iobuf) << endl;
    memset(iobuf, 0, sizeof(iobuf));
    

    TS1 = getTimeStamp();

    response.push_back(car);
    response.push_back("AS");
    response.push_back(TS1);

    plainText = TS1 + "," + branch;
    ciperText = encryption(plainText, key);
    response.push_back(ciperText);
    cout << ciperText << endl;
    cout << ciperText.data() << endl;
    cout << ciperText.c_str() << endl;

    char ptr;
    fstream outFile("outfile2.dat", ios::out);
    for(int i = 0; i < ciperText.length();i++)
        outFile << ciperText[i];
    outFile.close();

    cout << "\nAfter reading from file :" << endl;
    int i = 0;
    fstream inFile("outfile2.dat", ios::in);
    while(!inFile.eof()){
        inFile >> iobuf[i];
        i++;

    }
    inFile.close();
    cout << iobuf << endl << endl;

    memset(iobuf, 0, sizeof(iobuf));

    plainText = car + "," + "AS" + "," + TS1 + "," + branch;
    hashValue = getHashValue(hashFileName, plainText);

    ciperText = encryption(hashValue, key);
    fstream outFile2("outfile3.dat", ios::out);
    for(int i = 0; i < ciperText.length();i++)
        outFile2 << ciperText[i];
    outFile.close();



    response.push_back(ciperText);
    cout << ciperText << endl;
    cout << ciperText.data() << endl;
    cout << ciperText.c_str() << endl;
    cout << "\nAfter reading from file :" << endl;
    i = 0;
    fstream inFile2("outfile3.dat", ios::in);
    while(!inFile2.eof()){
        inFile2 >> iobuf[i];
        i++;

    }
    inFile2.close();
    cout << iobuf << endl << endl;





}
Last edited on
The problem is char = 0.... c strings view this as the termination character. There are also some other worrying chars, for example char = 12 or 13. Run this program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>


int main(){

    for(unsigned int i = 0; i < 256; i++){
        std::string test;
        test.push_back(i);
        test.append("T");
        std::cout << "i = " << i << " " << test;
        std::cout << " " << test.c_str() << std::endl;
    }

    std::string test;
    test = "About to append char 0...";
    char zero = 0;
    test.push_back(zero);
    test.append(" this is some stuff after appending char 0.");

    std::cout << test << std::endl;
    std::cout << test.c_str() << std::endl;

    for(unsigned int i = 0; i < test.size(); i++){

        if(test[i] == 0){
            test[i] = -1;
        }

    }

    std::cout << test.c_str() << std::endl;

    return 0;

}




I'm not really sure what you should do about it. Maybe edit your encrypt function or do what I've done above - before using strcpy (See I change all char == 0 to char = -1).
Last edited on
Look like the only way is either i change my algorithm =( It kind of wasted after spending a week on this project and realise this problem. But the better side is i learn something new. Anyway i found one aes coding and was asking around aes exception error. Are you able help me debug it ? Some guy in a forum say the problem happen due to compiler issues. Want to get more feedback.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//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;

	}

}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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’


I know the last error i simply need to switch void main to int main but i try working on exception and nothing seen to be working for me
Last edited on
Sorry ... have to post in 2 thread otherwise it show me error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
[code]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__ 
[/code]
Topic archived. No new replies allowed.