TEA实施 [英] TEA Implementation

查看:69
本文介绍了TEA实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将微型加密

算法(TEA)源代码实现到c程序时遇到了一些困难。我很擅长编程,并且很少有经验。我在将数据发送到子例程以加密和解密数据的方式很困惑。

我使用以下源代码加密4个字母的字符串,

由它起作用。我对如何用算法加密

字符串感到困惑。任何有关这方面的帮助都是最值得赞赏的!

#include< stdio.h>

#include< stdlib.h>


int main()

{

unsigned long v [2],k [4];

char endp [5];


v [0] = strtoul(",endp,2);

k [0] = strtoul( " key",endp,2);


代码(v,k);


返回0;

}


无效代码(长* v,长* k){

无符号长y = v [0],z = v [1 ],sum = 0,/ *设置* /

delta = 0x9e3779b9,n = 32; / *关键时间表常数* /

而(n - > 0){/ *基本周期开始* /

总和+ = delta;

y + =(z<< 4)+ k [0] ^ z + sum ^(z>> 5)+ k [1];

z + =(y<< ; 4)+ k [2] ^ y + sum ^(y>> 5)+ k [3]; / *结束周期* /

}

v [0] = y; v [1] = z; }

解决方案

是** *********@ntlworld.com (Be​​n Chivers)写道:

新闻:8b **************** **********@posting.google.c om:

我在实施微型加密算法(TEA)源方面遇到一些困难代码到ac程序。我比较新c




我甚至无法阅读这段代码,这里是为人类重新格式化的。希望

这有帮助。


#include< stdio.h>

#include< stdlib.h>


int main(无效)

{

unsigned long v [2];

unsigned long k [4];

char endp [5];


v [0] = strtoul(",endp,2);

k [0] = strtoul(" key",endp,2);


代码(v,k);


返回0;

}


无效代码(长* v,长* k)

{

/ *设置关键时间表常数* /

无符号长y = v [0];

无符号长z = v [1];

unsigned long sum = 0;

unsigned long delta = 0x9e3779b9;

unsigned long n = 32;


while(n--> 0)

{

/ *基本周期开始* /

总和+ = delta;

y + =(z<< 4)+ k [0] ^ z + sum ^(z>> 5)+ k [1];

z + = (y << 4)+ k [2] ^ y + sum ^(y>>> 5)+ k [3];

/ *结束周期* /

}


v [0] = y;

v [1 ] = z;

}


-

- 马克 - >

-




" Ben Chivers"其中p是*********** @ ntlworld.com>在消息中写道

#include< stdio.h>
#include< stdlib.h>

int main()
{
unsigned long v [2],k [4];
char endp [5];

v [0] = strtoul(",endp,2);
k [0] = strtoul(" key",endp,2);

这是对strtol()的误用。 strtol旨在将人类可读的
数字字符串转换为计算机可读的二进制文件。通过传递的"你不是
给strtol()一个人类可读的数字来转换。

我认为你要做的就是将文本转换成某种32位

整数,基于编码方案(可能是ASCII,也许是别的东西)。

在C中,char变量可以被视为字符或小的

整数,所以在某种意义上编码是为你完成的。你只需要将字母打包成长字。

v [0] =(''''<< 24)| (''t''<< 16)| (''h''<< 8)| '''';
代码(v,k);

返回0;
}

无效代码(长* v,长* k)


/ *如果你每行声明一个变量,它会使代码更加轻松* /

/ *

do你知道你在做什么混合签名和未签名吗?

* /无符号长y = v [0],z = v [1],sum = 0,/ *设置* /
delta = 0x9e3779b9,n = 32; / *关键时间表常数* /
while(n - > 0){/ *基本周期开始* /
总和+ = delta;
/ *

将括号放在这里以消除歧义。

* / y + =(z << 4)+ k [0] ^ z + sum ^(z>> 5)+ k [1];
z + =(y << 4)+ k [2] ^ y + sum ^(y>> 5)+ k [3] ; / *结束周期* /
}
v [0] = y; v [1] = z; }




我相信这个功能正在做你想要的。




2003年12月23日星期二,Mark A. Odell写道:


是*********** @ ntlworld.com (Be​​n Chivers)写道......


我在实施时遇到了一些困难微小加密算法(TEA)源代码转换成ac程序。我是新手,我甚至无法阅读这段代码,这里是为人类重新格式化的。希望
这会有所帮助。




谢谢,马克。 OP应该注意。无论如何,这是我对原始问题的看法

...

#include< stdio.h>
#include< stdlib.h> ;

int main(void)
{
unsigned long v [2];
unsigned long k [4];
char endp [5] ;

v [0] = strtoul(",endp,2);
k [0] = strtoul(key,endp,2);


滥用''strtoul''。 ''strtoul''将字符串转换为12345等字符串。进入

数字,如12345,而非字符串,如的"数字如

0x20716663。 [我没有打扰查找真正的ASCII码。]

code(v,k);


这个TEA的实现显然是[对一个优秀的C程序员,

虽然肯定不是新手]期待你通过它

指向实际数据的指针。像这样:


代码((长*)"要加密的字符串",(long *)" the key");


只差一点点;你看,这个函数是

,一次只能获取两个longs'的数据(在这个案例中,它是b / b,显然是为32-设计的)有点多头)。所以你

必须使用一个循环来做[几乎]正确:

'b'*在''main''顶部声明这些* /

char key [16] =" passwordpassword";

char plaintext [24] ="要加密的文本; / *必须是8 | * /

char * p;

char * end = plaintext + sizeof plaintext;


for(p = plaintext; p < end; p + = 2)

{

/ *演员阵容难看,但需要* /

代码((长*) )p,(长*)键);

}

返回0;
}

无效代码(长* v, long * k)


这个TEA的实现非常糟糕,恕我直言。它的名字很差

命名,输入错误类型的参数,甚至没有使用便携式的假装,并用

密文而不是将其存储在新缓冲区中。另外,它有一个隐藏的要求:明文是8字节长的多个

[因此明文旁边的神秘评论[32]

以上] - 否则就是barfs。


TEA的实际运作方式是黑魔法。据我所知,b $ b;所有你需要知道的是它要你传递它

4 * 4 = 16字节的keytext,2 * 4 = 8字节的明文,其中

it将转换为2 * 4 = 8字节的密文并将

存回明文缓冲区。并且它只适用于系统

其中''long''正好* 4字节长(字节宽度为8位)。


Left在剩下的代码中为了那些

来自我的人的利益:),但没有进一步的评论。


-Arthur

{
/ *设置关键时间表常数* /
无符号长y = v [0];
无符号长z = v [1];
无符号长sum = 0;
unsigned long delta = 0x9e3779b9;
unsigned long n = 32;

while(n - > 0)
{
/ *基本周期开始* /
总和+ = delta;
y + =(z << 4)+ k [0] ^ z + sum ^(z>> 5)+ k [1];
z + =(y << 4)+ k [2] ^ y + sum ^(y>>> 5)+ k [3];
/ *结束周期* /

v [0] = y;
v [1] = z;
}



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);
k[0] = strtoul("key ", endp, 2);

code(v,k);

return 0;
}

void code(long* v, long* k) {
unsigned long y=v[0],z=v[1], sum=0, /* set up */
delta=0x9e3779b9, n=32 ; /* a key schedule constant */
while (n-->0) { /* basic cycle start */
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 */
}
v[0]=y ; v[1]=z ; }

解决方案

be***********@ntlworld.com (Ben Chivers) wrote in
news:8b**************************@posting.google.c om:

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



I couldn''t even read this code, here it is reformatted for humans. Hope
this helps.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
unsigned long v[2];
unsigned long k[4];
char endp[5];

v[0] = strtoul(" the", endp, 2);
k[0] = strtoul("key ", endp, 2);

code(v, k);

return 0;
}

