#include "memory.h"
#include "3des.h"
#include "stdio.h"
#include <stdlib.h>
/*********************************************************/
static void F_func(bool In[32], const bool Ki[48]);// f 函数
static void S_func(bool Out[32], const bool In[48]);// S 盒代替
static void Transform(bool *Out, bool *In, const char *Table, int len);// 变换
static void Xor(bool *InA, const bool *InB, int len);// 异或
static void RotateL(bool *In, int len, int loop);// 循环左移
static void ByteToBit(bool *Out, const char *In, int bits);// 字节组转换成位组
static void BitToByte(char *Out, const bool *In, int bits);// 位组转换成字节组
static bool SubKey[16][48];// 16圈子密钥
/*********************************************************/
/************************加密*********************************/
int Encrypt(char *Msg, char *Key, char *Cipher,int length) //如果消息长度不是8的倍数,末位补0凑成8的倍数。
{
if(length <=0)
{
return -1;
}
char keyarray1[8];
char keyarray2[8];
char keyarray3[8];
memcpy(&keyarray1, &Key[0], 8);
memcpy(&keyarray2, &Key[8], 8);
memcpy(&keyarray3, &Key[16], 8);
if (length%8==0)
{
int dst = length/8;
char out[8];
char in[8];
for (int j=0;j<dst;j++)
{
memcpy(&in[0], &Msg[8*j], 8);
encrypt(in, keyarray1, out);///////////////////
decrypt(out, keyarray2, in);//////////////////
encrypt(in, keyarray3, out);//////////////////
memcpy(&Cipher[8*j], &out, 8);
}
return 1;
}else
{
int ext = length/8;
int dst = length%8;
char * temp_in ;
char * temp_add;
temp_in = (char *) malloc((ext+1)*8);
temp_add = (char *)malloc(8-dst);
memcpy(&temp_in[0], &Msg[0], length);
memset(temp_add, 0, 8-dst);
memcpy(&temp_in[length],&temp_add[0], 8-dst);
int round = ext+1;
char out [8];
char in[8];
for (int j=0;j<round;j++)
{
memcpy(&in[0], &temp_in[8*j], 8);
encrypt(in, keyarray1, out);
decrypt(out, keyarray2, in);
encrypt(in, keyarray3, out);
memcpy(&Cipher[8*j], &out, 8);
}
return 1;
}
}
void encrypt(char In[8], const char Key[8], char Out[8])
{
Des_SetKey(Key);
static bool M[64], Tmp[32], *Li = &M[0], *Ri = &M[32];
ByteToBit(M, In, 64);
Transform(M, M, IP_Table, 64);
for(int i=0; i<16; i++)
{
memcpy(Tmp, Ri, 32);
F_func(Ri, SubKey[i]);
Xor(Ri, Li, 32);
memcpy(Li, Tmp, 32);
}
Transform(M, M, IPR_Table, 64);
BitToByte(Out, M, 64);
}
/************************解密*********************************/
int Decrypt(char *Msg, char *Key, char *Cipher,int length)//如果消息长度不是8的倍数,末位补0凑成8的倍数。
{
if(length <=0)
{
return -1;
}
char keyarray1[8];
char keyarray2[8];
char keyarray3[8];
memcpy(&keyarray1, &Key[0], 8);
memcpy(&keyarray2, &Key[8], 8);
memcpy(&keyarray3, &Key[16], 8);
if (length%8==0)
{
int dst = length/8;
char out [8];
char in[8];
for (int j=0;j<dst;j++)
{
memcpy(&in[0], &Msg[8*j], 8);
decrypt(in, keyarray3, out);
encrypt(out, keyarray2, in);
decrypt(in, keyarray1, out);
memcpy(&Cipher[8*j], &out, 8);
}
return 1;
}else
{
int ext = length/8;
int dst = length%8;
char * temp_in ;
char * temp_add;
temp_in = (char *) malloc((ext+1)*8);
temp_add = (char *)malloc(8-dst);
memcpy(&temp_in[0], &Msg[0], length);
memset(temp_add, 0, 8-dst);
memcpy(&temp_in[length],&temp_add[0], 8-dst);
int round = ext+1;
char out [8];
char in[8];
for (int j=0;j<round;j++)
{
memcpy(&in[0], &temp_in[8*j], 8);
decrypt(in, keyarray3, out);
encrypt(out, keyarray2, in);
decrypt(in, keyarray1, out);
memcpy(&Cipher[8*j], &out, 8);
}
return 1;
}
}
void decrypt(char In[8], const char Key[8], char Out[8])
{
Des_SetKey(Key);
static bool M[64], Tmp[32], *Li = &M[0], *Ri = &M[32];
ByteToBit(M, In, 64);
Transform(M, M, IP_Table, 64);
for(int i=15; i>=0; i--)
{
memcpy(Tmp, Li, 32);
F_func(Li, SubKey[i]);
Xor(Li, Ri, 32);
memcpy(Ri, Tmp, 32);
}
Transform(M, M, IPR_Table, 64);
BitToByte(Out, M, 64);
}
/********************设置密钥*****************************/
void Des_SetKey(const char Key[8])
{
static bool K[64], *KL = &K[0], *KR = &K[28];
ByteToBit(K, Key, 64);
Transform(K, K, PC1_Table, 56);
for(int i=0; i<16; i++) {
RotateL(KL, 28, LOOP_Table[i]);
RotateL(KR, 28, LOOP_Table[i]);
Transform(SubKey[i], K, PC2_Table, 48);
}
}
/*********************************************************/
void F_func(bool In[32], const bool Ki[48])
{
static bool MR[48];
Transform(MR, In, E_Table, 48);
Xor(MR, Ki, 48);
S_func(In, MR);
Transform(In, In, P_Table, 32);
}
void S_func(bool Out[32], const bool In[48])
{
for(char i=0,j,k; i<8; i++,In+=6,Out+=4) {
j = (In[0]<<1) + In[5];
k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4];
ByteToBit(Out, &S_Box[i][j][k], 4);
}
}
void Transform(bool *Out, bool *In, const char *Table, int len)
{
static bool Tmp[256];
for(int i=0; i<len; i++)
Tmp[i] = In[ Table[i]-1 ];
memcpy(Out, Tmp, len);
}
void Xor(bool *InA, const bool *InB, int len)
{
for(int i=0; i<len; i++)
InA[i] ^= InB[i];
}
void RotateL(bool *In, int len, int loop)
{
static bool Tmp[256];
memcpy(Tmp, In, loop);
memcpy(In, In+loop, len-loop);
memcpy(In+len-loop, Tmp, loop);
}
void ByteToBit(bool *Out, const char *In, int bits)
{
for(int i=0; i<bits; i++)
Out[i] = (In[i/8]>>(i%8)) & 1;
}
void BitToByte(char *Out, const bool *In, int bits)
{
memset(Out, 0, (bits+7)/8);
for(int i=0; i<bits; i++)
Out[i/8] |= In[i]<<(i%8);
}
/********************* end *********************************/
3DES加密算法C语言实现


wtbee
- 粉丝: 19
- 资源: 36
最新资源
- node-v18.19.0-x64.msi
- tensorflow-2.8.0-cp39-cp39-manylinux2010-x86-64.whl.zip
- C++医院管理系统实例.zip
- tensorflow-2.8.0-cp38-cp38-win-amd64.whl.zip
- tensorflow-2.8.0-cp38-cp38-manylinux2010-x86-64.whl.zip
- 产教融合平台解决方案(最新)
- c++最大子矩阵代码及分析
- 2_NISP模拟卷答案版本(10月17日更新).xls
- tensorflow-2.8.0-cp38-cp38-macosx-10-14-x86-64.whl.zip
- tensorflow-2.8.0-cp37-cp37m-win-amd64.whl.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
- 6
前往页