我正在尝试了解 BlowFish 加密算法。我遇到了下面详细描述的一些问题:
函数如下:
void BF_encrypt (data, encrypt)
BF_LONG *data; //data is array of two members i.e ti[0] and ti[1]
//and BF_LONG is defined as unsigned long in header file
int encrypt; //encrypt is defined as 1
{
register BF_LONG l, r, *p, *s; //BF_LONG is defined as unsigned long in header file
p = key_P; //key_P is declared as BF_LONG key_P[16 + 2];
s = &(key_S[0]); //key_S is declared as BF_LONG key_S[4 * 256];
l = data[0]; //data[0]=ti[0]
r = data[1]; //data[1]=ti[1]
l ^= p[0]; //Bitwise ex-or of l with p[0];
BF_ENC (r, l, s, p[1]);
}
这里是 BF_ENC 函数:
#define BF_ENC(LL,R,S,P) \
LL^=P; \
LL^=((( S[ (R>>24L) ] + \
S[0x0100+((R>>16L)&0xff)])^ \
S[0x0200+((R>> 8L)&0xff)])+ \
S[0x0300+((R )&0xff)])&0xffffffff;
这是我的问题:
BF_ENC 中每一行的“\”是什么意思?
R>>24L,我知道它在向右移动。 L 在这里很长,但我在这里没有得到它的存在。 L对数据有什么影响?
最后一个问题是 BF_ENC 函数到底做了什么,只是一个概述,并不深入。
如果您需要更多信息,我在这里。 谢谢!
最佳答案
BF_ENC
是一个 C 风格的宏。通常那些由行尾字符分隔。 \
转义字符就在那里表示下一行应该被认为是这一行的一部分。所以一切直到0xffffffff;
是宏的一部分。
L
的效果是在许多语言中,输出的原始类型可能取决于两个操作数。 L
生成 long
类型的 24 个(以及 16 个和 8 个)文字 .这将确保输出是正确的原始类型(至少 32 位)。特别是它确保输出足够大以存储 32 位信息。
对于移位,这没有多大意义(您不会期望移位超过 64 位,因此输出可能只是放在左操作数中的原始类型)但是语言设计者喜欢对称性 - 使用运算符这样作为+
和 *
- 不喜欢特殊情况。
看之前的一个问题我发现:
yes, this was my thought as well. Afair, this really worked in K&R (
1 << 1L
was of typelong
like1 + 1L
is in ISO C), which is why I asked where the code is from...
因此请确保您验证此宏在您的环境中是否正常工作。要了解河豚鱼,您最好查看根本不包含宏的更现代的代码。
BF_ENC
Blowfishes F 函数是 Blowfish 的轮函数(Feistel 函数)。
在右上角of the Wikipedia page about Blowfish并被描述为:
The diagram to the upper right shows Blowfish's F-function. The function splits the 32-bit input into four eight-bit quarters, and uses the quarters as input to the S-boxes. The outputs are added modulo 2^32 and XORed to produce the final 32-bit output.
关于c - C语言中的Blowfish加密函数(BF_encrypt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27742043/