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

📄 des.cpp

📁 本代码是自己封装的MD5加密类
💻 CPP
📖 第 1 页 / 共 2 页
字号:
int hang[3];     //生成行号
hang[1]=yihuo[1];
hang[1]=hang[1]<<1;
hang[1]=hang[1]|yihuo[4];
hang[2]=yihuo[5];
hang[2]=hang[2]<<1;
hang[2]=hang[2]|yihuo[8];
int lie[3];       //生成列号
lie[1]=yihuo[2];
lie[1]=lie[1]<<1;
lie[1]=lie[1]|yihuo[3];
lie[2]=yihuo[6];
lie[2]=lie[2]<<1;
lie[2]=lie[2]|yihuo[7];
int s0[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};//s0盒子
int s1[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};//s0盒子
int r1=hang[1];
int r2=hang[2];
int c1=lie[1];
int c2=lie[2];
int out[3];  
out[1]=s0[r1][c1];
out[2]=s1[r2][c2];
int outbit1[3];
int outbit2[3];
for(int x=2;x>=1;x--)    //取输出前两位
{if (out[1]&1==1)
{outbit1[x]=1;}
    
else {outbit1[x]=0;}
out[1]=out[1]>>1;
}
for(int y=2;y>=1;y--)    //取输出后两位
{if (out[2]&1==1)
{outbit2[y]=1;}
    
else {outbit2[y]=0;}

out[2]=out[2]>>1;
}
int p4[5];    //p4置换--F函数的输出
p4[1]=outbit1[2];
p4[2]=outbit2[2];
p4[3]=outbit2[1];
p4[4]=outbit1[1];

int fk[9];   //fk函数的输出
fk[1]=ip[1]^p4[1];
fk[2]=ip[2]^p4[2];
fk[3]=ip[3]^p4[3];
fk[4]=ip[4]^p4[4];
fk[5]=ip[5];
fk[6]=ip[6];
fk[7]=ip[7];
fk[8]=ip[8];

ep[1]=fk[4];  //对左4位进行置换
ep[2]=fk[1];
ep[3]=fk[2];
ep[4]=fk[3];
ep[5]=fk[2];
ep[6]=fk[3];
ep[7]=fk[4];
ep[8]=fk[1];
yihuo[1]=ep[1]^key1[1]; //异或
yihuo[2]=ep[2]^key1[2];
yihuo[3]=ep[3]^key1[3];
yihuo[4]=ep[4]^key1[4];
yihuo[5]=ep[5]^key1[5];
yihuo[6]=ep[6]^key1[6];
yihuo[7]=ep[7]^key1[7];
yihuo[8]=ep[8]^key1[8];
hang[1]=yihuo[1];  //生成行号
hang[1]=hang[1]<<1;
hang[1]=hang[1]|yihuo[4];
hang[2]=yihuo[5];
hang[2]=hang[2]<<1;
hang[2]=hang[2]|yihuo[8];
lie[1]=yihuo[2];   //生成列号
lie[1]=lie[1]<<1;
lie[1]=lie[1]|yihuo[3];
lie[2]=yihuo[6];
lie[2]=lie[2]<<1;
lie[2]=lie[2]|yihuo[7];
r1=hang[1];
 r2=hang[2];
 c1=lie[1];
 c2=lie[2];
out[1]=s0[r1][c1];
out[2]=s1[r2][c2];

for(int o=2;o>=1;o--)  //取输出前两位
{if (out[1]&1==1)
{outbit1[o]=1;}
    
else {outbit1[o]=0;}

out[1]=out[1]>>1;
}
for(int w=2;w>=1;w--)  //取输出后两位
{if (out[2]&1==1)
{outbit2[w]=1;}
    
else {outbit2[w]=0;}

out[2]=out[2]>>1;
}    
p4[1]=outbit1[2];  //p4置换--F函数的输出
p4[2]=outbit2[2];
p4[3]=outbit2[1];
p4[4]=outbit1[1];

