News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

C++ help

Started by Seb, December 29, 2005, 02:06:33 AM

Previous topic - Next topic

Seb

Hello!

I'm developing a client-server side application in C++ with parts written in Assembly. I've got a MESSAGE structure which is used in the communication and I've added a simple XOR procedure to code the structure before sending it to the server (or the other way round). Now, the question is, how do I encrypt a structure and then decrypt it to get the structure back exactly like the client constructed it (ie. same data in the members etc).

I've already written some code myself, but I wonder if it's the right way to do it before I move on. If it's not possible using the "method" I've used below, please provide any idea of how to do it instead.


BOOL Communication::sendCommand(MESSAGE *msg)
{
BYTE *b=(BYTE*)msg;
Crypt(b,sizeof(*msg),KEY,sizeof(KEY)); // encrypt the structure
if (s->sendMessage((char*)b)==-1)
return FALSE;
return TRUE;
}

MESSAGE *Communication::getCommand()
{
char msg[100];
int ret=s->recieveMessage((char*)msg);
if (ret<=0)
return NULL;
MESSAGE m=*(MESSAGE*)msg;
BYTE *b=(BYTE*)&m;
Crypt(b,sizeof(m),KEY,sizeof(KEY)); // maybe i should use "ret" instead of "sizeof(m)" since "ret" contains the number of bytes received
return (MESSAGE*)b;
}


Thanks in advance!

Regards,
Seb

hutch--

Seb,

I would be inclined to have a look at the CRYPTO API stuff as it probably powerful enough to give you reasonably good encryption without having to write your own.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Seb

Heya Hutch!

Well, I just want a basic encryption with a key (like in my code), but the thing I don't understand is how to encrypt a structure or something binary, eg. an image. :wink

MusicalMike

While we are on the subject of encryption, I actually did write one of my own.

Devide the length of the byte stream by the key and store the first byte of the result and xor that value with each byte in the stream. The fact that the value that is used in the encryption is dirrectly dependent not only on a key provided by the user but also the length of the data being encrypted is simple yet in my oppinion effective. Just remember to be careful using this on data structures that contain pointers unless you actually enjoy code that produces GPFs.

Heres an example that uses a simple data structure and some nice pointer tricks to allow access to each individual byte that makes up the structure. When you run it, it tells you step by step what its doing so you can familiorize yourself with it. Feel free to add more entries to the structure and see that no matter what, the thing still works. This algorithm also works great on c-style strings.


#include <iostream>
#include <tchar.h>
#include <windows.h>

using namespace std;

struct myDataStruct
{
int i;
char c;
float f;
};

int main()
{
cout << "Encryption Demo" << endl;

BYTE xorit;
int length;
int key;

myDataStruct * mdt = new myDataStruct;

length = sizeof(myDataStruct);

cout << "Enter a key: ";

cin >> key;

cout << endl;
cout << "Lets fill out a hypothetical data structure" << endl;
cout << "which we will encrypt" << endl;
cout << endl;

cout << "Enter an int value: ";
cin >> mdt->i;

cout << "Enter a char: ";
cin >> mdt->c;

cout << "Enter a float: ";
cin >> mdt->f;

cout << endl;
cout << "myDataStruct{" << mdt->i << ',' << mdt->c << ',' << mdt->f << "};" << endl;
cout << "Size of data = " << sizeof(myDataStruct) << endl;
cout << endl;

Sleep(5000);

cout << "We will now mod the key by the length of the file" << endl;
cout << "and xor each byte in the structure with that number." << endl;
cout << endl;

xorit = (BYTE)(key % length);

cout << "key (" << key << ") % length (" << length << ") = " << (int)xorit << endl;

BYTE * c = (BYTE *)mdt;
mdt = (myDataStruct *)c;

Sleep(5000);

cout << endl;
cout << "Printing contents of structure before encryption:" << endl;
cout << mdt->i << endl;
cout << mdt->c << endl;
cout << mdt->f << endl;
cout << endl;

Sleep(5000);

cout << "XORing data . . . " << endl;
for (int i = 0; i < length; i++) c[i] ^= xorit;

Sleep(5000);

cout << endl;
cout << "Printing contents of structure after encryption:" << endl;
cout << mdt->i << endl;
cout << mdt->c << endl;
cout << mdt->f << endl;
cout << endl;

Sleep(5000);

cout << "XORing data . . .  " << endl;
for (int i = 0; i < length; i++) c[i] ^= xorit;

Sleep(5000);

cout << endl;
cout << "Printing contents of structure after encryption:" << endl;
cout << mdt->i << endl;
cout << mdt->c << endl;
cout << mdt->f << endl;
cout << endl;

Sleep(5000);

cout << endl;
cout << "This has been an encryption demo by Michael S. Miller." << endl;

Sleep(5000);

return 0;
}


Ps. This is an example of whats called XOR encryption. Cryptography is an art form that stresses creativity and forsight. The above example is actually rather trivial, but its a good place to start.

hutch--

Guys,

We do allow some C/C++ where it relates to assembler programming but for general purpose C/C++ it is probably better targetted at the C/C++ subforums and it keeps this stuff out of the assembler subforums.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tedd

Just treat the structure as a block of bytes starting at the same address, and being SIZEOF bytes long. You then just have an array of bytes to be encrypted and sent.
Receiving it at the other end, you have to know what structure it started out as (in most cases you should know anyway) to be able to put the 'array' back and treat it as the structure again.

If you won't know what the structure is in advance, then you'll need some kind of serialization/marshalling which will attach extra data which describes the structure, which can then be unpacked again at the other end. There are C++ classes to do all of this for you.
No snowflake in an avalanche feels responsible.

rwalt

I thought all of you hated C++

MusicalMike

Nope, I love c++, what gave you that impression?

rwalt

Well, not everybody, but I do remember Hutch calling it puke.

MusicalMike

I don't understand how hutch can make a carreer out of programming in assembly language and hate c++.  :dazzled:

zooba

Quote from: MusicalMike on January 11, 2006, 05:34:56 PM
I don't understand how hutch can make a carreer out of programming in assembly language and hate c++.  :dazzled:

I think it's more of a hobby rather than a career. :U

Tedd

Quote from: MusicalMike on January 11, 2006, 05:34:56 PM
I don't understand how hutch can make a carreer out of programming in assembly language and hate c++.  :dazzled:

He used to love C++ (he even proposed), but she left him for .NET and it's been sour grapes ever since :boohoo:
So now he tries to pretend he's over it by being with PowerBasic. Unfortunately, PowerBasic is the type of girl who doesn't mind going with anyone.
No snowflake in an avalanche feels responsible.

MusicalMike

.NET is not a bad technology except for the fact that it was designed only for applications to be used internally at a coorperation. It is not supposed to be used for generalized programming, ie games, comercial apps, tools, etc. Too many people don't get this, they use .NET for something it was not designed, and ofcourse it doesn't perform up to par and it looks bad. If you want something that truely was bad, try MFC. If he doesn't like .NET, VC still includes the windows api headers necessary for windows applications, and allows you to do native compiles. Actually, I personally prefere the borland c++ command line compiler. It has better compilance with ISO standards. But, since he is currently basing his carreer on something that was at one time owned by microsoft, I believe that will cause him some what of a moral delema if he ever looks at or considers using Borland products.