void code(long *v, long *k)
{
/* set up a key schedule constant */
unsigned long y = v[0];
unsigned long z = v[1];
unsigned long sum = 0;
unsigned long delta = 0x9e3779b9;
unsigned long n = 32;

while (n-- > 0)
{
/* basic cycle start */
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 */
}

v[0] = y;
v[1] = z;
}

--
- Mark ->
--



"Ben Chivers" <be***********@ntlworld.com> wrote in message

#include <stdio.h>
#include <stdlib.h>

int main()
{
unsigned long v[2], k[4];
char endp[5];

v[0] = strtoul(" the", endp, 2);
k[0] = strtoul("key ", endp, 2);
This is a misuse of strtol(). strtol is designed to convert human-readable
number strings to computer-readable binaries. By passing " the" you are not
giving strtol() a human-readable number to convert.
I think what you wnat to do is to convert text to some sort of 32-bit
integer, based on a coding scheme (maybe ASCII, maybe something else).
In C, char variables can be treated either as characters or as small
integers, so in a sense the coding is done for you. You just need to pack
the letters into a long.
v[0] = ('' '' << 24) | (''t'' << 16) | (''h'' << 8) | ''e'';
code(v,k);

return 0;
}

void code(long* v, long* k)
/* it makes code much more readbale if you declar one variable per line */
/*
do you know what you''re doing mixing signed and unsigned here ?
*/ unsigned long y=v[0],z=v[1], sum=0, /* set up */
delta=0x9e3779b9, n=32 ; /* a key schedule constant */
while (n-->0) { /* basic cycle start */
sum += delta ; /*
put parentheses in here to disambiguate.
*/ y += (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1] ;
z += (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3] ; /* end cycle */
}
v[0]=y ; v[1]=z ; }



