Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

Is it possible to encrypt a structure of different data types (struct) in C++, and how can this be done?

Best regards.
Rania
Posted
Comments
Sergey Alexandrovich Kryukov 23-Feb-14 12:25pm    
Just a note: such questions make no sense unless you explain the scenario: how do you want to decrypt it, how the keys are distributed. Otherwise the one who is not supposed to see the unencrypted data will be able to decrypt it...
—SA

/*
The simplest type of data structure is a linear array
and simplest encrypt a structure in C++ similar this code :
*/
#include <iostream >
using std :: cout;
using std :: endl;
void encryptArray( char [ ] ); 
void decryptArray( char * eStr );
int main( )
{
	// create a string to Array
	char string[ ] = "this is a text string !";
	cout << "Original Array is: " << string << endl;
	encryptArray( string );
	// function encrypt Array( )
	cout << "encrypt Array string is: " << string << endl;
	decryptArray( string );
	//function decrypt Array( )
	cout << "after Array string is: " << string << endl;
	std::cin.get();
	return 0;

}// end of main
//-----------------------------------------------------
//encrypt Array data
void encryptArray (char e[] ) 
{
	for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt Array
//-----------------------------------------------------
//decrypt Array data
void decryptArray( char * eStr ) {
	for( ; * eStr != '\0'; ++ eStr ) --(* eStr);
} 
 
Share this answer
 
v2
Comments
phil.o 26-Feb-14 9:26am    
Wow, what a strong encryption! ^^
For encryption, the data types do not really matter as long as you know the length of the data that you wish to encrypt.

For example, in Windows, you can use the Crypto API function CryptEncrypt[^] to encrypt data.
So if you wish to encrypt a variable called myStruct of type MyStruct having different data types, pass &myStruct as the pbData parameter and sizeof(MyStruct) as the pdwDataLen parameter.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Feb-14 12:26pm    
Agree, my 5, but see also my comment to the question.
—SA
«_Superman_» 24-Feb-14 1:42am    
Absolutely.
CPallini 23-Feb-14 12:44pm    
5.And as long the other side knows the actual length of the plain data. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900