c - C语言中的Blowfish加密函数(BF_encrypt)

标签 c encryption blowfish

我正在尝试了解 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 type long like 1 + 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/

相关文章:

java - Blowfish 在 Java/Scala 中加密并在 bash 中解密

Java Blowfish 解密不返回原始字符串

c - 在 32 位系统上寻找非常大的文件

c - 使用外部函数在任务中获取互斥量

php - 从MySQL普通数据库值迁移到完全AES加密的值(PHP APi)

encryption - AES 解密 iv 错误 : must be a class [B value

c - 为什么用指针声明字符串数组?这样做时到底会发生什么?

c - 快速检查 C99 中函数指针的结构是否为 NULL

encryption - Microsoft Office 2010 使用什么算法进行加密?

c++ - 它是否等同于使用 null iv 执行 CBC 模式?