密码学之Blowfish与Twofish
前言
Blowfish和 Twofish 都属于block cipher的一种,发明这两个算法的都是大神Bruce Schneier。Blowfish因为key太短,不再被推荐使用。

一、Blowfish
Blowfish是一个64-bit block length, 16-round Feistel Network.
我们从master key 中提取18个subkey
以及根据master key生成个S-Box(Substitution Box):
然后流程如下:

其中的f函数如下:

Key schedule太复杂难以理解所以略过。
原文如下供参考:
1. Initialize first the P-array and then the four S-boxes, in order, with a fixed string. This string consists of the hexadecimal digits of pi (less the initial 3).
2. XOR P1 with the first 32 bits of the key, XOR P2 with the second 32-bits of the key, and so on for all bits of the key. Repeatedly cycle through the key bits
3. Encrypt the all-zero string with the Blowfish algorithm, using the subkeys described in steps (1) and (2).
4. Replace P1 and P2 with the output of step (3).
5. Encrypt the output of step (3) using the Blowfish algorithm with the modified subkeys.
6. Replace P3 and P4 with the output of step (5).
7. Continue the process, replacing all entries of the P-array, and then all four S-boxes in order, with the output of the continuously-changing Blowfish algorithm.

二、Twofish
Twofish是一个128-bit block length, 16-round Feistel Network. 使用key的长度为128~256 bits.

其中,A,B,C,D都是32-bit的序列。
函数F如下:

其中函数g的计算过程如下:
首先将输入的32-bit序列拆分成4个8-bit的序列. 每一个序列都通过一个S-Box,
. 接下来用一个4*4的MDS(Maximum Distance Separable)矩阵 over
,计算
,最后串联z中四个元素
.
这里的MDS矩阵作者已经给出:
01 EF 5B 5B
5B EF EF 01
EF 5B 01 EF
EF 01 EF 5B

三、Sub-key和S-Box的生成
twofish中一共用到40个sub-key,每一个都是32 bits.
首先将master key拆分为多个32-bit的子序列,这些子序列的数量由master key的长度决定(128-bit的话就是4个,256-bit的话就是8个)。如果master key的长度不够则用padding补足。
然后将下标分别为奇数和偶数的子序列分到2个不同的组中,这两个组用来计算subkey。
然后将master key拆分为多个8-bit的子序列,然后每8个序列为一组,组成一个向量,然后让一个
的RS矩阵和刚才的那个向量做矩阵的乘法运算。
RS矩阵作者已经给出:
01 A4 55 87 5A 58 DB 9E
A4 56 82 F3 1E C6 68 E5
02 A1 FC C1 47 AE 3D 19
A4 55 87 5A 58 DB 9E 03
上述计算得到的所有结果将归到同一个组
里面,
将参与到S-BOX的运算中:
,X为32-bit的输入值。
Subkey的计算方式如下:
h函数太过于复杂,这里就列原图了:

其中,分别为2个permutation运算。
最后放上一段用Twofish加密的密文,代码可参见——https://www.medo64.com/2016/01/twofish-in-csharp/
原文:Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Key:199 247 109 46 240 141 51 38 36 125 108 51 213 170 204 36
Op Mode:CBC
密文:217 235 175 81 36 252 44 160 41 34 38 109 199 3 184 147 69 118 178 172 179 217 97 161 82 132 101 214 37 23 201 159 93 25 21 46 211 165 227 185 123 136 237 154 176 127 160 117 138 187 236 178 239 177 141 10 2 151 94 141 223 154 54 65 229 85 255 217 248 240 168 251 71 151 115 200 77 49 66 178 164 224 103 163 75 230 125 83 71 96 178 60 100 135 69 254 213 0 222 105 91 89 148 203 74 85 120 138 224 61 73 198 207 68 36 122 216 252 175 120 7 198 179 118 157 199 131 65 10 12 242 181 188 13 14 135 251 22 176 89 130 83 62 192 238 75 145 217 54 97 247 134 70 61 7 7 123 99 205 37 38 143 74 51 98 156 181 164 42 176 86 176 99 253 254 52 207 103 34 157 174 242 225 16 245 98 10 200 24 90 28 114 166 10 200 83 81 220 72 3 88 77 83 96 1 154 135 226 129 86 16 30 47 167 165 236 109 214 185 21 82 121 157 89 187 95 121 138 194 211 237 13 88 243 9 253 92 216 23 59 100 94 69 123 240 85 252 7 11 153 51 186 70 186 105 96
这里的每个数字都是十进制下的数字,每个数字代表一个byte(范围从0~255),例如199即11000111b
有关Twofish的分析实在是太多了(而且大多都非常专业)读不过来。
反正,这是一个安全的加密方案。

参考资料
Bruce Sctmeier - Description of a New Variable-Length Key, 64-Bit Block Cipher
Bruce Schneier, John Kelseyy, Doug Whitingz, David Wagnerx, Chris Hall - Two sh: A 128-Bit Block Cipher
Twofish in C# - https://www.medo64.com/2016/01/twofish-in-csharp/

THE END.