int fk1[9];    //fk函数的最后输出
fk1[1]=fk[5]^p4[1];
fk1[2]=fk[6]^p4[2];
fk1[3]=fk[7]^p4[3];
fk1[4]=fk[8]^p4[4];
fk1[5]=fk[1];
fk1[6]=fk[2];
fk1[7]=fk[3];
fk1[8]=fk[4];
int ipni[9];   //ip逆置换
ipni[1]=fk1[4];
ipni[2]=fk1[1];
ipni[3]=fk1[3];
ipni[4]=fk1[5];
ipni[5]=fk1[7];
ipni[6]=fk1[2];
ipni[7]=fk1[8];
ipni[8]=fk1[6];
outint=0;
for(int m=1;m<=8;m++)
{outint=outint|ipni[m];
if(m!=8){outint=outint<<1;}
}
return true;
}
/***************字符串一次加密函数*****************/
bool  MD5des::Stringencrypy(CString &instr,CString &outstr,CString key) 
{
	InitializeKey(key);   //初始化密钥key,生成密钥key1,key2,key3
	char *str=NULL;
	str   =  new char[strlen(CW2A(instr))+1];
	 memset(str,0,strlen(CW2A(instr)));
	 strcpy(str,CW2A(instr));
	 char *strout=NULL;//
	 strout   =  new char[strlen(CW2A(instr))+1];
	 strcpy(strout,CW2A(instr));
	 memset(strout,0,strlen(CW2A(instr)));
  char ch;
  size_t len1;
  len1=strlen(str);
  CString out;
for(int i=0;i<len1;i++)
{  
	ch=str[i];
   DesEncrypt(ch,m_key1); //一次加密
   strout[i]=outint1; 
}


for (int j=0;j<len1;j++)
{
	out += (CString)strout[j];

}
outstr=out;

if (str)
{
	delete []str;
	str=NULL;
}

if (strout)
{
	delete []strout;
	strout=NULL;
}

//释放
return true;


}
/***************字符串一次解密****************/
bool MD5des::Stringdecrypy(CString &instr,CString &outstr,CString key)  
{
	InitializeKey(key);  //初始化密钥key,生成密钥key1,key2,key3
	char *in=NULL;
	char *out=NULL;
	out   =new char[(strlen(CW2A(instr)))+1];
	in   =new char[strlen(CW2A(instr))+1];
	strcpy(out,CW2A(instr));
	memset(out,0,strlen(CW2A(instr)));
	strcpy(in,CW2A(instr));
	memset(in,0,strlen(CW2A(instr)));
     int len=instr.GetLength();
	char ch;
	for(int i=0;i<len;i++)  //循环读取每个字符

	{
		in[i]=instr[i];


		ch=in[i];
		DesDecrypt(ch,m_key1);  //对每个字符进行解密
		out[i]=outint; 

	}
	outstr=out;
	if (in)
	{
		delete []in;
		in=NULL;
	}
	if (out)
	{
		delete []out;
		out=NULL;
	}

return true;


}
/***********初始化密钥key,生成二个子密钥**********/
bool MD5des::InitializeKey(CString key)
 {
  unsigned char digest[16];
GetMd5String(digest, key); //对密钥进行MD5加密
int keyn[16];
for(int j=0;j<16;j++)//取出密钥各位的值
   {
	if(digest[j]&1)
{
	keyn[j]=1;
}
else
   {
	keyn[j]=0;
  }
}
for (int i=0;i<8;i++) //得到两个8位子密钥
   {
  m_key1[i]=keyn[i];
 m_key2[i]=keyn[i+8];
   }
return true;
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction   MD5加密函数
//////////////////////////////////////////////////////////////////////


bool MD5des::GetMd5String(unsigned char* digest, CString strinput)
{
	   int inputlen ;
    char *input=NULL ;
	//input = (char* ) malloc(strlen(CW2A(strinput))*sizeof(char)); 
	input = new char[strlen(CW2A(strinput))+1];
	if(!input)
	{
		return true ;
	}
	strcpy(input,CW2A(strinput.GetBuffer()));
	inputlen=strlen(input);
    Init();
    Update((unsigned char*)input,(unsigned int)inputlen);
   Final(digest);

  strinput.ReleaseBuffer(); 
  if(input)
  {
 delete []input;
  input = NULL;
  }
  return true;
  

}

VOID MD5des::GetMd5String(CString &strMD5, CString strInput)
{
    int len = 0;
    len=strInput.GetLength();
	
	char *input = NULL;
	input = new char[len+2];
	if(!input)
	{
		return ;
	}
	strcpy(input,CW2A(strInput.GetBuffer()));

	unsigned char digest[16];
    Init();
    Update((unsigned char*)input,(unsigned int)len);
	memset(digest,0x00,16);
	Final(digest);
	
	CString strTemp(_T(""));
	for(int i=0; i<16; i++)
	{
		strTemp.Format(_T("%x"),(int)digest[i]);   
		if(strTemp.GetLength()>2)
		{
			strTemp = strTemp.Right(2);
		}
		if(strTemp.GetLength() ==1)
		{
			strTemp = _T("0")+strTemp;
		}
        strMD5+=strTemp; 
	}
	if(input)
	{
		delete [] input;
		input = NULL;
	}
	strInput.ReleaseBuffer();
}

void MD5des::Enctypt(CString &strMD5, char *input, int inputlen)
{
	unsigned char digest[16];
    
    Init();
    Update((unsigned char*)input,(unsigned int)inputlen);
    Final((unsigned char*)digest);
    
    CString strTemp;
    for(int i=0;i<16;i++)
    {
        strTemp.Format(_T("%02x"),digest[i]);
        strMD5+=strTemp;
    }
}

VOID MD5des::Init()
{
    context->count[0] = context->count[1] = 0;//将当前的有效信息的长度设成0,
    /* Load magic initialization constants.
    */
    context->state[0] = 0x67452301;//初始化链接变量,算法要求这样
    context->state[1] = 0xefcdab89;
    context->state[2] = 0x98badcfe;
    context->state[3] = 0x10325476;
}

VOID MD5des::Update(unsigned char *input, unsigned int inputLen)
{
    unsigned int i, index, partLen;
    
  //计算已有信息的bits长度的字节数的模64, 64bytes=512bits。
 //用于判断已有信息加上当前传过来的信息的总长度能不能达到512bits,
 //如果能够达到则对凑够的512bits进行一次处理
    index = (unsigned int)((context->count[0] >> 3) & 0x3F);
    
 //更新已有信息的bits长度
    if ((context->count[0] += ((UINT)inputLen << 3))
        < ((UINT)inputLen << 3))
        context->count[1]++;
    context->count[1] += ((UINT)inputLen >> 29);
    
    partLen = 64 - index;   //计算已有的字节数长度还差多少字节可以 凑成64的整倍数
    
    /* Transform as many times as possible. */
    if (inputLen >= partLen) {           //如果当前输入的字节数 大于 已有字节数长度补足64字节整倍数所差的字节数
        memcpy(&context->buffer[index], input, partLen);
        MD5Transform (context->state, context->buffer);//*用当前输入的内容把context->buffer的内容补足512bits
        
        for (i = partLen; i + 63 < inputLen; i += 64)
            MD5Transform (context->state, &input[i]); //将输入缓冲区中的不足填充满512bits的剩余内容填充到context->buffer中,留待以后再作处理*/

        
        index = 0;
    }
    else
        i = 0;
    
    /* Buffer remaining input */
    memcpy(&context->buffer[index], &input[i],inputLen-i);
}

VOID MD5des::Final(UCHAR digest[16])
{ 
	//用于bits填充的缓冲区,为什么要64个字节呢?因为当欲加密的信息的bits数被512除其余数为448时,
//需要填充的bits的最大值为512=64*8 。

    static unsigned char PADDING[64] = {
        0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    };
	
    unsigned char bits[8];
    unsigned int index, padLen;
    
  //将要被转换的信息(所有的)的bits长度拷贝到bits中
    Encode (bits, context->count, 8);
    
   // 计算所有的bits长度的字节数的模64, 64bytes=512bits
    index = (unsigned int)((context->count[0] >> 3) & 0x3f);//计算需要填充的字节数,padLen的取值范围在1-64之间
    padLen = (index < 56) ? (56 - index) : (120 - index);
    Update ( PADDING, padLen);
    
    /* Append length (before padding) */
    Update (bits, 8);
    /* Store state in digest */
    Encode (digest, context->state, 16);
    
    /* Zeroize sensitive information.
    */
    memset (context, 0, sizeof (*context));
	
}

//////////////////////////////////////////////////////////////////////////
//private:
VOID MD5des::MD5Transform(UINT state[4],UCHAR block[64])
{
    UINT a = state[0], b = state[1], c = state[2], d = state[3], x[16];
    
    Decode (x, block, 64);
    
    /* Round 1 */
    FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
    FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
    FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
    FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
    FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
    FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
    FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
    FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
    FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
    FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
    FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
    FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
    FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
    FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
    FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
    FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
    
    /* Round 2 */
    GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
    GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
    GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
    GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
    GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
    GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
    GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
    GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
    GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
    GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
    GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
    
    GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
    GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
    GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
    GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
    GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
    
    /* Round 3 */
    HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
    HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
    HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
    HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
    HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
    HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
    HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
    HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
    HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
    HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
    HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
    HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
    HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
    HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
    HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
    HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
    
    /* Round 4 */
    II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
    II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
    II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
    II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
    II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
    II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
    II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
    II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
    II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
    II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
    II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
    II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
    II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
    II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
    II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
    II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
    
    state[0] += a;
    state[1] += b;
    state[2] += c;
    state[3] += d;
    
    /* Zeroize sensitive information.
    */
    memset (x, 0, sizeof (x));
}

VOID MD5des::Encode(UCHAR *output,UINT *input,UINT len)
{
    unsigned int i, j;
    
    for (i = 0, j = 0; j < len; i++, j += 4) {
        output[j] = (unsigned char)(input[i] & 0xff);
        output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
        output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
        output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
    }
}

VOID MD5des::Decode(UINT *output,UCHAR *input,UINT len)
{
    unsigned int i, j;
    
    for (i = 0, j = 0; j < len; i++, j += 4)
        output[i] = ((UINT)input[j]) | (((UINT)input[j+1]) << 8) |
        (((UINT)input[j+2]) << 16) | (((UINT)input[j+3]) << 24);
}

⌨️ 快捷键说明

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