3

For a school project, I'm writing a blowfish encryption (just the encryption, not the decryption). I've finished the encryption itself, and I decided I would do the decryption for fun (it's easy enough in Blowfish).

I used unsigned chars to represent bytes (although I suppose uint8_t would have been more portable). The question I have comes in when I am attempting to print out the decrypted bytes. I am encrypting plain text messages. I've been able to print out the actual text message that I encrypted, but only at a very specific spot. The same exact code seems not to work anywhere else. Here it is:

int n;
for(n = 0; n < numBlocks; n++) // Blocks are 32-bit unsigned ints (unsigned long)
{
     // uchar is just a typedef for unsigned char
     // message is an array of uchars
     message[n] = (uchar) ((blocks[n]>>24));
     message[n+1] = (uchar) ((blocks[n]>>16));
     message[n+2] = (uchar) ((blocks[n]>>8));
     message[n+3] = (uchar) ((blocks[n]));
     // Printing works here; exact message comes back
     printf("%c%c%c%c", message[n], message[n+1], message[n+2], message[n+3]); 
}

But when I try to use the exact same code two lines later, it doesn't work.

for(n = 0; n < numBlocks; n++)
{
     // Printing doesn't work here.
     // Actually, the first letter works, but none of the others
     printf("%c%c%c%c", message[n], message[n+1], message[n+2], message[n+3]); 
}

I have tried printing out the characters in number format as well, and I can see that they have in fact changed.

What exactly is going on here? Is this undefined behavior? Does anyone have any reliable solutions? I'm not doing anything to change the value of the message array in between the two calls.

I'm running and compiling this on Sun 5.10 with a sparc processor.

TPXL
  • 327
  • 1
  • 3
  • 12
  • 2
    If the byte values don't correspond to human-readable characters, then they're not going to display meaningfully if you use `%c`. – Oliver Charlesworth Jul 06 '12 at 17:28
  • 1
    So, what do you get when you `printf("%d %d %d %d\n", ...)` ? – Dietrich Epp Jul 06 '12 at 17:28
  • how your array contents of "message" changes? have you tried running it under gdb(or xdb, as sun compilers comes with xdb i guess) and then examine the memory blocks pointed by messages? – Aftnix Jul 06 '12 at 17:29
  • 116 101 115 116 10 0 0 0 is what I get inside the first loop. (the string is "test\n") – TPXL Jul 06 '12 at 17:35
  • BTW, if it is defined `uint8_t` is the same as `unsigned char`. Print `uint8_t` with format macros `PRIu8` or `PRIx8` and `unsigned char` with format string `%hhu` or `%hhx`. – Jens Gustedt Jul 06 '12 at 22:14

1 Answers1

9
for(n = 0; n < numBlocks; n++) // Blocks are 32-bit unsigned ints (unsigned long)
{
     message[n] = (uchar) ((blocks[n]>>24));
     message[n+1] = (uchar) ((blocks[n]>>16));
     message[n+2] = (uchar) ((blocks[n]>>8));
     message[n+3] = (uchar) ((blocks[n]));
}

Every time you go through this loop, you set message[n] to message[n+3], then increment n by 1. This means that your first iteration sets message[0], message[1], message[2] and message[3], then your second sets message[1], message[2], message[3] and message[4]. So basically, you overwrite all but the first char in your message on every iteration.

Most likely you need to make message 4x larger and then do:

message[n*4] = ...
message[n*4 + 1] = ...
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • 2
    @TragicPixel Don't feel bad, I think everyone's experienced this. Sometimes you just need an outsider to look at your code from a new perspective to find bugs. – Brendan Long Jul 06 '12 at 17:39