Cryptography with C
I've been using C in university for simulation (physics) and now I'm studding a new area . I decided to start with XTEA , and using the Wiki code (XTEA - Wikipedia, the free encyclopedia) and I'm having some technical difficulties.
1 - How to properly convert a string like "teste" to a uint32_t (in python there is unicode(str, "utf-8") , but I didn't found a C equivalent)
2 - I saw that the argument is v , after converting I should passing the array in blocks of 2 right?
Or anybody knows a full example to how implementing that?
Thanks in advance
July 10th, 2014, 02:09 AM
You're missing out on an important part of XTEA: the fact that it uses the Feistel cipher. Feistel forms a larger algorithm and XTEA fills a key role in it.
1. Breaks the input into two halves
2. Each "character" in both halves is fed through some other algorithm (like XTEA) to get new "characters"
3. Those are then reassembled into the output
To answer the questions,
1. Any way you want - only the raw numbers matter. To begin with you could use byte values. More advanced would be using sets of four bytes at once (to fill the whole 32-bit space available). Later you can deal with character encoding - UTF-8 and the like; C doesn't have anything built-in to support encodings but there's plenty of free code on the internet you can use.
2. v means you're supposed to pass an array with two values. You don't pass in an array of blocks (=arrays) of numbers but a one-dimensional array of two numbers. Also, when in doubt, check what the code does.
July 10th, 2014, 02:47 PM
I see,so XTEA would be the "round function"? I can understand the logic but I'm having difficulties to be able to write down the code.
July 10th, 2014, 05:10 PM
Yeah, and if you didn't realize (it took me a minute when I read through it) their use of "round function" does not mean numeric rounding, it means the function applied during the round of processing.
What do you have so far?
July 11th, 2014, 10:25 AM
My problem is to actually having a string i.e char message = "Test" and char key = "key",how to pass as argument of the function like, encrypt(message,key) , since the arguments are uint
July 11th, 2014, 04:52 PM
Like I said earlier,
What's your current implementation of everything besides the message and key parts? I mean the function(s) for Feistel and XTEA. And whatever calls those functions, like main().
Originally Posted by requinix
July 12th, 2014, 01:45 AM
So , my first thought was doing something like
but since it gives a different output at time ,it doesn't look a properly implementation ...
char message = "Hi!",key = "KEY";
unsigned int *mesInt = (unsigned int*) (&message),*keyInt = (unsigned int*)(&key);
July 12th, 2014, 03:16 AM
All your program proves is that your OS implements ASLR, since it prints out different values each time (for your pointer).
> bar.c:12:3: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat]
This is wrong on two counts.
1. You don't have 3 unsigned ints, you have 3 chars.
2. You're not subscripting your pointer anyway.
Last edited by salem; July 12th, 2014 at 11:39 PM.
July 12th, 2014, 12:45 PM
Yeah, before compiling it seems that wasn't right , but like I said I don't know how to properly do it.
Originally Posted by salem
July 12th, 2014, 09:22 PM
Yes, I don't know how to convert correctly the types
July 12th, 2014, 11:53 PM
> char message = "Hi!",key = "KEY";
Originally Posted by wikipedia
To satisfy the input requirements, you need at least this.
Otherwise, you end up encrypting unknown data with an unknown key.
> Yes, I don't know how to convert correctly the types
char message = "Hi!",key = "KEY";
The result is in your message array.
I suggest using the %d format to print each element of message.
July 13th, 2014, 02:25 AM
First I want to thank you guys for the patience. But when the string is bigger than 8 chars , I have to break it into blocks right?
July 13th, 2014, 06:41 AM
You also need to read the specification to find out how to 'pad' the last block to a multiple of 8 bytes. Is it just zeros, or something else?
Likewise, you need to make sure the key is exactly 128 bits to begin with.
July 14th, 2014, 11:39 PM
Thank you guys for the help