Bit of a unicode ish

G

Guest

Hi
Having a bit of an ish with unicode.
Basically, I've discovered an encryption library on planetsourcecode that
does exactly what I want - it encrypts a string using a fairly (more than
just XOR) sophisticated method, using the functions in advapi32.lib, and can
decrypt it to get the same string back. It's not some unix hybrid that only
works with files or anything, so it seems good. And I tested it, and it does
seem to get the right string back.

However, I'm having a bit of an ish using printf on the returned value (the
encrypted data).
Does the console have an ish with printing garbage characters on the screen?
The code was something like this:
_TCHAR* origdata = "the data";
_TCHAR encrypted[255];
_TCHAR returndata[255];
printf(_T("The original data is \"%s\"\n"), origdata);
DoTheEncryption(origdata, encrypted, ...);
printf(_T("The encrypted data is \"%s\"\n"), encrypted);
DoTheDecryption(encrypted, returndata, ...);
printf(_T("The decrypted data is \"%s\"\n"), decyrpted);

And the output is something like
The original data is "the data"
The encrypted data is "Öûê┼!]The decyrpted data is "the data"

What's baffling is no matter what it thinks of my encrypted string - it
doesn't even respect my linefeed.

Any ideas?
I looked in the memory editor and the unicode string just looks like one
byte random, one byte zero, one byte random, one byte zero, etc...
 
S

Simon Trew

Why are you using printf with Unicode? You need to use wprintf, or the
generic text-mapping routine _tprintf which maps to printf in non-Unicode
builds and wprintf in Unicode builds.
You'd also need to put your string literal of origdata inside a _T()
declaration, but I guess this was just an example and not in the real code.
So something like:

_TCHAR* origdata = _T("the data");
_TCHAR encrypted[255];
_TCHAR returndata[255];
_tprintf(_T("The original data is \"%s\"\n"), origdata);
DoTheEncryption(origdata, encrypted, ...);
_tprintf(_T("The encrypted data is \"%s\"\n"), encrypted);
DoTheDecryption(encrypted, returndata, ...);
_tprintf(_T("The decrypted data is \"%s\"\n"), decyrpted);
 
M

Mihai N.

Any ideas?
I looked in the memory editor and the unicode string just looks like one
byte random, one byte zero, one byte random, one byte zero, etc...
There is no guarantee that encrypted data is printable.
You can have control characters, anything.
The custom is to dump it hex (for screen, email, etc.) or to write it a
file opened in binary mode.
 
G

Guest

Brilliant, that's what I hoped.
I wanted to make sure somebody else thought the same thing to make sure I'm
not going *completely* insane.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top