#include "des.h"
#include "bitvec.h"
#include <string.h>
static unsigned 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
};
static unsigned int Shift_Array[] =
{
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};
static unsigned 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,
};
static unsigned 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,
};
static unsigned 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,
};
static char S[][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,
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,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,
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,
};
static unsigned 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,
};
static unsigned int FP[] =
{
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,
};
struct des_t
{
BitVec<48> subkey[16];
};
static void des_transform_s(BitVec<48> & bv48)
{
BitVec<6> bv6;
for (int i = 0; i < 8; i++)
{
BitCopy(bv6, bv48, i*6, 6, 0);
char index1 = 0, index2 = 0;
index1 |= bv6.GetBit(0) ? 0x1 : 0x0;
index1 |= bv6.GetBit(5) ? 0x2 : 0x0;
index2 |= bv6.GetBit(1) ? 0x1 : 0x0;
index2 |= bv6.GetBit(2) ? 0x2 : 0x0;
index2 |= bv6.GetBit(3) ? 0x4 : 0x0;
index2 |= bv6.GetBit(4) ? 0x8 : 0x0;
bv6.SetBits(&S[i][index1][index2], 6);
BitCopy(bv48, bv6, 0, 6, i*6);
}
}
static void des_encrypt_block(struct des_t *des, BitVec<64> &block64, bool encrypt)
{
BitVec<64> block64_perm;
BitVec<32> block32_l, block32_r, p32, temp32;
BitVec<48> block48_l, block48_r;
BitPermutate(block64_perm, block64, IP, sizeof(IP)/sizeof(IP[0]));
BitCopy(block32_l, block64_perm, 0, 32, 0);
BitCopy(block32_r, block64_perm, 32, 32, 0);
for (int i = 0; i < 16; i++)
{
BitPermutate(block48_r, block32_r, E, sizeof(E)/sizeof(E[0]));
if (encrypt)
BitXOR(block48_r, block48_r, des->subkey[i]);
else
BitXOR(block48_r, block48_r, des->subkey[15-i]);
des_transform_s(block48_r);
BitPermutate(p32, block48_r, P, sizeof(P)/sizeof(P[0]));
BitCopy(temp32, block32_r, 0, 32, 0);
BitXOR(block32_r, p32, block32_l);
BitCopy(block32_l, temp32, 0, 32, 0);
}
BitCopy(block64_perm, block32_r, 0, 32, 0);
BitCopy(block64_perm, block32_l, 0, 32, 32);
BitPermutate(block64, block64_perm, FP, sizeof(FP)/sizeof(FP[0]));
}
struct des_t* des_alloc()
{
return new des_t();
}
void des_free(struct des_t *des)
{
delete des;
}
void des_set_key(struct des_t *des, char *key, unsigned int key_len)
{
char dupkey[8] = {0};
if (key_len < 8)
{
memcpy(dupkey, key, key_len);
}
else
{
memcpy(dupkey, key, 8);
}
BitVec<64> keyvec64(dupkey, 64);
BitVec<56> keyvec56;
BitVec<28> subkey[2];
BitPermutate(keyvec56, keyvec64, PC_1, sizeof(PC_1)/sizeof(PC_1[0]));
BitCopy(subkey[0], keyvec56, 0, 28, 0);
BitCopy(subkey[1], keyvec56, 28, 28, 0);
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 2; j++)
subkey[j].RotateLeft(Shift_Array[i]);
BitConcat(keyvec56, subkey, 2);
BitPermutate(des->subkey[i], keyvec56, PC_2, sizeof(PC_2)/sizeof(PC_2[0]));
}
}
void des_encrypt(struct des_t *des, const char *plain, unsigned int plain_len)
{
BitVec<64> block64;
for (; plain_len>=8; plain_len-=8, plain+=8)
{
block64.SetBits(plain, 64);
des_encrypt_block(des, block64, true);
block64.DumpHex();
//des_encrypt_block(des, block64, false);
//block64.DumpStr();
}
if (plain_len > 0)
{
assert(plain_len < 8);
block64.SetBits(plain, plain_len*8);
des_encrypt_block(des, block64, true);
block64.DumpHex();
//des_encrypt_block(des, block64, false);
//block64.DumpStr();
}
}
void des_decrypt(struct des_t *des, const char *encrypt, unsigned int encrypt_len)
{
}
fwijqiut
- 粉丝: 0
- 资源: 3
最新资源
- 【毕业设计】matlab植物虫害检测的系统源码.zip
- 【毕业设计】Python 图形化麻将游戏 带蒙特卡洛AI源码.zip
- 【毕业设计】matlab做的一个水果分拣系统带有人机交互界面源码.zip
- 【毕业设计】MATLBA小波变换图像拼接要求两个图像需要有重叠区域源码.zip
- 桁架结构有限元程序的一般过程,利用matlab有限元来计算二维桁架的固有频率
- 【毕业设计】Python的pyqt5写的图书管理系统源码+说明文档+运行说明.zip
- 【毕业设计】SLAM-单目3D物体检测+SLAM算法实现源码.zip
- 【毕业设计】SLAM-利用点+线特征计算立体视觉SLAM算法源码.zip
- 跨年烟花代码-javascript
- 【毕业设计】YOLOv8-使用ONNX+YOLOv8+Python实现目标检测.zip
- 鼠笼式异步电机simulink仿真
- 【毕业设计】yolov9 瑞芯微 rknn 板端 C++部署源码+部署说明.zip
- 【毕业设计】YOLOv9 QT+NCNN实现安卓端部署源码+部署步骤+演示apk.zip
- 低秩矩阵分解代码 用于图像、信号等杂波去除 Matlab实现 算法较新,实现效果好
- python简易聊天室代码
- 【毕业设计】动态语义SLAM-目标检测+VSLAM+光流+多视角几何动态物体检测源码+octomap地图+目标数据库.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