strcat with a string containing null in the middle

R

rtillmore

Hi,

I have run into a small snag with a program I am writing. I am
generating a string using openssl DES_ecb_encrypt. The string is
properly generated and contains the correct value. The problem is
that one of the generated strings contains null in the middle so when
I use strcat to append the generated string to the rest of the key I
am losing the bytes after the NULL. Here is my code:

for (i=0, j=0; i<=64; i+=8) {
strncpy((char*)enc_block,(char*)&encrypteddata, 8);
strncpy((char*)block_key,(char*)&seckey[j], 7);

str_to_key(block_key,des_key);

DES_set_key_checked((des_cblock *)des_key, &ks1);
DES_ecb_encrypt((des_cblock *)enc_block, (des_cblock *)temphold,
&ks1, DES_DECRYPT);

j += 7;
if (strlen((char*)&seckey[j]) <7) {
j = strlen((char*)&seckey[j]);
printf("j = %d\n",j);
}
strcat((char*)output1,(char*)temphold);
}

The key causing the problem is when temphold = b0 71 ba d5 00 93 9a b7
(I can't paste the char string). So when the program gets to the
strcat b0 71 ba d5 is copied and the rest is dropped causing problems
down the line. How can I work around this?

Thanks,
 
V

viza

The string is properly generated and contains the correct value. The
problem is that one of the generated strings contains null in the
middle so when I use> strcat to append the generated string to the rest
of the key I am losing the bytes after the NULL.

"After the null character". NULL in capitals means something else.

use memcpy().
 
K

Keith Thompson

I have run into a small snag with a program I am writing. I am
generating a string using openssl DES_ecb_encrypt. The string is
properly generated and contains the correct value. The problem is
that one of the generated strings contains null in the middle so when
I use strcat to append the generated string to the rest of the key I
am losing the bytes after the NULL. Here is my code:
[snip]

A string is, by definition, "a contiguous sequence of characters
terminated by and including the first null character". A string
cannot have a null character in the middle.

A byte sequence can have a null character in the middle. Since you're
dealing with byte sequences rather than strings, you need to use
memcpy rather than any of the str* functions.
 
R

rtillmore

"After the null character".  NULL in capitals means something else.

use memcpy().

Sorry about the null confusion. memcpy won't work as it overwrites
the previous contents. Unfortunately there is no memcat. But I think
I could do something like this:
memcpy(output1 + i, temphold, sizeof temphold);

I will try it later, probably tomorrow.

Thanks,
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top