我不知道这段代码是如何工作的; ; [英] I don't know how this code work; ;

查看:104
本文介绍了我不知道这段代码是如何工作的; ;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习DES,但我遇到了这个部分的麻烦;;

有谁能告诉我这段代码是如何工作的?

以及为什么将位移4?16 2 8 ??



i'm studying DES but i have troubles with this part;;
can anyone tell me how this code work??
and why shift bits by 4 16 2 8??

private void des( int[] inInts, int[] outInts, int[] keys )
	{
	int fval, work, right, leftt;
	int round;
	int keysi = 0;

	leftt = inInts[0];
	right = inInts[1];

	work   = ((leftt >>>  4) ^ right) & 0x0f0f0f0f;
	right ^= work;
	leftt ^= (work << 4);

	work   = ((leftt >>> 16) ^ right) & 0x0000ffff;
	right ^= work;
	leftt ^= (work << 16);

	work   = ((right >>>  2) ^ leftt) & 0x33333333;
	leftt ^= work;
	right ^= (work << 2);

	work   = ((right >>>  8) ^ leftt) & 0x00ff00ff;
	leftt ^= work;
	right ^= (work << 8);
	right  = (right << 1) | ((right >>> 31) & 1);

	work   = (leftt ^ right) & 0xaaaaaaaa;
	leftt ^= work;
	right ^= work;
	leftt  = (leftt << 1) | ((leftt >>> 31) & 1);

	for ( round = 0; round < 8; ++round )
	    {
	    work   = (right << 28) | (right >>> 4);
	    work  ^= keys[keysi++];
	    fval   = SP7[ work	       & 0x0000003f ];
	    fval  |= SP5[(work >>>  8) & 0x0000003f ];
	    fval  |= SP3[(work >>> 16) & 0x0000003f ];
	    fval  |= SP1[(work >>> 24) & 0x0000003f ];
	    work   = right ^ keys[keysi++];
	    fval  |= SP8[ work         & 0x0000003f ];
	    fval  |= SP6[(work >>>  8) & 0x0000003f ];
	    fval  |= SP4[(work >>> 16) & 0x0000003f ];
	    fval  |= SP2[(work >>> 24) & 0x0000003f ];
	    leftt ^= fval;
	    work   = (leftt << 28) | (leftt >>> 4);
	    work  ^= keys[keysi++];
	    fval   = SP7[ work	       & 0x0000003f ];
	    fval  |= SP5[(work >>>  8) & 0x0000003f ];
	    fval  |= SP3[(work >>> 16) & 0x0000003f ];
	    fval  |= SP1[(work >>> 24) & 0x0000003f ];
	    work   = leftt ^ keys[keysi++];
	    fval  |= SP8[ work	       & 0x0000003f ];
	    fval  |= SP6[(work >>>  8) & 0x0000003f ];
	    fval  |= SP4[(work >>> 16) & 0x0000003f ];
	    fval  |= SP2[(work >>> 24) & 0x0000003f ];
	    right ^= fval;
	    }

	right  = (right << 31) | (right >>> 1);
	work   = (leftt ^ right) & 0xaaaaaaaa;
	leftt ^= work;
	right ^= work;
	leftt  = (leftt << 31) | (leftt >>> 1);
	work   = ((leftt >>>  8) ^ right) & 0x00ff00ff;
	right ^= work;
	leftt ^= (work << 8);
	work   = ((leftt >>>  2) ^ right) & 0x33333333;
	right ^= work;
	leftt ^= (work << 2);
	work   = ((right >>> 16) ^ leftt) & 0x0000ffff;
	leftt ^= work;
	right ^= (work << 16);
	work   = ((right >>>  4) ^ leftt) & 0x0f0f0f0f;
	leftt ^= work;
	right ^= (work << 4);
	outInts[0] = right;
	outInts[1] = leftt;
	}





我的尝试:



是的,我正在尝试这个部分3周。排列太难了;;有人帮我TT ;;



What I have tried:

yeah i'm trying this part for 3 weeks. too difficult about permutation;; somebody help me T.T;;

推荐答案

您是否知道逐行解释代码的工作量是多少?

每行都需要一段解释!例如:

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();



创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。



可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?



不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 您是否想坐下45分钟并无缘无故地输入逐行描述?



所以回到Wiki并再次阅读DES算法描述。然后阅读: DES Algorithm Illustrated [ ^ ]这很清楚地解释了DES。


Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

So go back to Wiki and read the DES algorithm description again. Then read this: The DES Algorithm Illustrated[^] which explains DES pretty clearly.


这篇关于我不知道这段代码是如何工作的; ;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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