I trust that this function is doing what you want.



On Tue, 23 Dec 2003, Mark A. Odell wrote:


be***********@ntlworld.com (Ben Chivers) wrote...


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
I couldn''t even read this code, here it is reformatted for humans. Hope
this helps.



Thanks, Mark. The OP should take note. Anyway, here''s my take
on the original question...
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
unsigned long v[2];
unsigned long k[4];
char endp[5];

v[0] = strtoul(" the", endp, 2);
k[0] = strtoul("key ", endp, 2);
Misuse of ''strtoul''. ''strtoul'' converts strings like "12345" into
numbers like 12345, not strings like " the" into numbers like
0x20716663. [I didn''t bother looking up the real ASCII codes.]
code(v, k);
This implementation of TEA is obviously [to a good C programmer,
although certainly not to a novice] expecting you to pass it
pointers to the actual data. Like this:

code((long*)"the string to be encrypted", (long *)"the key");

Only a little bit worse than that; you see, the function is
designed to take only two longs'' worth of data at a time (in
this case, it''s obviously designed for 32-bit longs). So you
have to use a loop to do it [almost] right:

/* at the top of ''main'' declare these */
char key[16] = "passwordpassword";
char plaintext[24] = "the text to be encrypted"; /* must be 8| */
char *p;
char *end = plaintext + sizeof plaintext;

for (p = plaintext; p < end; p += 2)
{
/* The casts are ugly, but required */
code((long*)p, (long*)key);
}
return 0;
}

void code(long *v, long *k)
This implementation of TEA is pretty awful, IMHO. It''s poorly
named, takes parameters of the wrong type, doesn''t even make a
pretense of being portable, and overwrites the plaintext with
the ciphertext rather than storing it in a new buffer. Also, it
has a hidden requirement: that the plaintext be exactly a multiple
of 8 bytes long [hence the cryptic comment next to plaintext[32]
above] -- otherwise it barfs.

The actual workings of TEA are "black magic" as far as I''m
concerned; all you need to know is that it wants you to pass it
4*4 = 16 bytes of keytext, and 2*4 = 8 bytes of plaintext, which
it will convert into 2*4 = 8 bytes of ciphertext and store
back in the plaintext buffer. And that it only works on systems
where ''long'' is *exactly* 4 bytes long (and bytes are 8 bits wide).

Left in the rest of the code for the benefit of those who
come after me :), but no further comments.

-Arthur
{
/* set up a key schedule constant */
unsigned long y = v[0];
unsigned long z = v[1];
unsigned long sum = 0;
unsigned long delta = 0x9e3779b9;
unsigned long n = 32;

while (n-- > 0)
{
/* basic cycle start */
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 */
}

v[0] = y;
v[1] = z;
}



这篇关于TEA实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