一、实验目的
通过实现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次轮迭代
- 首先进行初始置换IP,然后进行16轮迭代,第一步分成L0,R0,每一轮迭代为Li=Ri-1 Ri=L i-1^f(R i-1,Ki)
- 关于f函数
先进行位选择函数E(R),将32位的输入变成48位,然后经过S盒将Bj缩减为4位Sj,变成共为32位,最后经过p盒处理得到f(R i-1,Ki)
代码如下:
// des.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include "pch.h"
#include <iostream>
using namespace std;void IP_transform(int *data);//第一步进行初始置换IP,然后进行16轮迭代
//先分成L0,R0void 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<<endl;int k = count % 8;if (k == 0)num = count/8;else{num = (count - k) / 8 + 1;}for (int r = 0; r < num; r++){for (i=0;i<8; i++){text[i] = mingwen[i + 8 * r];}for (i = 0; i < 64; i++){data[i] = (text[(i / 8)] >> (7 - i % 8)) & 0x1; //将8个字符转化为64位的01字符串}cout << "一次加密解密:"<<endl;//*******************************加密*******************************IP_transform(data);//初始置换for (j = 0; j < 32; j++)//获取初始置换后明文的前后32位数据{L0[j] = data[j];R0[j] = data[j + 32];}for (i = 0; i < 16; i++) //16次轮迭代{for (j = 0; j < 32; j++) //临时变量temp记录R0的数据{temp[j] = R0[j];}E_extend(R0, data_e);//E扩展for (j = 0; j < 48; j++)//将E扩展得到的48位的数据与密钥输入Ki进行异或运算{data_e[j] = data_e[j] ^ K[i][j];}S_replace(data_e, data_s);//S盒置换P_replace(data_s);//P盒置换for (j = 0; j < 32; j++){R0[j] = L0[j] ^ data_s[j];}for (j = 0; j < 32; j++) //下一个L0为上一次迭代的右边R0{L0[j] = temp[j];}}//16次轮迭代以后//左右交换for (i = 0; i < 32; i++) //交换16次迭代后的L0和R0{data[i] = R0[i];data[i + 32] = L0[i];}IP1_transform(data);cout << "密文二进制表示为:";for (i = 0; i < 64; i++){cout << data[i];}cout << endl;cout << "输出字符为:";for (i = 0; i < 8; i++){int tempc = data[8 * i] * 128 + data[8 * i + 1] * 64+ data[8 * i + 2] *32 +data[8 * i + 3] * 16+data[8 * i + 4] *8+ data[8 * i + 5] * 4+ data[8 * i + 6] * 2+ data[8 * i + 7];printf("%c", tempc);}cout << endl;//*******************************解密*******************************IP_transform(data);//初始置换for (j = 0; j < 32; j++)//获取初始置换后明文的前后32位数据{L0[j] = data[j];R0[j] = data[j + 32];}for (i = 0; i < 16; i++) //16次轮迭代{for (j = 0; j < 32; j++) //临时变量temp记录R0的数据{temp[j] = R0[j];}E_extend(R0, data_e);//E扩展for (j = 0; j < 48; j++)//将E扩展得到的48位的数据与密钥输入Ki进行异或运算{data_e[j] = data_e[j] ^ K[15 - i][j];}S_replace(data_e, data_s);//S盒置换P_replace(data_s);//P盒置换for (j = 0; j < 32; j++){R0[j] = L0[j] ^ data_s[j];}for (j = 0; j < 32; j++) //下一个L0为上一次迭代的右边R0{L0[j] = temp[j];}}//16次轮迭代以后//左右交换for (i = 0; i < 32; i++) //交换16次迭代后的L0和R0{data[i] = R0[i];data[i + 32] = L0[i];}IP1_transform(data);cout << "该次解密出的明文为:";int ch_num = 0;char ch;for (i = 0; i < 8; i++){for (j = 0; j < 7; j++){ch_num = (ch_num + data[i * 8 + j]) * 2;}ch_num += data[i * 8 + j];ch = ch_num;cout << ch;mw_out[i + 8 * r] = ch;ch_num = 0;}cout << endl;cout << endl;}cout << "解密出的全体明文为:" << mw_out;return 0;
}//初始置换IP;
int IP[] = { 58, 50, 42, 34, 26, 18, 10, 2,60, 52, 44, 36, 28, 20, 12, 4,62, 54, 46, 38, 30, 22, 14, 6,64, 56, 48, 40, 32, 24, 16, 8,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 };void IP_transform(int *data)
{int temp[64], i;for (i = 0; i < 64; i++){temp[i] = data[i];}for (i = 0; i < 64; i++){data[i] = temp[IP[i] - 1];}
}// 逆初始置换IP^(-1)
int IP1[] = { 40, 8, 48, 16, 56, 24, 64, 32,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 };void IP1_transform(int *data)
{int i;int temp[64];for (i = 0; i < 64; i++){temp[i] = data[i];}for (i = 0; i < 64; i++){data[i] = temp[IP1[i] - 1];}
}//*******************由密钥k生成子密钥Ki所需的表*******************
// 密钥置换表,将64位密钥变成56位
int PC_1[] = { 57, 49, 41, 33, 25, 17, 9,1, 58, 50, 42, 34, 26, 18,10, 2, 59, 51, 43, 35, 27,19, 11, 3, 60, 52, 44, 36,63, 55, 47, 39, 31, 23, 15,7, 62, 54, 46, 38, 30, 22,14, 6, 61, 53, 45, 37, 29,21, 13, 5, 28, 20, 12, 4 };void PC_1_replace(int *key, int *key_56)
{for (int i = 0; i < 56; i++){key_56[i] = key[PC_1[i] - 1];}
}// 压缩置换,将56位密钥压缩成48位子密钥
int PC_2[] = { 14, 17, 11, 24, 1, 5,3, 28, 15, 6, 21, 10,23, 19, 12, 4, 26, 8,16, 7, 27, 20, 13, 2,41, 52, 31, 37, 47, 55,30, 40, 51, 45, 33, 48,44, 49, 39, 56, 34, 53,46, 42, 50, 36, 29, 32 };void PC_2_replace(int *key_56, int *key_48)
{for (int i = 0; i < 48; i++){key_48[i] = key_56[PC_2[i] - 1];}
}// 每轮左移的位数
int LS[] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };void MoveLeft(int *c,int *d,int m)
{if (m == 1 || m == 2 || m == 9 || m == 16){int temp_c = c[0];int temp_d = d[0];for (int i = 0; i < 27; i++){c[i] = c[i + 1];d[i] = d[i + 1];}c[27] = temp_c;d[27] = temp_d;}else{int temp_c0 = c[0];int temp_d0 = d[0];int temp_c1 = c[1];int temp_d1 = d[1];for (int i = 0; i < 26; i++){c[i] = c[i + 2];d[i] = d[i + 2];}c[26] = temp_c0;d[26] = temp_d0;c[27] = temp_c1;d[27] = temp_d1;}
}//*******************加密函数f所需的表*******************
// 位选择函数E,将32位扩展至48位
int E[] = { 32, 1, 2, 3, 4, 5,4, 5, 6, 7, 8, 9,8, 9, 10, 11, 12, 13,12, 13, 14, 15, 16, 17,16, 17, 18, 19, 20, 21,20, 21, 22, 23, 24, 25,24, 25, 26, 27, 28, 29,28, 29, 30, 31, 32, 1 };void E_extend(int *data_32,int* data_48)
{for (int i = 0; i < 48; i++){data_48[i] = data_32[E[i] - 1];}
}// 置换函数P,32位 -> 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的密钥kfor (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-1PC_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-2PC_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;}
}
结果如下: