⚡️ 欢迎来到 注意安全CL 的博客主页 🌷
🙋 我的专栏推荐 🙋
🌱《软件安装与资源共享》分享好用的工具软件,条理清晰资源多多🍗,欢迎来访🐇
🌱《信息安全-初级网络》有全面的网络基础知识分享,欢迎来访🙉
🌱《信息安全-高级网络》涉及高级网络技术知识,实验案例部署配置,快速提升🐲
🌱《信息安全-渗透测试》有超全漏洞复现、攻击方式、渗透工具分享欧~💦
🌱《信息安全-密码学》校内布置的剪短算法练习,帮助你更快爱上这一学科 🌺
目录
一、DES算法和CBC模式的介绍
DES(Data Encryption Standard,数据加密标准)是一种基于密钥的对称加密算法,而CBC(Cipher Block Chaining,密码块链接模式)是一种使用初始化向量(IV)和前一个块的加密结果来加密当前块的模式。
二、代码的主要组成部分及其功能
-
密钥和参数定义:代码开始部分定义了多个全局数组,包括置换表、E盒、P盒、置换表等,这些都是DES算法中的关键部分。
-
辅助函数:
get(int r)
:根据轮数返回移位数量。yihuo(int* a, int* b, int num)
:执行两个数组的按位异或操作。fuck(int a[64], int b[64], int n)
:将数组b
的值复制到数组a
。left_move(int s[28], int s1[28], int n)
:对数组进行左移操作。exchange(int* a, int* b, int* biao, int n)
:根据置换表对数组进行置换。erzhuanshi(int* a, unsigned char* b, int n)
:将二进制数组转换为十六进制表示。shizhuaner(int* a, int x, int i, int n)
:将十进制数转换为二进制数组。
-
DES算法核心函数:
s_box(int a[32], int b[48])
:S盒替换函数。DES(unsigned char a[8], unsigned char b[8], unsigned char plus3[8],int n,unsigned char IV[8],unsigned char b1[8])
:DES加密函数,其中a
是待加密的8字节数据块,b
是密钥,plus3
是加密结果,n
是数据块编号,IV
是初始化向量,b1
是上一轮的密文。
-
CBC模式加密函数:
CBC(unsigned char b[8],unsigned char IV[8])
:实现CBC模式的加密过程,读取文件中的明文,使用DES算法进行加密,并将结果写入新文件。
-
CBC模式解密函数:
RE_DES(unsigned char a[8], unsigned char min_plus[8],int n,unsigned char IV[8],unsigned char b1[8])
:DES解密函数。RE_CBC(unsigned char b[8], int size,unsigned char IV[8])
:实现CBC模式的解密过程,读取加密文件,使用DES算法进行解密,并将结果写入新文件。
-
main函数:程序入口,接收用户输入的密钥和初始向量,调用CBC模式加密和解密函数,最后验证解密结果是否与原文一致。
三、源代码附上
CBC代码简单举例。
# CBC代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
using namespace std;
int key_3[17][48];
int zhihuan2[48] = { 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 }; //置换2
int e[48] = { 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 }; //E盒
int p[32] = { 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 }; //P盒
int zhihuan1[56] = { 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 }; //置换1
int IP[64] = { 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 }; //IP置换
int IP_1[64] = { 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 }; //IP_1置换
int get(int r) //得到第r轮的移位数
{
int i;
if (r == 1 || r == 2 || r == 9 || r == 16) {
i = 1;
}
else {
i = 2;
}
return i;
}
void yihuo(int* a, int* b, int num) //异或
{
int i;
for (i = 0; i < num; i++)
{
if (a[i] == b[i]) {//相同为0
a[i] = 0;
}
else {//不同为1
a[i] = 1;
}
}
}
void fuck(int a[64], int b[64], int n) {
for (int i = 0; i < n; i++) {
a[i] = b[i];
}
}
void left_move(int s[28], int s1[28], int n) //左移
{
int i, t, temp;
fuck(s, s1, 28);
for (i = 0; i < n; i++) {
temp = s[0];
for (t = 0; t < 27; t++) {
s[t] = s[t + 1];
}
s[27] = temp;//原先的左半部分第一位赋给最后一位
}
}
void exchange(int* a, int* b, int* biao, int n) //置换
{
int i, loc;
for (i = 0; i < n; i++)//输入的第biao[i]位是输出的第i位
{
loc = biao[i];
b[i] = a[loc - 1];
}
return;
}
void erzhuanshi(int* a, unsigned char* b, int n)
// a是二进制数组,b是存放十六进制的数组,n是二进制数组的大小
{
int i, j;
n = n / 8;
for (i = 0; i < n; i++)
{
j = 128 * a[8 * i] + 64 * a[8 * i + 1] + 32 * a[8 * i + 2] + 16 * a[8 * i + 3] + 8 * a[8 * i + 4] + 4 * a[8 * i + 5] + 2 * a[8 * i + 6] + 1 * a[8 * i + 7];//先转换为十进制
b[i] = (char)j;//转换为char型字符
}
}
void shizhuaner(int* a, int x, int i, int n)
// a是存放二进制的数组,x是需要进行转换的十进制数,i是第i个十进制数,n是数组大小
{
int j; n = n / 8; j = n - 1;
do {
a[i * n + j] = x % 2;//x的2进制最后一位,0or1
x = x / 2;//右移
j--;
} while (x != 0);
while (j >= 0)//如果右移后出现000....的情况
{
a[i * n + j] = 0;
j--;
}
return;
}
void s_box(int a[32], int b[48]) //S盒
{
int i, r, c, s;
int s1[4][16] = { { 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[4][16] = { { 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[4][16] = { { 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[4][16] = { { 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, 3 },{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } },
s5[4][16] = { { 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[4][16] = { { 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][16] = { { 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[4][16] = { { 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 } };
for (i = 0; i < 8; i++)
{
r = 2 * b[6 * i] + b[6 * i + 5];//第一位和最后一位构建行
c = 8 * b[6 * i + 1] + 4 * b[6 * i + 2] + 2 * b[6 * i + 3] + b[6 * i + 4];//中间四位构建列
switch (i) //选择盒子
{
case 0:s = s1[r][c]; break;
case 1:s = s2[r][c]; break;
case 2:s = s3[r][c]; break;
case 3:s = s4[r][c]; break;
case 4:s = s5[r][c]; break;
case 5:s = s6[r][c]; break;
case 6:s = s7[r][c]; break;
case 7:s = s8[r][c]; break;
}
shizhuaner(a, s, i, 32); //转化为二进制
}
}
void DES(unsigned char a[8], unsigned char b[8], unsigned char plus3[8],int n,unsigned char IV[8],unsigned char b1[8])
{
int m_a[64] = { 0 };
int m_b[64]; //IP置换前后的明文
int s_a[64] = { 0 };
int s_b[56]; //置换1前后的二进制密钥
int IV_2[64],b1_2[64];//初始向量的二进制以及上一轮密文块的二进制
int r; //轮数
int miwen[8] = { 0 }; //最终密文
int i, j = 0;
int key_l[17][28];//16轮密钥
int key_r[17][28];
int key_2[17][56];
for (i = 0; i < 8; i++)
{
shizhuaner(m_a, (int)a[i], i, 64);
}
for (i = 0; i < 8; i++)
{
shizhuaner(s_a, (int)b[i], i, 64);
}
for (i = 0; i < 8; i++)
{
shizhuaner(IV_2, (int)IV[i], i, 64);
}
for (i = 0; i < 8; i++)
{
shizhuaner(b1_2, (int)b1[i], i, 64);
}
if (n == 0) {
yihuo(m_a, IV_2, 64);
}
else {
yihuo(m_a, b1_2, 64);
}
exchange(m_a, m_b, IP, 64); //明文进行初始置换
int min_l[17][32];
int min_r[17][32];
for (i = 0; i < 32; i++)//明文第0轮的左半部分
min_l[0][i] = m_b[i];
for (i = 32; i < 64; i++)//明文的右半部分
min_r[0][i - 32] = m_b[i];
exchange(s_a, s_b, zhihuan1, 56);//密钥进行PC-1置换
int run = 1;
for (int i = 0; i < 28; i++) {
key_l[0][i] = s_b[i];
key_r[0][i] = s_b[i + 28];
}
for (r = 1; r <= 16; r++) {
n = get(r);
left_move(key_l[run], key_l[run - 1], n);
left_move(key_r[run], key_r[run - 1], n);
run++;
}
//结合
for (r = 1; r <= 16; r++) {
for (i = 0; i < 28; i++) {
key_2[r][i] = key_l[r][i];
key_2[r][i + 28] = key_r[r][i];
}
}
//pc-2置换
for (r = 1; r <= 16; r++) {
exchange(key_2[r], key_3[r], zhihuan2, 48);
}
//真正的16轮密钥到此生成
int temp6[48], temp1[32];
for (int r = 1; r <= 16; r++) {
fuck(min_l[r], min_r[r - 1], 32);//左半部分等于上一轮右半部分
exchange(min_r[r - 1], temp6, e, 48);//E置换
yihuo(temp6, key_3[r], 48);//准备S盒置换
s_box(temp1, temp6);//s盒替换完毕
exchange(temp1, temp6, p, 32);//P置换
yihuo(temp6, min_l[r - 1], 32);
fuck(min_r[r], temp6, 32);
}
int plus1[64] = { 0 };
int plus2[64] = { 0 };
for (i = 0; i < 32; i++) {
plus1[i] = min_r[16][i];
plus1[i + 32] = min_l[16][i];
}
exchange(plus1, plus2, IP_1, 64);
erzhuanshi(plus2, plus3, 64);
//plus3是密文
}
void RE_DES(unsigned char a[8], unsigned char min_plus[8],int n,unsigned char IV[8],unsigned char b1[8]) {
//开始解密
int IV_2[64], b1_2[64];
int plus2[64], plus1[64], min_l[17][32], min_r[17][32];
for (int i = 0; i < 8; i++) {
shizhuaner(plus2, int(a[i]), i, 64);
}
for (int i = 0; i < 8; i++)//IV变为二进制
{
shizhuaner(IV_2, (int)IV[i], i, 64);
}
for (int i = 0; i < 8; i++)//上一轮的密文变为二进制
{
shizhuaner(b1_2, (int)b1[i], i, 64);
}
exchange(plus2, plus1, IP, 64);//IP_1置换
for (int i = 0; i < 32; i++) {
min_l[16][i] = plus1[i + 32];
min_r[16][i] = plus1[i];
}
cout << endl;
for (int r = 16; r >= 1; r--) {
fuck(min_r[r - 1], min_l[r], 32);//这一轮的左等于上一轮的右
exchange(min_r[r - 1], plus1, e, 48);//E扩展
yihuo(plus1, key_3[r], 48);//与密钥异或
s_box(plus2, plus1);//S盒置换
exchange(plus2, plus1, p, 32);//p盒置换
fuck(min_l[r - 1], plus1, 32);
yihuo(min_l[r - 1], min_r[r], 32);//异或
}
for (int i = 0; i < 32; i++) {
plus1[i + 32] = min_r[0][i];
plus1[i] = min_l[0][i];
}
exchange(plus1, plus2, IP_1, 64);;
//min_plus是明文
if (n == 0) {
yihuo(plus2, IV_2, 64);
}
else {
yihuo(plus2, b1_2, 64);
}
erzhuanshi(plus2, min_plus, 64);
}
int CBC(unsigned char b[8],unsigned char IV[8]) {
FILE* input = fopen("C:\\Users\\Administrator\\Desktop\\CBC.txt", "r");
FILE* output = fopen("C:\\Users\\Administrator\\Desktop\\CBC_1.txt", "a");
int size, size1;
unsigned char a[8], plus3[8], mi_temp[8] = { "0" };
fseek(input, 0, SEEK_END);
size = ftell(input);
fseek(input, 0, SEEK_SET);
for (int i = 0; i < size / 8; i++) {
fread(a, 8, 1, input);
DES(a, b, plus3,i,IV,mi_temp);
for (int j = 0; j < 8; j++) {//储存这一轮密文
mi_temp[j] = plus3[j];
}
fwrite(plus3, 8, 1, output);
}
size1 = size % 8;
fread(a, size1, 1, input);
for (int i = size1; i < 8; i++) {
a[size1] = '0';
}
DES(a, b, plus3,size/8,IV,mi_temp);
fwrite(plus3, 8, 1, output);
fclose(input);
fclose(output);
return size;
}
void RE_CBC(unsigned char b[8], int size,unsigned char IV[8]) {
FILE* input1 = fopen("C:\\Users\\Administrator\\Desktop\\CBC_1.txt", "r");
FILE* output1 = fopen("C:\\Users\\Administrator\\Desktop\\CBC_2.txt", "a");
unsigned char min_plus[8];
int size1;
unsigned char a[8],mi_temp[8];
fseek(input1, 0, SEEK_END);
size1 = ftell(input1);
fseek(input1, 0, SEEK_SET);
for (int i = 0; i < size / 8; i++) {
fread(a, 8, 1, input1);
RE_DES(a, min_plus,i,IV,mi_temp);
for (int j = 0; j < 8; j++) {//储存这一轮密文
mi_temp[j] = a[j];
}
fwrite(min_plus, 8, 1, output1);
}
size1 = 8 - (size1 - size);
fread(a, 8, 1, input1);
RE_DES(a, min_plus,size/8,IV,mi_temp);
fwrite(min_plus, size1, 1, output1);
fclose(input1);
fclose(output1);
}
int main() {
unsigned char b[8],IV[8];
int size;
cout << "请输入密钥" << endl;
for (int i = 0; i < 8; i++) {
cin >> b[i];
}
cout << "请输入初始向量" << endl;
for (int i = 0; i < 8; i++) {
cin >> IV[i];
}
size = CBC(b,IV);
RE_CBC(b, size,IV);
FILE* min = fopen("C:\\Users\\Administrator\\Desktop\\CBC_2.txt", "r ");
FILE* min1 = fopen("C:\\Users\\Administrator\\Desktop\\CBC.txt", "r");
char p, q;
int flag = 0;
for (int i = 0; i < size; i++) {
fread(&p, 1, 1, min);
fread(&q, 1, 1, min1);
if (p != q) {
flag = 1;
break;
}
}
if (flag == 0) {
cout << "解密成功";
}
else {
cout << "解密失败";
}
fclose(min);
fclose(min1);
return 0;
}