概述
DES加密算法,算是密码学里入门的知识了,前段时间有兴趣搞了一下,但是没有做完。现在桌面实在是东西太多了,暂且丢到博客里,以后有空再来改一下。
一共三个文件:
- data.h 各种表组成的头文件
- des.h DES加密算法的主要函数都在里边了
- test.cpp 测试各个函数的测试文件
贴代码:
/*
下面是DES加密算法的各种置换表
由Halfish整理,2014.12.13
*/
//初始置换表IP
int IP_Table[64] = {
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7,
56,48,40,32,24,16,8,0,
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6
};
//逆初始置换表IP^-1
int IP_1_Table[64] = {
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25,
32,0,40,8,48,16,56,24
};
//扩展置换表E
int E_Table[48] = {
31, 0, 1, 2, 3, 4,
3, 4, 5, 6, 7, 8,
7, 8, 9,10,11,12,
11,12,13,14,15,16,
15,16,17,18,19,20,
19,20,21,22,23,24,
23,24,25,26,27,28,
27,28,29,30,31, 0
};
//置换选择1
int PC_1[56] = {
56,48,40,32,24,16,8,
0,57,49,41,33,25,17,
9,1,58,50,42,34,26,
18,10,2,59,51,43,35,
62,54,46,38,30,22,14,
6,61,53,45,37,29,21,
13,5,60,52,44,36,28,
20,12,4,27,19,11,3
};
//置换选择2, 又叫做压缩置换
int PC_2[48] = {
13,16,10,23,0,4,2,27,
14,5,20,9,22,18,11,3,
25,7,15,6,26,19,12,1,
40,51,30,36,46,54,29,39,
50,44,32,46,43,48,38,55,
33,52,45,41,49,35,28,31
};
//对左移次数的规定
int left_move_times[16] = {
1,1,2,2, 2,2,2,2,
1,2,2,2, 2,2,2,1
};
//置换函数P
int P_Table[32] = {
15, 6,19,20,28,11,27,16,
0,14,22,25, 4,17,30, 9,
1, 7,23,13,31,26, 2, 8,
18,12,29, 5,21,10, 3,24
};
//S盒
int S[8][64] =
{
//S1
{
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
},
//S2
{
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9
},
//S3
{
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
},
//S4
{
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
},
//S5
{
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3
},
//S6
{
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
},
//S7
{
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
},
//S8
{
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
}
};
des.h
/*
* Halfish
* 2014 December
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include "data.h"
using namespace std;
const int EXTRA_BITS = 1;
void mstrcpy(char to[], char from[], int offset) {
for (int i = 0; i < offset; ++i)
{
to[i] = from[i];
}
}
// 初始置换
void initial_permutation(char plain_block[])
{
char temp[64 + EXTRA_BITS];
mstrcpy(temp, plain_block, 64);
for (int i = 0; i < 64; ++ i) {
plain_block[i] = temp[IP_Table[i]];
}
}
// 置换选择1: subkey[56] <-- key[64]
void get_subkey(char subkey[], const char key[])
{
for (int i = 0; i < 56; ++ i)
{
subkey[i] = key[PC_1[i]];
}
}
void shift_subkey(int shift, char subkey[], int size)
{
char temp0 = subkey[0];
char temp1 = subkey[1];
switch(shift) {
case 1:
for (int i = 0; i < size - 1; ++ i) {
subkey[i] = subkey[i + 1];
}
subkey[size - 1] = temp0;
break;
case 2:
for (int i = 0; i < size - 1; ++ i) {
subkey[i] = subkey[i + 2];
}
subkey[size - 2] = temp0;
subkey[size - 1] = temp1;
break;
default:
break;
}
}
// 扩展置换 expand_block[48] <-- plain_right[32]
void expand_permutation(char expand_block[], char plain_right[])
{
for (int i = 0; i < 48; ++ i)
{
expand_block[i] = plain_right[ E_Table[i] ];
}
}
// 压缩置换后,得到48位的字密钥
void compress_permutation(char comp_key[], char subkey[])
{
for (int i = 0; i < 48; ++ i)
{
comp_key[i] = subkey[ PC_2[i] ];
}
}
int char2int(char a[], int index, int offset)
{
int base = 1;
int ret = 0;
for (int i = offset - 1; i >= 0; -- i)
{
ret += (a[index + i] - '0') * base;
base *= 2;
}
return ret;
}
void int2char(int num, char a[], int index, int offset)
{
char temp[10];
int i = 0;
while(num) {
temp[i] = num % 2 + '0';
num = num / 2;
++ i;
}
for (int j = 0; j < offset; ++ j)
{
a[index + j] = '0';
}
for (int j = 0; j < i; ++ j)
{
a[index + offset - j - 1] = temp[j];
}
//a[offset] = '\0';
}
// S-盒置换 sbox_block[32] <-- expand_block[48]
void sbox_substitution(char sbox_block[], char expand_block[])
{
for (int i = 0; i < 8; ++ i)
{
int location = 0;
char temp_int[2 + EXTRA_BITS];
temp_int[0] = expand_block[i*6];
temp_int[1] = expand_block[i*6 + 5];
location += char2int(temp_int, 0, 2) * 16;
location += char2int(expand_block, i * 6 + 1, 4);
int2char(S[i][location], sbox_block, i * 4, 4);
}
}
// P-盒置换 sbox
void pbox_substitution(char pbox_block[], char sbox_block[])
{
for (int i = 0; i < 32; ++ i)
{
pbox_block[i] = sbox_block[ P_Table[i] ];
}
}
void Xor(char c[], char a[], char b[], int size)
{
for (int i = 0; i < size; ++ i)
{
// 异或操作,相同为0,不同为1
c[i] = (a[i] == b[i]) ? '0' : '1' ;
}
}
void final_permutation(char cipher_block[], char plain_block[])
{
for (int i = 0; i < 64; ++ i)
{
cipher_block[i] = plain_block[ IP_1_Table[i] ];
}
}
// for debug
void myprint(const char *funcName, char *arr, int size, int columns)
{
cout << funcName << endl;
for (int i = 0; i < size; ++ i)
{
cout << *(arr ++) ;
if ( (i + 1) % columns == 0) {
cout << endl;
}
}
cout << endl;
}
void encryption(char myplain_block[], char cipher_block[], char key[]) {
char plain_block[64 + EXTRA_BITS];
char plain_left[32 + EXTRA_BITS];
char plain_right[32 + EXTRA_BITS];
char subkey[56 + EXTRA_BITS];
char comp_key[48 + EXTRA_BITS];
char expand_block[48 + EXTRA_BITS];
char sbox_block[32 + EXTRA_BITS];
char pbox_block[32 + EXTRA_BITS];
mstrcpy(plain_block, myplain_block, 64);
// 初始置换
initial_permutation(plain_block);
strncpy(plain_left, plain_block, 32);
strncpy(plain_right, plain_block + 32, 32);
// 置换选择1, 得到初始子密钥
get_subkey(subkey, key);
for (int i = 0; i < 16; ++ i)
{
// 将56位的subkey左移一位或者两位
shift_subkey(left_move_times[i], subkey, 28);
shift_subkey(left_move_times[i], subkey + 28, 28);
// 压缩置换,又称为置换选择2,将56位的subkey压缩成48位的comp_key
compress_permutation(comp_key, subkey);
// 扩展置换,将32位的 plain_right 扩展成48位
expand_permutation(expand_block, plain_right);
// 异或操作
Xor(expand_block, expand_block, comp_key, 48);
// S盒替换
sbox_substitution(sbox_block, expand_block);
if (i == 0)
{
myprint("sbox_substitution", sbox_block, 32, 8);
}
// P盒替换
pbox_substitution(pbox_block, sbox_block);
if (i == 0)
{
myprint("pbox_substitution", pbox_block, 32, 8);
}
char temp_right[32 + EXTRA_BITS];
mstrcpy(temp_right, plain_right, 32);
// 异或操作
Xor(plain_right, pbox_block, plain_left, 32);
if (i == 0)
{
myprint("pbox_block", pbox_block, 32, 8);
myprint("L0", plain_left, 32, 8);
myprint("R1", plain_right, 32, 8);
}
mstrcpy(plain_left, temp_right, 32);
if (i == 0 || i == 15)
{
cout << i << endl;
myprint("R1", plain_right, 32, 8);
myprint("L1", plain_left, 32, 8);
}
}
mstrcpy(plain_block, plain_left, 32);
mstrcpy(plain_block + 32, plain_right, 32);
// 末置换
final_permutation(cipher_block, plain_block);
}
void decreption(char key[], char plain_block[], char cipher_block[])
{
}
/*
* test.cpp
* Halfish, 2014, December
*/
#include <iostream>
#include "des.h"
using namespace std;
int main(int argc, char const *argv[])
{
char plain_block[64 + EXTRA_BITS]; // 明文块
char cipher_block[64 + EXTRA_BITS]; // 密文块
char key[64 + EXTRA_BITS]; // 密钥
strcpy(plain_block, "0000000100100011010001010110011110001001101010111100110111101111");
strcpy(key, "0001001100110100010101110111100110011011101111001101111111110001");
encryption(plain_block, cipher_block, key);
myprint("plain_block", plain_block, 64, 8);
myprint("key", key, 64, 8);
myprint("cipher_block", cipher_block, 64, 8);
return 0;
}