📄 desprocess.cpp
字号:
//the implementation of the DESProcess class
//construct and desconstruct of the DESProcess
DESProcess::DESProcess()
{
//give defalute value to every member
for(int i = 0; i < 8; i++)
mch[i] = '#';
for(i = 0; i < 64; i++)
{
bits[i] = false;
key[i] = false;
constkey[i] = false;
if(i < 48)
tkey[i] = false;
}
right = left = 0;
}
DESProcess::~DESProcess()
{
}
void DESProcess::setBits(const unsigned int& r, const unsigned int& l)
{
right = r;
left = l;
}
void DESProcess::setKey(const unsigned int& r, const unsigned int& l)
{
right = r;
left = l;
intToBit();
for(int i = 0; i < 64; ++i)
constkey[i] = bits[i];
}
void DESProcess::desEnCode()
{
intToBit();
desCoding(true);
bitToInt();
}
void DESProcess::desDeCode()
{
intToBit();
desCoding(false);
bitToInt();
}
void DESProcess::outputBits(unsigned int& r, unsigned int& l)
{
bitToInt();
r = right;
l = left;
}
void DESProcess::intToBit()
{
unsigned int con = 0x80000000; //the power of 2;
int i; //circle variable;
for(i = 0; i < 32; ++i)
{
if((con & right) != 0)
bits[i] = true;
else
bits[i] = false;
con >>= 1;
}
con = 0x80000000;
for(; i < 64; ++i)
{
if((con & left) != 0)
bits[i] = true;
else
bits[i] = false;
con >>= 1;
}
}
void DESProcess::bitToInt()
{
int i;
right = 0;
for(i = 0; i < 32; ++i)
{
right <<= 1;
if(bits[i])
right += 1;
}
left = 0;
for(; i < 64; ++i)
{
left <<= 1;
if(bits[i])
left += 1;
}
}
//descoding process
void DESProcess::desCoding(bool enCoding)
{
ipBits();
int i;//cicle variable
for(i = 0; i < 64; ++i)
key[i] = constkey[i];
pc_1Key();
int sum = 0;
for(i = 0; i < 16; ++i)
sum += ls[i];
if(! enCoding)
lsKey(sum);
else
lsKey(ls[0]);
for(i = 0; i < 16; ++i)
{
BitsToKey();
pc_2Key();
sBoxKey();
pKey();
DiffBits();
if(i < 15)
{
if(enCoding)
lsKey(ls[i + 1]);
else
lsKey(0 - ls[15 - i]);
reverseBits();
}
}
fpBits();
}
//IP Permute
void DESProcess::ipBits()
{
bool temp[64];
for(int i = 0; i < 64; ++i)
temp[i] = bits[i];
for(i = 0; i < 64; ++i)
bits[i] = temp[ip[i] - 1];
}
//exchange the front and tail
void DESProcess::reverseBits()
{
bool temp[32];
for(int i = 0; i < 32; ++i)
{
temp[i] = bits[i];
bits[i] = bits[32 + i];
bits[32 + i] = temp[i];
}
}
//Bits and tkey different or
void DESProcess::DiffBits()
{
for(int i = 0; i < 32; ++i)
bits[i] = ( (! bits[i]) && tkey[i]) || ( (! tkey[i]) && bits[i]);
}
//ip's contradictory permulate
void DESProcess::fpBits()
{
bool temp[64];
for(int i = 0; i < 64; ++i)
temp[i] = bits[i];
for(i = 0; i < 64; ++i)
bits[i] = temp[fp[i] - 1];
}
//pc-1 permulate
void DESProcess::pc_1Key()
{
bool temp[64];
for(int i = 0; i < 64; ++i)
temp[i] = key[i];
for(i = 0; i < 56; ++i)
key[i] = temp[pc1[i] - 1];
}
//left shift the key with parameter length
void DESProcess::lsKey(int length)
{
bool temp[28];
int i;//circle variable
length = length + 28;
for(i = 0; i < 28; ++i)
temp[i] = key[i];
for(i = 0; i < 28; ++i)
key[i] = temp[(i + length) % 28];
for(i = 0; i < 28; ++i)
temp[i] = key[28 + i];
for(i = 0; i < 28; ++i)
key[28 + i] = temp[(i + length) % 28];
}
//expand the right part to the tkey
void DESProcess::BitsToKey()
{
for(int i = 0; i < 48; ++i)
tkey[i] = bits[31 + e[i]];
}
//pc_2 permulate and different or
void DESProcess::pc_2Key()
{
for(int i = 0; i < 48; ++i)
tkey[i] = ((! tkey[i]) && key[pc2[i] - 1]) || ((! key[pc2[i] - 1]) && tkey[i]);
}
//sBox permulate the key
void DESProcess::sBoxKey()
{
const int pow_2[4] = {1, 2, 4, 8};
int i,j;
bool temp[48];
for(i = 0; i < 48; ++i)
temp[i] = tkey[i];
int tempint;
for(i = 0; i < 8; ++i)
{
tempint = 0;
for(j = 0; j < 4; ++j)
if(temp[i * 6 + 4 - j])
tempint += pow_2[j];
if(temp[i * 6 + 5])
tempint += 16;
if(temp[i*6])
tempint += 32;
tempint = sbox[i][tempint];
for(j = 0; j < 4; ++j) //number change to tkey
tkey[i * 4 + j] = ((tempint & pow_2[3 - j]) != 0);
} //end of for{}
}
//p permulate
void DESProcess::pKey()
{
bool temp[32];
for(int i = 0; i < 32; ++i)
temp[i] = tkey[i];
for(i = 0; i < 32; ++i)
tkey[i] = temp[ p[i] - 1];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -