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

📄 des.cpp

📁 该代码实现跨平台的des加密解密功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}; 

/* 
* This encryption function is fairly clever in the way it does its 
* s-box lookup. The S-boxes are indexed by bytes, rather than 
* words, because that's faster on many machines, and shifting 
* everything two bits to do the multiply by 4 is trivial. 
* Then, the indexing into the various S boxes is done by 
* adding the appropriate offset bits into the key array, so the 
* addition is done by the XOR with the key rather than having to 
* be done explicitly here. 
*/ 

void desDES (byte const inblock[8], byte outblock[8], WORD32 const *keys) 
{ 
    WORD32 s, t, right, leftt; 
    int round; 
    
    leftt    = (inblock[0] << 24) 
                | (inblock[1] << 16) 
                | (inblock[2] << 8) 
                | inblock[3]; 
    right    = (inblock[4] << 24) 
                | (inblock[5] << 16) 
                | (inblock[6] << 8) 
                | inblock[7]; 

    /* Initial permutation IP */ 
    t = ((leftt >> 4) ^ right) & 0x0f0f0f0fL; 
    right ^= t; 
    leftt ^= (t << 4); 
    t = ((leftt >> 16) ^ right) & 0x0000ffffL; 
    right ^= t; 
    leftt ^= (t << 16); 
    t = ((right >> 2) ^ leftt) & 0x33333333L; 
    leftt ^= t; 
    right ^= (t << 2); 
    t = ((right >> 8) ^ leftt) & 0x00ff00ffL; 
    leftt ^= t; 
    right ^= (t << 8); 
    leftt = ((leftt >> 1) | (leftt << 31)); 
    t = (leftt ^ right) & 0x55555555L; 
    leftt ^= t; 
    right ^= t; 
    right = ((right >> 1) | (right << 31)); 

    for (round = 0; round < 8; round++) 
    { 
        s = (right & 0xfcfcfcfc) ^ keys[0]; 
        t = (((right >> 28) | (right << 4)) & 0xfcfcfcfc) ^ keys[1]; 
        leftt ^= *(WORD32 *)((char *)SP0+WORD32::getactualvalue(( s & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP0+WORD32::getactualvalue(((s >> 8 ) & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP0+WORD32::getactualvalue(((s >> 16) & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP0+WORD32::getactualvalue(((s >> 24) & 0x0fc))) 
            ^ *(WORD32 *)((char *)SP1+WORD32::getactualvalue(( t & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP1+WORD32::getactualvalue(((t >> 8 ) & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP1+WORD32::getactualvalue(((t >> 16) & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP1+WORD32::getactualvalue(((t >> 24) & 0x0fc))); 
        s = (leftt & 0xfcfcfcfc) ^ keys[2]; 
        t = (((leftt >> 28) | (leftt << 4)) & 0xfcfcfcfc) ^ keys[3]; 
        right ^= *(WORD32 *)((char *)SP0+WORD32::getactualvalue(( s & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP0+WORD32::getactualvalue(((s >> 8 ) & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP0+WORD32::getactualvalue(((s >> 16) & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP0+WORD32::getactualvalue(((s >> 24) & 0x0fc))) 
            ^ *(WORD32 *)((char *)SP1+WORD32::getactualvalue(( t & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP1+WORD32::getactualvalue(((t >> 8 ) & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP1+WORD32::getactualvalue(((t >> 16) & 0x3fc))) 
            ^ *(WORD32 *)((char *)SP1+WORD32::getactualvalue(((t >> 24) & 0x0fc))); 
        keys += 4; 
    }
    /* Inverse IP */ 
    leftt = ((leftt << 1) | (leftt >> 31)); 
    t = (leftt ^ right) & 0x55555555L; 
    leftt ^= t; 
    right ^= t; 
    right = ((right << 1) | (right >> 31)); 
    t = ((leftt >> 8) ^ right) & 0x00ff00ffL; 
    right ^= t; 
    leftt ^= (t << 8); 
    t = ((leftt >> 2) ^ right) & 0x33333333L; 
    right ^= t; 
    leftt ^= (t << 2); 
    t = ((right >> 16) ^ leftt) & 0x0000ffffL; 
    leftt ^= t; 
    right ^= (t << 16); 
    t = ((right >> 4) ^ leftt) & 0x0f0f0f0fL; 
    leftt ^= t; 
    right ^= (t << 4); 
    outblock[0] = *(byte*)(right >> 24); 
    outblock[1] = *(byte*)(right >> 16); 
    outblock[2] = *(byte*)(right >> 8); 
    outblock[3] = *(byte*)(right ); 
    outblock[4] = *(byte*)(leftt >> 24); 
    outblock[5] = *(byte*)(leftt >> 16); 
    outblock[6] = *(byte*)(leftt >> 8); 
    outblock[7] = *(byte*)(leftt      ); 
}

#undef WORD32 
#undef byte 

/****************************************************** 
The Below Code (Package Coder) is Writen In "C++" 
*******************************************************/ 
void CPackage_Encoder::SetKey(const unsigned char* password)
{ 
    unsigned i=0; 
    SBitBlock key; 
    key.dword.low=0x476226E8; 
	key.dword.high=0x87F5F4AD; 

    
    while(password[i])
    {
        key[i%8]^=(password[i]+(i>>3)+37); 
        i++;
    }
    theDEScoder.SetKey(key);
    SetSeed(0x31A68E0D,0x6508CB93);
}

int CPackage_Encoder::Encrypt(unsigned char destination[], const unsigned char source[], unsigned sourcesize) const
{
    unsigned i,j; 
    SBitBlock a,b; 
    theDEScoder.Encrypt(a,seed); 
    for(i=0;i<8;i++)
        destination[i]=a[i]; 
    a.dword.low=seed.dword.low; 
    a.dword.high=seed.dword.high; 
    for(i=0,j=8;i<sourcesize;i++,j++)
    {
        if(i%8==0)
        {
            a.dword.low+=23; 
            a.dword.high-=71; 
            theDEScoder.Encrypt(b,a); 
        }
        destination[j]=source[i]^b[i%8]; 
    }
    return 0; 
}

int CPackage_Encoder::Decrypt(unsigned char destination[],const unsigned char source[], unsigned sourcesize)
{
    unsigned i,j; 
    SBitBlock a,b; 
    if(sourcesize<8)
        return -201;    //Too Small Package 
    for(i=0;i<8;i++)
        a[i]=source[i]; 
    theDEScoder.Decrypt(seed,a); 
    a.dword.low=seed.dword.low; 
    a.dword.high=seed.dword.high; 
    for(i=0,j=8;j<sourcesize;i++,j++)
    { 
        if(i%8==0)
        { 
            a.dword.low+=23; 
            a.dword.high-=71; 
            theDEScoder.Encrypt(b,a); 
        } 
        destination[i]=source[j]^b[i%8]; 
    }
    return 0; 
}

int CPackage_Encoder::encode(unsigned char* scr,int nslen,unsigned char**des,int* ndlen )
{
	*ndlen=nslen+8;
	*des=new unsigned char[*ndlen];
	Encrypt(*des,scr,nslen);
	return 1;

}
int CPackage_Encoder::decode(unsigned char* scr,int nslen,unsigned char**des,int* ndlen )
{
	if (nslen<8) return -1;
	*ndlen=nslen-8;
	unsigned char* temp=new unsigned char[nslen+8];
	for(int nloop=0;nloop<nslen;nloop++)
	{
		temp[nloop]=scr[nloop];
	}
	*des=new unsigned char[nslen];
	Decrypt(*des,temp,nslen);
	delete []temp; 
	
	return 1;
}


/*
CString CPackage_Encoder::SEncrypt(CString s)
{
    int z = s.GetLength();
    CString sdet;
    unsigned char * src = new unsigned char[z];
    for(int i=0;i<z;i++)
        src[i] = s[i];
    
    unsigned char * det = new unsigned char[z+8];

    Encrypt(det,src, z); 
    
    for(i=0;i<z+8;i++)
        sdet += det[i];
    
    delete[] det;
    delete[] src;
    
    return sdet;
}

CString CPackage_Encoder::SDecrypt(CString s)
{
    int z = s.GetLength();
    CString sdet;

    unsigned char * src = new unsigned char[z+8];
    for(int i=0;i<z;i++)
        src[i] = s[i];
    
    unsigned char * det = new unsigned char[z];
    Decrypt(det,src, z); 
    
    for(i=0;i<z-8;i++)
        sdet += det[i];
    
    delete[] det;
    delete[] src;
    
    return sdet;
}
*/
inline void CDES_Encoder::SetKey(const SBitBlock& key)
{ 
    deskey((unsigned char const*)(&key),0,(WORD32 *)encrypt_k); 
    deskey((unsigned char const*)(&key),1,(WORD32 *)decrypt_k); 
}

inline void CDES_Encoder::Encrypt(SBitBlock& destination, const SBitBlock& source)const
{ 
    desDES((unsigned char const*)(&source),(unsigned char*)(&destination),(WORD32 *)encrypt_k); 
}

inline void CDES_Encoder::Decrypt(SBitBlock& destination, const SBitBlock& source)const
{ 
    desDES((unsigned char const*)(&source),(unsigned char*)(&destination),(WORD32 *)decrypt_k); 
}


inline void CPackage_Encoder::SetSeed(unsigned long lo,unsigned long hi)
{ 
    seed.dword.low=lo; 
    seed.dword.high=hi; 
}

inline void CPackage_Encoder::GetSeed(unsigned long& lo,unsigned long& hi)const
{ 
   // lo=seed.dword.low.byte[3]; 
    //hi=seed.dword.high; 
}

⌨️ 快捷键说明

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