Click here to Skip to main content
13,055,798 members (50,434 online)
Rate this:
 
Please Sign up or sign in to vote.
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 23-Feb-14 5:15am
raniam548
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov 23-Feb-14 12:26pm
   
Agree, my 5, but see also my comment to the question.
—SA
CPallini 23-Feb-14 12:44pm
   
5.
And as long the other side knows the actual length of the plain data. :-)
«_Superman_» 24-Feb-14 1:42am
   
Absolutely.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

/*
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);
} 
 
  Permalink  
v2
Comments
phil.o 26-Feb-14 9:26am
   
Wow, what a strong encryption! ^^

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Feb 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100