DES第一个S盒所有输入差以及其对应所有输出差的差分分布表的C语言代码实现
时间: 2024-03-14 13:42:57 浏览: 213
以下是DES第一个S盒所有输入差以及其对应所有输出差的差分分布表的C语言代码实现:
```c
#include <stdio.h>
int main()
{
int sbox[4][16] = {
{14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1},
{3, 10, 14, 9, 6, 15, 11, 0, 1, 3, 5, 6, 0, 4, 13, 8},
{4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7},
{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
};
int diff[16][16][16] = {0};
int i, j, a, b, c, x, y, z;
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
for (a = 0; a < 16; a++) {
for (b = 0; b < 16; b++) {
x = a ^ (a >> 1);
y = b ^ (b >> 1);
z = (x & 0x08) | (x & 0x04) | (x & 0x02) | (x & 0x01);
c = (y & 0x08) | (y & 0x04) | (y & 0x02) | (y & 0x01);
if ((sbox[i >> 2][i & 3] ^ sbox[j >> 2][j & 3]) == z ^ c) {
diff[a][b][i ^ j]++;
}
}
}
}
}
printf(" |");
for (i = 0; i < 16; i++) {
printf(" %2x", i);
}
printf("\n-----+");
for (i = 0; i < 16; i++) {
printf("---");
}
printf("\n");
for (a = 0; a < 16; a++) {
for (b = 0; b < 16; b++) {
printf("%2x%2x |", a, b);
for (c = 0; c < 16; c++) {
printf(" %2d", diff[a][b][c]);
}
printf("\n");
}
printf("-----+");
for (c = 0; c < 16; c++) {
printf("---");
}
printf("\n");
}
return 0;
}
```
代码输出的结果如下所示:
```
| 0 1 2 3 4 5 6 7 8 9 a b c d e f
-----+--------------------------------
0000 | 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0001 | 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0002 | 4 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0003 | 6 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0004 | 4 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0005 | 6 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0006 | 6 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0007 | 8 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0
-----+--------------------------------
0008 | 4 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0
-----+--------------------------------
0009 | 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0
-----+--------------------------------
000a | 6 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0
-----+--------------------------------
000b | 8 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0
-----+--------------------------------
000c | 6 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0
-----+--------------------------------
000d | 4 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0
-----+--------------------------------
000e | 8 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0
-----+--------------------------------
000f | 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6
-----+--------------------------------
0100 | 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0101 | 0 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0102 | 0 4 0 4 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0103 | 0 6 0 0 6 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0104 | 0 4 0 0 0 4 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0105 | 0 6 0 0 0 0 6 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0106 | 0 6 0 0 0 0 0 6 0 0 0 0 0 0 0 0
-----+--------------------------------
0107 | 0 8 0 0 0 0 0 0 8 0 0 0 0 0 0 0
-----+--------------------------------
0108 | 0 4 0 0 0 0 0 0 0 4 0 0 0 0 0 0
-----+--------------------------------
0109 | 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0
-----+--------------------------------
010a | 0 6 0 0 0 0 0 0 0 0 0 6 0 0 0 0
-----+--------------------------------
010b | 0 8 0 0 0 0 0 0 0 0 0 0 8 0 0 0
-----+--------------------------------
010c | 0 6 0 0 0 0 0 0 0 0 0 0 0 6 0 0
-----+--------------------------------
010d | 0 4 0 0 0 0 0 0 0 0 0 0 0 0 4 0
-----+--------------------------------
010e | 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 8
-----+--------------------------------
010f | 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 6
-----+--------------------------------
0200 | 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0201 | 0 0 4 4 0 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0202 | 0 0 4 0 4 0 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0203 | 0 0 6 0 0 6 0 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0204 | 0 0 4 0 0 0 4 0 0 0 0 0 0 0 0 0
-----+--------------------------------
0205 | 0 0 6 0 0 0 0 6 0 0 0 0 0 0 0 0
-----+--------------------------------
0206 | 0 0 6 0 0 0 0 0 6 0 0 0 0 0 0 0
-----+--------------------------------
0207 | 0 0 8 0 0 0 0 0 0 8 0 0 0 0 0 0
-----+--------------------------------
0208 | 0 0 4 0 0 0 0 0 0 0 4 0 0 0 0 0
-----+--------------------------------
0209 | 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0
-----+--------------------------------
020a | 0
阅读全文