>  > I want to decrypt variable length strings using EncDES.
>>  Here is some of the code I'm using and it is blowing up (just wrote to a
>>  memory location in the storage heap) on the TxtSetNextChar function. eStr is
>>  not NULL. TxtGetNextChar sets ch to 255. Is there something simple I'm doing
>>  wrong here or am I using the wrong functions?

The reason you're getting a fatal alert is that you're probably 
passing the address of a storage heap chunk to TxtSetNextChar, which 
then tries to write to it directly, versus using DmWrite. Nearly all 
of the Palm OS routines that write to memory, other than a few Data 
Mgr calls and the field code, assume that they're dealing with 
regular (non-write protected) memory. The way to work around this 
problem is to allocate and use a buffer with TxtSetNextChar, then 
call DmWrite to flush this out to the storage heap. But as Dave noted 
below, you don't want to be passing individual characters to EncDES.

>You probably shouldn't mix EncDES with Txt* functions. The Txt*
>functions are meant to work with internationalized character sets which
>may or may not contain multibyte characters.

Actually the Txt* functions with with all character sets, not just 
"internationalized". These are general purpose text manipulation 
routines that should always be used, unless there's a special problem 
(e.g. performance reasons, or you _know for sure_ that you're only 
dealing with 7-bit ASCII, etc)

>DES is a block cipher that encrypts blocks of bytes. Thus, your real
>goal is to encrypt a block of bytes that happens to contain a "C" style
>string.

Yes, exactly.

>Something like:
>
>UInt8* encrypt_block(UInt8* src, block_size, UInt8* key) {
>       UInt8* dest = MemPtrNew(block_size);
>       UInt8* srcp = src;
>       UInt8* destp = dest;
>       for (i = 0; i < input_size; i += 8) {
>          EncDES(srcp, key, destp, true);
>          srcp += 8;
>          destp += 8;
>       }
>       return dest;
>}
>
>There's a bug in the above code where it fails to encrypt the last
>partial block. The best workaround is to always ensure that the size of
>your input block always a multiple of 8, probably by padding it with
>NULs.
>
>Also note that StrLen will not correctly determine the size of the block
>if you have multibyte characters, so you'll have to use some other
>method to determine how many bytes a string occupies.

No, that's not right. Strlen _always_ returns back the number of 
bytes in a string, not the number of characters. To count the number 
of characters you'd have to walk the string using TxtGetNextChar.

-- Ken
-- 
Ken Krugler
TransPac Software, Inc.
<http://www.transpac.com>
+1 530-470-9200

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to