Groovy hepcat Ben Chivers was jivin' on 23 Dec 2003 09:20:02 -0800 in
comp.lang.c.
TEA Implementation's a cool scene! Dig it!
I am having a few difficulties with implementing the tiny encryption
algorithm (TEA) source code into a c program. I am rather new to c
programming and have very little experience in it. I am confused in
the way you send data to the sub-routines to encrypt and decrypt data.
I was using the following source code to encrypt a 4 letter string,
by it does not work. I am completely confused in how you can encrypt
strings with the algorithm. Any help with this would be most
appreciated!
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned long v[2], k[4];
char endp[5];
v[0] = strtoul(" the", endp, 2);
That's wrong. The second argument should be a pointer to pointer to
char. It should be the address of a pointer to char. But you are
passing an array of char (which converts to a pointer to char). This
is the wrong type, and makes no logical sense.
Also, the string in the first argument should contain digits at the
start, otherwise strtoul() will return 0 and set the pointer pointed
at by its second argument to point at the first character. Ovbiously
this is no good. It makes no sense. What are you trying to do,
exactly?
Only one array element is being used here. What do you need an array
for if you only use one element?
k[0] = strtoul("key ", endp, 2);
Same here.
At this point code() is an undeclared function. A C90 compiler will
assume it returns an int, and will do no argument checking. A C99
compiler won't even accept it at all.
return 0;
}
void code(long* v, long* k) {
unsigned long y=v[0],z=v[1], sum=0, /* set up */
v[1] is uninitialised and, therefore, contains an indeterminate
value.
Set up what? This comment is useless. It doesn't do anithing to help
me understand the program. It does not explain the purpose of these
variables. All it does is clutter the code, making it harder to
understand.
delta=0x9e3779b9, n=32 ; /* a key schedule constant */
A what? Again, this comment really doesn't say much. It's slightly
more helpful that the previous one, but only because now I know that
either delta or n contains something called a key schedule constant
(whatever that is).
while (n-->0) { /* basic cycle start */
Ye gads! Seems like a rather confusing way of writing
for(n = 0; n < 32; n++)
{
Since the value of n is never used, except to control this loop, the
more usual form (as above) would be better.
And again, the comment is useless, and only clutters the code.
sum += delta ;
y += (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1] ;
z += (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3] ; /* end cycle */
k[1], k[2] and k[3] are uninitialised and, therefore, contain
indeterminate values. Since v[1] is uninitialised, z is based on an
indeterminate value and is, therefore, indeterminate itself.
And once again, the comment is useless. It is not clear how this
code is supposed to work, so a more useful comment that briefly,
succinctly describes what it is supposed to do would be in order.
The code is messy and inconsistently formatted, making it more
difficult to read. It contains utterly pointless comments that make
the situation even worse instead of making it easier to understand.
This dificulty is further exasperated by the lack of proper white
space and grouping of logically related statements. The way the code
is supposed to work is unclear, especially since you seem to be
seriously confused about C and have written the worst code I've seen
posted here in months! The program appears to have no input and no
output, unless I've missed something.
I know it seems like I'm being mean. I'm sorry! I don't mean to beat
you up. But look at this thing from our point of view. You post a
complete mess and tell us it doesn't work. You don't say how it's
supposed to work, nor even what "it does not work" means. (It won't
compile; compiles but crashes; compiles and runs but produces
incorrect output; what??? Be specific, please.)
I think you have chosen to write a program that is currently beyond
your C programming skills. You should probably write some simpler code
until you're more confident and compitent, and come back to this in a
few months.
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?