一、实验目的
通过实现DES/AES算法,加深对DES/AES算法的理解,同时学习组合密码常用的代换、移位等运算的实现。
1)利用C\C++实现DES/AES算法的加、解密运算。
Des加密的流程图如下
按照上面的原理图实现加密主要的函数如下:
void IP_transform(int *data);//第一步进行初始置换IP,然后进行16轮迭代
//先分成L0,R0
void Get_key();
//由k生成子密钥Ki的算法
void PC_1_replace(int *key, int *key_56);//选择置换PC_1,去掉奇偶校验位并打乱,变成56位
void MoveLeft(int *c, int *d, int m);//划分成左右32位进行左移变化
void PC_2_replace(int *key_56, int *key_48);//选择置换PC_2,从CiDi中选取48位作为密钥Ki
//加密函数f
void E_extend(int *data_32, int* data_48);//位选择函数E(R),将32位的输入变成48位
//将E(R(i-1))与子密钥Ki进行异或E(R i-1)^Ki=B1 B2 B3 B4 B5 B6 B7 B8,
void S_replace(int *data_48, int *data_s32);//Bj原为6位经过S盒后缩减为4位,S1到S8,变成共为32位
void P_replace(int *data_32);//经过p盒处理得到f(R i-1,Ki)
void IP1_transform(int *data);//逆初始置换得到密文
1.由k生成子密钥Ki的算法
先进行选择置换PC_1,然后划分成左右32位进行左移变化,最后进行选择置换PC_2,从CiDi中选取48位作为密钥Ki,共执行16次轮迭代
先进行位选择函数E(R),将32位的输入变成48位,然后经过S盒将Bj缩减为4位Sj,变成共为32位,最后经过p盒处理得到f(R i-1,Ki)
代码如下:
// des.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include "pch.h"
#include
using namespace std;
void IP_transform(int *data);//第一步进行初始置换IP,然后进行16轮迭代
//先分成L0,R0
void Get_key();
//由k生成子密钥Ki的算法
void PC_1_replace(int *key, int *key_56);//选择置换PC_1,去掉奇偶校验位并打乱,变成56位
void MoveLeft(int *c, int *d, int m);//划分成左右32位进行左移变化
void PC_2_replace(int *key_56, int *key_48);//选择置换PC_2,从CiDi中选取48位作为密钥Ki
//每一轮迭代为Li=Ri-1 Ri=L i-1^f(R i-1,Ki)
//加密函数f
void E_extend(int *data_32, int* data_48);//位选择函数E(R),将32位的输入变成48位
//将E(R(i-1))与子密钥Ki进行异或E(R i-1)^Ki=B1 B2 B3 B4 B5 B6 B7 B8,
void S_replace(int *data_48, int *data_s32);//Bj原为6位经过S盒后缩减为4位,S1到S8,变成共为32位
void P_replace(int *data_32);//经过p盒处理得到f(R i-1,Ki)
void IP1_transform(int *data);//逆初始置换得到密文
char InputKey[8];//存放字符型密钥
int key[64];//64位的密钥
int keyPC1[56]; //存放密钥key经过PC1换位表后变成的56位二进制
int C[28], D[28];//将PC-1输出中的前28位作为A[28],后28位作为B[28],用LS对C、D进行循环左移变换
int keyCD[56];//将循环左移的结果合并
int K[16][48]; //存放16次循环左移产生的子密钥
int main()
{
Get_key();
char text[8];
int data[64],data_out[64];
int L0[32], R0[32];//将64位数据分成左右32位进行迭代
int temp[32];
int data_e[48],data_s[32],data_p[32];
int i,j;
char mingwen[100] = {" "};
char mw_out[100] = { " " };
cout << endl << "输入明文:";
cin >> mingwen;//因为是cin,所以明文里不能出现空格
int num = 0, count = 0;//num为分组数,count为字符数
cout << endl;
while (mingwen[count] != '\0')
{
count++;
}
cout << "count:" << count<> (7 - i % 8)) & 0x1; //将8个字符转化为64位的01字符串
}
cout << "一次加密解密:"< 32位
int P[] = { 16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25 };
void P_replace(int *data)
{
int i;
int temp[32];
for (i = 0; i < 32; i++)
{
temp[i] = data[i];
}
for (i = 0; i < 32; i++)
{
data[i] = temp[P[i] - 1];
}
}
// S盒,每个S盒是4行16列的置换表,6位 -> 4位
int S_BOX[8][4][16] = { {{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7}, //S盒
{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{4,1,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}},
{{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}} };
void S_replace(int *data, int *dataout)//Bj原为6位经过S盒后缩减为4位
{
int i, out;
int temp1 = 0;
int temp2 = 0;
int row = 0;
int col = 0;
for (i = 0; i < 8; i++)
{
temp1 = i * 6;
temp2 = i * 4;
row = (data[temp1] * 2) + data[temp1 + 5];
col = data[temp1 + 1] * 8 + data[temp1 + 2] * 4 + data[temp1 + 3] * 2 + data[temp1 + 4];
out = S_BOX[i][row][col];
dataout[temp2] = out % 2;
out /= 2;
dataout[temp2 + 1] = out % 2;
out /= 2;
dataout[temp2 + 2] = out % 2;
out /= 2;
dataout[temp2 + 3] = out % 2;
}
}
void Get_key()
{
cout << "请输入密钥(8位字符密钥):";
for (int i = 0; i < 8; i++)
{
cin >> InputKey[i];
}
cout << endl;
cin.ignore();
//64的密钥k
for (int i = 0; i < 8; i++) //将密钥转化成64位二进制数放到一维数组key中
{
int a[8] = { 0,0,0,0,0,0,0,0 };
char m = InputKey[i];
for (int j = 0; m != 0; j++)
{
a[j] = m % 2;
m = m / 2;
for (int j = 0; j < 8; j++)
key[(i * 8) + j] = a[7 - j];
}
}
cout << "密钥的二进制表示为:";
for (int i = 0; i < 64; i++)
{
cout << key[i];
}
cout << endl;
//****************由密钥k生成子密钥Ki****************
//选择置换PC-1
PC_1_replace(key, keyPC1);
//分成左右进行左移
for (int i = 0; i < 28; i++)
{
C[i] = keyPC1[i];
D[i] = keyPC1[i + 28];
}
//16轮迭代
for (int t = 0; t < 16; t++)
{
MoveLeft(C, D, t + 1);
//合并
for (int i = 0; i < 28; i++)
{
keyCD[i] = C[i];
keyCD[i + 28] = D[i];
}
//选择置换PC-2
PC_2_replace(keyCD, K[t]);
}
//打印
for (int i = 0; i < 16; i++)
{
cout << "子密钥K" << i + 1 << "为:";
for (int j = 0; j < 48; j++)
{
cout << K[i][j];
}
cout << endl;
}
}
结果如下: