⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des.cpp

📁 用C++方式编写的DES算法,是一个不错的源代码哟,适和对C++有一定认识的人
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include <memory.h> 
#include "des.h" 

int DES::encrypt ( unsigned char key[8], unsigned char* data, int blocks ) 
{ 
if ((!data)||(blocks<1)) 
return 0; 
deskey ( (unsigned char *)key, ENCRYPT ); 
des ( (unsigned char *)data, (unsigned char *)data, blocks); 
return 1; 
}; 

int DES::decrypt ( unsigned char key[8], unsigned char* data, int blocks )
{ 
if ((!data)||(blocks<1)) 
return 0; 
deskey ( (unsigned char *)key, DECRYPT ); 
des ( (unsigned char *)data, (unsigned char *)data, blocks); 
return 1; 
}; 

int DES::yencrypt ( unsigned char key[8], unsigned char* data, int size )
{ 
if ((!data)||(size<1)) 
return 0; 

// The last char of data is bitwise complemented and filled the rest 
// buffer.If size is 16, it will extend to 24,and 17 still 24. 
char lastChar = *(data+size-1); 
int blocks = size/8+1; 
memset (data+size, ~lastChar, blocks*8-size); 
deskey ( (unsigned char *)key, ENCRYPT ); 
return encrypt ( data, data, blocks); 
}; 

int DES::ydecrypt ( unsigned char key[8], unsigned char* data, int blocks, int* size ) 
{ 
if ( (!data) || (blocks<1) ) 
return 0; 

deskey ( (unsigned char *)key, DECRYPT ); 
if ( !decrypt ( data, data, blocks) ) 
return 0; 
if ( size != 0 ) 
{ 
int pos = blocks*8-1; 
char endChar = data[pos]; 
while ((pos>0)&&(data[pos]==endChar)) 
pos--; 
if ( data[pos] != ~endChar ) 
return 0; 
*size = pos+1; 
} 
return 1; 
}; 

// ----------------------------------------------------------------------- 
// des 
// Encrpts/Decrypts(according to the key currently loaded int the 
// internal key register) SOME blocks of eight bytes at address 'in' 
// into the block at address 'out'. They can be the same. 
// 
// "in" 
// "out" 
// "block" Number of blocks. 
// ----------------------------------------------------------------------- 
void DES::des ( unsigned char* in, unsigned char* out, int blocks ) 
{ 
for (int i = 0; i < blocks; i++,in+=8,out+=8) 
des_block(in,out); 
}; 

// ----------------------------------------------------------------------- 
// des_block 
// Encrpts/Decrypts(according to the key currently loaded int the 
// internal key register) one block of eight bytes at address 'in' 
// into the block at address 'out'. They can be the same. 
// 
// "in" 
// "out" 
// ----------------------------------------------------------------------- 
void DES::des_block(unsigned char *in, unsigned char *out) 
{ 
unsigned long work[2]; 

scrunch(in, work); 
desfunc(work, KnL); 
unscrun(work, out); 
} 

// ---------------------------------------------------------------------- 
// deskey 
// Sets the internal key register (KnR) according to the hexadecimal 
// key contained in the 8 bytes of hexkey, according to the DES, 
// for encryption or decrytion according to MODE 
// 
// "key" is the 64 bits key. 
// "md" means encryption or decryption. 
// ---------------------------------------------------------------------- 
void DES::deskey(unsigned char key[8], Mode md) /* Thanks to James Gillogly & Phil Karn! */ 
{ 
register int i, j, l, m, n; 
unsigned char pc1m[56], pcr[56]; 
unsigned long kn[32]; 

for (j = 0; j < 56; j++) { 
l = pc1[j]; 
m = l & 07; 
pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1:0; 
} 
for (i = 0; i < 16; i++) { 
if (md == DECRYPT) m = (15 - i) << 1; 
else m = i << 1; 
n = m + 1; 
kn[m] = kn[n] = 0L; 
for (j = 0; j < 28; j++) { 
l = j + totrot[i]; 
if (l < 28) pcr[j] = pc1m[l]; 
else pcr[j] = pc1m[l - 28]; 
} 
for (j = 28; j < 56; j++) { 
l = j + totrot[i]; 
if (l < 56) pcr[j] = pc1m[l]; 
else pcr[j] = pc1m[l - 28]; 
} 
for (j = 0; j < 24; j++) { 
if (pcr[ pc2[j] ]) kn[m] |= bigbyte[j]; 
if (pcr[ pc2[j+24] ]) kn[n] |= bigbyte[j]; 
} 
} 
cookey(kn); 
return; 
} 

// ---------------------------------------------------------------------- 
// cookey 
// Only called by deskey. 
// ----------------------------------------------------------------------- 
void DES::cookey(register unsigned long *raw1) 
{ 
register unsigned long *cook, *raw0; 
unsigned long dough[32]; 
register int i; 

cook = dough; 
for (i = 0; i < 16; i++, raw1++) { 
raw0 = raw1++; 
*cook = (*raw0 & 0x00fc0000L) << 6; 
*cook |= (*raw0 & 0x00000fc0L) << 10; 
*cook |= (*raw1 & 0x00fc0000L) >> 10; 
*cook++ |= (*raw1 & 0x00000fc0L) >> 6; 
*cook = (*raw0 & 0x0003f000L) << 12; 
*cook |= (*raw0 & 0x0000003fL) << 16; 
*cook |= (*raw1 & 0x0003f000L) >> 4; 
*cook++ |= (*raw1 & 0x0000003fL); 
} 
usekey(dough); 
return; 
} 

// ---------------------------------------------------------------------- 
// usekey 
// Only called by cookey. 
// Loads the interal key register with the data in cookedkey. 
// ----------------------------------------------------------------------- 
void DES::usekey(register unsigned long *from) 
{ 
register unsigned long *to, *endp; 

to = KnL, endp = &KnL[32]; 
while (to < endp) *to++ = *from++; 
return; 
} 

void DES::scrunch(register unsigned char *outof, register unsigned long *into ) 
{ 
*into = (*outof++ & 0xffL) << 24; 
*into |= (*outof++ & 0xffL) << 16; 
*into |= (*outof++ & 0xffL) << 8; 
*into++ |= (*outof++ & 0xffL); 
*into = (*outof++ & 0xffL) << 24; 
*into |= (*outof++ & 0xffL) << 16; 
*into |= (*outof++ & 0xffL) << 8; 
*into |= (*outof & 0xffL); 
return; 
} 

void DES::unscrun(register unsigned long *outof, register unsigned char *into) 
{ 
*into++ = (*outof >> 24) & 0xffL; 
*into++ = (*outof >> 16) & 0xffL; 
*into++ = (*outof >> 8) & 0xffL; 
*into++ = *outof++ & 0xffL; 
*into++ = (*outof >> 24) & 0xffL; 
*into++ = (*outof >> 16) & 0xffL; 
*into++ = (*outof >> 8) & 0xffL; 
*into = *outof & 0xffL; 
return; 
} 

void DES::desfunc(register unsigned long *block,register unsigned long *keys) 
{ 
register unsigned long fval, work, right, leftt; 
register int round; 

leftt = block[0]; 
right = block[1]; 
work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL; 
right ^= work; 
leftt ^= (work << 4); 
work = ((leftt >> 16) ^ right) & 0x0000ffffL; 
right ^= work; 
leftt ^= (work << 16); 
work = ((right >> 2) ^ leftt) & 0x33333333L; 
leftt ^= work; 
right ^= (work << 2); 
work = ((right >> 8) ^ leftt) & 0x00ff00ffL; 
leftt ^= work; 
right ^= (work << 8); 
right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL; 
work = (leftt ^ right) & 0xaaaaaaaaL; 
leftt ^= work; 
right ^= work; 
leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL; 

for (round = 0; round < 8; round++) { 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -