Question

So I'm trying to use cbc_crypt to try encrypting and decrypting different strings. Here is my program so far.

int main(int argc, char *argv[])
{
  unsigned char key[8] = {0x00, 0x00, 0x00, 0x68, 0x67, 0x01, 0x75, 0x19};
  unsigned char iv[8] = {0xe0, 0x3d, 0x36, 0x0c, 0x55, 0xfc, 0xe4, 0x0f};
  unsigned char ciphertext[96] = {0xb2, 0xc0, 0x11, 0xd6, 0x58, 0xce, 0x4b, 0x3\
c, 0xa6, 0x05, 0x93, 0x4d, 0x4f, 0x4c, 0x80, 0x19, 0x3f, 0xb5, 0xa8, 0xd6, 0x03\
, 0xec, 0xf9, 0xbc, 0xb0, 0xea, 0x36, 0xe9, 0x8f, 0x3d, 0x94, 0x95, 0x02, 0xa8,\
 0xc0, 0x48, 0x23, 0xba, 0x51, 0xab, 0x65, 0x7f, 0xc2, 0xb8, 0xff, 0xfc, 0x3e, \
0x40, 0xdd, 0xfc, 0xd3, 0xe9, 0x32, 0x43, 0xd2, 0xf2, 0x37, 0x47, 0xab, 0x0d, 0\
x30, 0xba, 0xd0, 0x9e, 0x88, 0x66, 0x2b, 0x22, 0xdf, 0xed, 0x06, 0x76, 0xdb, 0x\
3a, 0xd5, 0x79, 0x7e, 0x22, 0x28, 0xcf, 0x3c, 0x5e, 0xbb, 0xc5, 0x85, 0xa0, 0x3\
f, 0x21, 0xab, 0xa8, 0xbe, 0x74, 0x37, 0xae, 0x59, 0xe9};



  unsigned char short_cipher[8] = {0xb2, 0xc0, 0x11, 0xd6, 0x58, 0xce, 0x4b, 0x\
3c};

  unsigned char text[8] = {0x12, 0x34, 0x56, 0xab, 0xba, 0xfe, 0xfe, 0xfe};

  printf("Text before is %u \n", text);
  // cbc_crypt(key, text, 8, 1, iv);                                            
  printf("Text after is %u \n", text);

  return;
}

Note that cbc_crypt is commented out. This is what happens when I run it while the cbc_crypt is commented out.

[dorset:usabledir] 247) gcc guessdes.c
[dorset:usabledir] 248) ./a.out
Text before is 73623220 
Text after is 73623220 

[dorset:usabledir] 249) ./a.out
Text before is 9cf73030 
Text after is 9cf73030 

[dorset:usabledir] 249) ./a.out
Text before is f46e1bc0 
Text after is f46e1bc0 

[dorset:usabledir] 249) ./a.out
Text before is 674ed540 
Text after is 674ed540 

I can't figure out why the text is changing each time I print. I haven't even tried to run the encryption, I just printed out the unsigned char I've initialized. Any help would be appreciated.

EDIT: So apparently I should be using %s. If I do that, it now prints consistently.

[dorset:usabledir] 256) ./a.out
Text before is 4V???????%< 
Text after is 4V???????%< 

[dorset:usabledir] 256) ./a.out
Text before is 4V???????%< 
Text after is 4V???????%< 

[dorset:usabledir] 256) ./a.out
Text before is 4V???????%< 
Text after is 4V???????%< 

Can someone explain why it was printing the addresses before? Also, is there a way to print it in hex? (I dont really understand where the 4V and all the question marks are coming from).

EDIT 2: Ok, so I'm now printing like this, as Scott suggested

  for(i = 0; i < 8; i++){
    printf("%u", text[i]);
  }

It then prints text as 185286171186254254254

I'm trying to figure out how that matches up to my original hex code, {0x12, 0x34, 0x56, 0xab, 0xba, 0xfe, 0xfe, 0xfe}.

EDIT: I realized I should be using %x instead of %u. Now its printing 123456abbafefefe correctly! Thanks guys.

Was it helpful?

Solution

Your data type is char[], so you should really be printing a string!

Use %s in the printf instead of %u.

Note that your strings are currently not null terminated. This means printf() wont know when to stop printing. Adding 0x00 at the end of each string before sending to printf() will fix this.

Why do the numbers change?

The numbers you were printing were pointers to the C-Strings you created. Upon every new run of the program, they changed because the entire program was given a different portion of memory to run inside!

Every time you run your program, it is almost guaranteed to be allocated to a different place in memory (virtual and physical). your program is not the only thing running on your computer. The memory is constantly being allocated and deallocated to keep up with all of the programs running on your computer. Because of this, your program will not necessarily be handed the same "chunk" of memory each time it is run.

OTHER TIPS

You are trying to print address of array 'text' in following statement

printf("Text before is %u \n", text);

Every time you are running the program it is being loaded to different address thats why you are getting different value each time.

If you want to print string at address text then you can use

printf("Text before is %s \n", text);

statement but note that your string is not NULL terminated you may end up with some junk data printed after 8 characters.

If you want to print the value as unsigned integer you can use

printf("Text before is %u \n", (unsigned int)*text);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top