c++写的DES类
DES.h
#include<iostream>
#include<string>
typedef unsigned char byte;
using std::string;
class DES {
public:
byte IP_in[64];
byte IP_out[64];
byte E[48];
byte S[8][64];
byte PC[56], PC_out[48];
byte P[32];
byte bKey[8];
byte bSubKey[16][6];
string sIn;//输入
string sOut;//输出
byte * f(byte[], byte[]);
void KeyGeneration();//16个子密钥生成
void Encryption();//加密
void Decryption();//解密
int get_bit(byte[], int);//取得位
void set_bit(byte[], int, int);//设置位
void LLSL(byte[], int, int);//循环逻辑左移
byte* Xor(void *, void *,int );//异或
DES();
~DES();
};
DEScpp.cpp
#include"DES.h"
byte* DES::f(byte in[], byte key[]) {
//check lenght...
byte bExIn[8],bExKey[8];
for (int i = 0; i < 48; i++) {
set_bit(bExIn, (i/6)*8 + i % 6, get_bit(in, E[i]-1));
set_bit(bExKey, (i / 6) * 8 + i % 6, get_bit(key, i));//可优化 (i/6)*8不能化简
}//32->48
byte temp,T[4];
byte * bResult=new byte[4];
for (int i = 0; i < 8; i++) {
byte bT = bExIn[i] ^ bExKey[i];
int t = S[i][(((bT & 0x04) >> 2) + ((bT & 0x80) >> 6)) * 8 + ((bT & 0x78) >> 3)];
if (i % 2 == 0)temp = t & 0x0f;
else T[i / 2] = (temp << 4) + (t & 0x0f);
}//48->32
for (int i = 0; i < 32; i++)set_bit(bResult, i, get_bit(T, P[i]-1));//p transpostion
return bResult;
}//ok
int DES::get_bit(byte in[], int number) {
return (in[number / 8] >> (7-number % 8))&0x1;
}//ok
void DES::set_bit(byte in[], int number, int value) {
int a = number / 8, b = number % 8;
if (value)in[a] = in[a] | (1 << (7 - b));
else in[a] = in[a] & ~(1 << (7 - b));//devite 2 pieces;
}//ok
void DES::LLSL(byte in[], int number, int round) {
if (number < 0)return;
/*int a = number / 8, b = number % 8;
while (round--) {
int t = in[0] & 0x80;
for (int i = 0; i < a+1; i++) {
in[i] = in[i] << 1 + (in[i + 1]>>7)&0x01;
}
set_bit(in, number, t);
}*/
//笨比方法
int t = number / 8;
t=number % 8==0?t:t+1;
byte *temp = new byte[t];
memcpy(temp, in, t);
for (int i = 0; i < number; i++)set_bit(in, i, get_bit(temp, (i + round+1) % number - 1));
delete []temp;//应该还可以再优化
}//ok
byte * DES::Xor(void * c1, void *c2, int number) {
number = number/8+1;
while (number--)((byte*)c1)[number] ^= ((byte*) c2)[number];
return ((byte*)c1);
}
void DES::KeyGeneration() {
byte bT[8], bL[4],bR[4];
for (int i = 0; i < 28; i++)set_bit(bL, i, get_bit(bKey, PC[i]-1));
for (int i = 0; i < 28; i++)set_bit(bR, i, get_bit(bKey, PC[i+28]-1));//transpostion
for (int i = 0; i < 16; i++) {
if(i==0||i==1||i==8||i==15){
LLSL(bL, 28, 1);//循环逻辑左移...1.2.9.16
LLSL(bR, 28, 1);
}
else {
LLSL(bL, 28, 2);//循环逻辑左移...1.2.9.16
LLSL(bR, 28, 2);
}
memcpy(bT, bL, 4);
memcpy(bT + 4, bR, 4);
for (int j = 0; j < 4; j++) {
bT[3 + j] = bT[3 + j] & 0xf0 + ((bT[4 + j] & 0xf0) >> 4);
bT[4 + j] = ((bT[4 + j] & 0x0f) << 4);
}//连接两个3.5的byte,笨比方法,可以优化
for (int j = 0; j < 48; j++)set_bit(bSubKey[i], j, get_bit(bT, PC_out[j]-1));
}
}
void byte_print(byte a[], byte b[]) {
for (int i = 0; i < 4; i++)printf("%.2X ", a[i]);
for (int i = 0; i < 4; i++)printf("%.2X ", b[i]);
printf("\n");
}
void DES::Encryption() {
sOut.clear();
byte *bL, *bR,bT[8];
int lenght = sIn.size();
lenght = lenght % 8 == 0 ? lenght/8 : lenght/8 + 1;
byte *bIn = new byte[lenght*8];//不足补整
memcpy(bIn, sIn.data(), sIn.size());//string->byte
bL = new byte[4]; bR = new byte[4];
byte *temp = bR, *temp2;
for (int k = 0; k < lenght; k++) {
for (int i = 0; i < 32; i++)set_bit(bL, i, get_bit(bIn + 8 * k, IP_in[i]-1));
for (int i = 0; i < 32; i++)set_bit(bR, i, get_bit(bIn + 8 * k, IP_in[i+32]-1));//transpostion
for (int j = 0; j < 16; j++) {
temp = bR; temp2 = f(bR, bSubKey[j]);
bR=Xor(bL,temp2 ,32);//delete...
bL = temp;
//byte_print(bL, bR);//print
delete[]temp2;
}//16轮加密
memcpy(bT, bR, 4);
memcpy(bT + 4, bL, 4);
for (int i = 0; i < 32; i++)set_bit(bL, i, get_bit(bT, IP_out[i]-1));
for (int i = 0; i < 32; i++)set_bit(bR, i, get_bit(bT, IP_out[i + 32]-1));//reverse transpostion
sOut.append((char *)bL,4);
sOut.append((char*)bR,4);
}
delete[] bL, bR;
}
void DES::Decryption() {
sOut.clear();
byte *bL, *bR, bT[8];
int lenght = sIn.size();
lenght = lenght % 8 == 0 ? lenght/8 : lenght/8 + 1;
byte *bIn = new byte[lenght * 8 ];//不足补整
memcpy(bIn, sIn.data(), sIn.size());//string->byte
bL = new byte[4]; bR = new byte[4];
byte *temp = bR, *temp2;
for (int k = 0; k < lenght; k++) {
for (int i = 0; i < 32; i++)set_bit(bL, i, get_bit(bIn + 8 * k, IP_in[i]-1));
for (int i = 0; i < 32; i++)set_bit(bR, i, get_bit(bIn + 8 * k, IP_in[i + 32]-1));//transpostion
for (int j = 15; j >=0; j--) {
temp = bR; temp2 = f(bR, bSubKey[j]);
bR = Xor(bL, temp2, 32);//delete...
bL = temp;
//byte_print(bL, bR);//print
delete[]temp2;
}//16轮加密
memcpy(bT, bR, 4);
memcpy(bT + 4, bL, 4);
for (int i = 0; i < 32; i++)set_bit(bL, i, get_bit(bT, IP_out[i]-1));
for (int i = 0; i < 32; i++)set_bit(bR, i, get_bit(bT, IP_out[i + 32]-1));//reverse transpostion
sOut.append((char *)bL,4);
sOut.append((char*)bR,4);
}
delete[] bL, bR;
}
DES::DES() {
this->bKey;//给定密钥,可以加在带参构造函数里;
memset(bKey, 0, 8);//无参未设置前默认全0
byte IP_intemp[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 };
byte IP_outtemp[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 };
byte Etemp[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};
byte s1[64] = {
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
};
byte s2[64] = {
15,1,8,14,6,11,3,4,9,7,2,12,14,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
};
byte s3[64] = {
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,
01,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
};
byte s4[64] = {
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
};
byte s5[64] = {
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
};
byte s6[64] = {
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
};
byte s7[64] = {
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
};
byte s8[64] = {
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
};
byte Ptemp[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
};
byte PCtemp[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
};
byte PC_outtemp[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
};
memcpy(this->IP_in, IP_intemp, 64);
memcpy(this->IP_out, IP_outtemp, 64);
memcpy(this->E, Etemp, 48);
memcpy(this->S[0], s1, 64);
memcpy(this->S[1], s2, 64);
memcpy(this->S[2], s3, 64);
memcpy(this->S[3], s4, 64);
memcpy(this->S[4], s5, 64);
memcpy(this->S[5], s6, 64);
memcpy(this->S[6], s7, 64);
memcpy(this->S[7], s8, 64);
memcpy(this->P, Ptemp,32);
memcpy(this->PC, PCtemp, 56);
memcpy(this->PC_out, PC_outtemp, 48);
//this->KeyGeneration();
}
/*
byte IP_in[64];
byte IP_out[64];
byte E[48];
byte S[8][64];
byte PC[56], PC_out[48];*/
DES::~DES() {
}
main.cpp
#include"DES.h"
int main() {
DES des;
des.bKey[0] = 12;//密钥设置,懒得再写函数一起设置了
des.KeyGeneration(); //子密钥生成
des.sIn = "adsdasdsdasd45sd45as4ds5d4s5ds";
printf("%s\n", des.sIn.data());
des.Encryption();
printf("%s\n", des.sOut.data());
des.sIn = des.sOut;
des.Decryption();
printf("%s\n", des.sOut.data());
}