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

📄 des.cpp

📁 des 密码算法 能够实现des的密钥生成和 数据加密
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				pstroutput+=sprintf(pstroutput,"解密步骤F处理B:密钥字节的F处理...........\n");//) & 07
				pstroutput+=sprintf(pstroutput,"解密步骤F处理B-%d-1:第%d个6位密钥:",KeyByte,KeyByte);//) & 07
				for(int  i=0;i<6;i++)
				{
					pstroutput+=sprintf(pstroutput,"%d", subKey[KeyByte]	& (byteBit[i%6+2 ]) ? 1:0);
					if ((i+1)%6==0) pstroutput+=sprintf(pstroutput,"\n");	
				}
				
				pstroutput+=sprintf(pstroutput,"解密步骤F处理B-%d-1:第%d个6位数据:",KeyByte,KeyByte);//) & 07
				for( i=0;i<6;i++)
				{
					if(KeyByte<7) pstroutput+=sprintf(pstroutput,"%d", rt0 >> (26-KeyByte*4)	& (byteBit[i%6+2 ]) ? 1:0);
					else pstroutput+=sprintf(pstroutput,"%d", rt1	& (byteBit[i%6+2 ]) ? 1:0);
					if ((i+1)%6==0) pstroutput+=sprintf(pstroutput,"\n");
				}
				
				pstroutput+=sprintf(pstroutput,"解密步骤F处理B-%d-2:第%d次异或6位:",KeyByte,KeyByte);//) & 07
				for(i=0;i<6;i++)
				{
					if(KeyByte<7) pstroutput+=sprintf(pstroutput,"%d", (rt0 >> (26-KeyByte*4) ^ subKey[KeyByte])	& (byteBit[i%6+2 ]) ? 1:0);
					else pstroutput+=sprintf(pstroutput,"%d", (rt1 ^ subKey[KeyByte])	& (byteBit[i%6+2 ]) ? 1:0);
					if ((i+1)%6==0) pstroutput+=sprintf(pstroutput,"\n");	
					
				}
			}
			if(KeyByte<7) XorValue=((rt0 >> (26-KeyByte*4)) ^ subKey[KeyByte]);
			else XorValue=(rt1	^ subKey[KeyByte]);
			if (doEncrypt)pstroutput+=sprintf(pstroutput,"加密步骤F处理B-%d-3:第%d字节处理S盒代换和P重排:",KeyByte,KeyByte);//) & 07
			else pstroutput+=sprintf(pstroutput,"解密步骤F处理B-%d-3:第%d字节处理S盒代换和P重排:",KeyByte,KeyByte);//) & 07
			
			rowCol = (XorValue & 32) | ((XorValue & 1) ? 16 : 0) | ((XorValue >> 1) & 0xf);//重行位和列位:b0b5b1b2b3b4
			if (doEncrypt)pstroutput+=sprintf(pstroutput,"用S[%d]盒中第%d号值:%d取代\n", KeyByte,rowCol+1,si[KeyByte][rowCol]);
			for(int i=0;i<4;i++)//每个取代值有4位
			{
				if (si[KeyByte][rowCol] & byteBit[i+4])//取得输出位
				{
					if (doEncrypt)pstroutput+=sprintf(pstroutput,"此位为1,该在32位中的第%d位;P排列换到第%d位\n", KeyByte*4+i+1,pBox[KeyByte*4+i]+1);
					rValtwx[KeyByte] |= 1L << (31 - pBox[4 * KeyByte + i]);//进行P排列,并累加
				}
			}
			if (doEncrypt)
			{
				pstroutput+=sprintf(pstroutput,"P排列后得到的32位:\n");//) & 07
				for(i=0;i<4;i++)
				{//输出编号
					for(int j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%d",j+1);
					pstroutput+=sprintf(pstroutput,"	");//8位一制表符
				}
				pstroutput+=sprintf(pstroutput,"\n");	
				for(i=0;i<4;i++)
				{
					for(int j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%u",(rValtwx[KeyByte]>>(3-i)*8 & byteBit[j]) ? 1:0);
					pstroutput+=sprintf(pstroutput,"	");//8位一制表符
					if ((i+1)%4==0) pstroutput+=sprintf(pstroutput,"\n");	
					
				}			 
			}
			else
			{
				pstroutput+=sprintf(pstroutput,"P排列后得到的32位:\n");//) & 07
				for(i=0;i<4;i++)
				{//输出编号
					for(int j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%d",j+1);
					pstroutput+=sprintf(pstroutput,"	");//8位一制表符
				}
				pstroutput+=sprintf(pstroutput,"\n");	
				for(i=0;i<4;i++)
				{
					for(int j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%u",(rValtwx[KeyByte]>>(3-i)*8 & byteBit[j]) ? 1:0);
					pstroutput+=sprintf(pstroutput,"	");//8位一制表符
					if ((i+1)%4==0) pstroutput+=sprintf(pstroutput,"\n");	
					
				}			 
			}
			rVal |=rValtwx[KeyByte];
	}
	/*	  rVal |= sp[0][((rt0 >> 26) ^ *subKey++) & 0x3f];
	rVal |= sp[1][((rt0 >> 22) ^ *subKey++) & 0x3f];
	rVal |= sp[2][((rt0 >> 18) ^ *subKey++) & 0x3f];
	rVal |= sp[3][((rt0 >> 14) ^ *subKey++) & 0x3f];
	rVal |= sp[4][((rt0 >> 10) ^ *subKey++) & 0x3f];
	rVal |= sp[5][((rt0 >> 6)  ^ *subKey++) & 0x3f];
	rVal |= sp[6][((rt0 >> 2)  ^ *subKey++) & 0x3f];
	rVal |= sp[7][(rt1 ^ *subKey) & 0x3f];
	*/
	if (doEncrypt)
	{
		pstroutput+=sprintf(pstroutput,"步骤F处理C:8个4位或运算后得到的32位:\n");//) & 07
		
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%u",(rVal>>(3-i)*8 & byteBit[j]) ? 1:0);
			pstroutput+=sprintf(pstroutput,"	");//8位一制表符
			if ((i+1)%4==0) pstroutput+=sprintf(pstroutput,"\n");	
			
		}			 
	}
	return rVal;
  } /* F() */
  
	/************************************************************************
	*  Function name   : PermInit
	*  Description	   : Initialize a perm array
	*				   :
	*  Parameters	   :
	*  Returns		   :
	*  Author		   : Richard Shen
	* ----------------------------------------------------------------------
	*  Date 	By		 Description
	* ----------------------------------------------------------------------
	*  20Jan99	RCS 	 Created.
	************************************************************************/
//初始排列
	void PermInit(char perm[16][16][8], const char p[64])
	{
		int i, j, k, m, n;
		
		/* Clear the permutation array */
		for (i = 0; i < 16; i++)
		{
			for (j = 0; j < 16; j++)
			{
				for (k = 0; k < 8; k++)
					perm[i][j][k]=0;
			} /* end of for j */
		} /* end of for i */
		
		for (i = 0; i < 16; i++)		 /* Each input nibble position */
		{
			for (j = 0; j < 16; j++)	  /* Each possible input nibble */
			{
				for (k = 0; k < 64; k++)   /* Each output bit position	 */
				{
					n = p[k] - 1;//常表中,是从1开始所以-1;
					
					/* Does this bit come from input position ? */
					if ((n >> 2) != i)
						continue;			 /* No, bit k is 0			   */
					
					if (!(j & nibbleBit[n & 3]))
						continue;
					//m第几位在字节中的位置如k=1在第1位
					m					= k & 07; /* Which bit is this in the byte */
					//k最多6位,k>>3取得字节位置。设置第几位
					perm[i][j][k >> 3] |= byteBit[m];
				} /* end of for k */
			} /* end of for j */
		} /* end of for i */
		
		
		return;
	} /* PermInit() */
	
	  /************************************************************************
	  *  Function name	 : SPInit
	  *  Description	 : Initialize the lookup table for the combined S and P
	  * 				 : boxes
	  *  Parameters 	 : -
	  *  Returns		 : -
	  *  Author 		 : Richard Shen
	  * ----------------------------------------------------------------------
	  *  Date	  By	   Description
	  * ----------------------------------------------------------------------
	  *  20Jan99  RCS	   Created.
	************************************************************************/
	
	void SPInit(void)
	{
		int   p, i, s, j, rowCol;
		long  val;
		
		/* Compute pbox, the inverse of p32i. This is easier to work with. */
		//求P排列的逆排列:如将第9项放到第1位。
		for (p = 0; p < 32; p++)
		{
			for (i = 0;i < 32; i++)
			{
				if (p32i[i] - 1 == p) //p32i[i]是从1起的,
				{
					pBox[p] = i;
					break;
				} /* end of if */
			} /* end of for i */
		} /* end of for p */
		
		  /*	  pstroutput+=sprintf(pstroutput,"P盒的逆排列:\n");//) & 07
		  
			for (p = 0; p < 32; p++)
			{
			pstroutput+=sprintf(pstroutput,"%d		", pBox[p]);
			if ((p+1)%8==0) pstroutput+=sprintf(pstroutput,"\n");	
			}	  
		*/
		for (s = 0; s < 8; s++) 	  /* For each S-box 		 */
		{
			int n(0);
			// if (s==0)pstroutput+=sprintf(pstroutput,"搬到相应位:\n");
			for(i = 0; i < 64; i++)    /* For each possible input */
			{
				val = 0;//将所有的32位初始化为0,P盒替代后是32位
						/*
						The row number is formed from the first and last
						bits; the column number is from the middle 4.
				*/
				// i & 32取第0位,((i & 1) ? 16 : 0)取第5位,如为1,相当于乘16,
				//(i >> 1) & 0xf取中间4位,三者或起来,而成了行列,12为行,3456为列了,
				//换位05	取最高位   //取最低位,但基数是16,	//右移1位,再取4位即中间四位。
				//将每个i表示左面位是行位,右4位是列序的形式。
				rowCol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf);
				//rowCol本身是一个整数才能反映在S盒中的顺序数:从左到右,从上到下。
				
				for (j = 0; j < 4; j++)    /* For each output bit */
				{
					//每个元素值由4位组成,这里要决定各位
					if (si[s][rowCol] & (8 >> j))//si[s][rowCol]书中S盒,如果该位为1
					{
						
						if ((n+1)%8==0) val |= 1L << (31 - pBox[4 * s + j]);//则要把1搬到相应位且都或起来。
						//本来是在( 4 * s + j)的位置但是移位的最高位在最左边所以32-( 4 * s + j)
						//
					}
				} /* end of for */
				sp[s][i] = val;
			} /* end of for */
		} /* end of for s */
		
		
		
		return;
	} /* SPInit() */
	
	  /************************************************************************
	  *  Function name	 : ByteSwap
	  *  Description	 : Byte swap a long
	  * 				 :
	  *  Parameters 	 : x
	  *  Returns		 : ...
	  *  Author 		 : Richard Shen
	  * ----------------------------------------------------------------------
	  *  Date	  By	   Description
	  * ----------------------------------------------------------------------
	  *  20Jan99  RCS	   Created.
	************************************************************************/
//变反序为正序,例:int temp=0x12345678在intel系统内存中为:78563412
	//通过此函数变为:12345678
	//因为在实现时,将字符串强转为整型来处理。
	unsigned long ByteSwap(unsigned long x)
	{
		//一个整型有4个字节,0字节与3字节互换
		char  tmp;
		char  *cp;
		
		cp	  = (char *)&x;
		tmp   = cp[3];
		cp[3] = cp[0];
		cp[0] = tmp;
		//0,1字节互换
		tmp   = cp[2];
		cp[2] = cp[1];
		cp[1] = tmp;
		
		return x;
	} /* ByteSwap() */
	
	
	int main(int argc, char* argv[])
	{
		
		unsigned char key[9]="12345678";
		unsigned char message[9]="ABCDEFGH";
		//	 char stroutput[104857];
		//可以看到C当中字串型是正序,而整型是反序存储。
		int i(2049);
		int *p=&i;
		pstroutput+=sprintf(pstroutput,"Plain ASCII String:%s \n",message);
		//	pstroutput+=sprintf(pstroutput,"\n");
		pstroutput+=sprintf(pstroutput,"Plain Bit String: \n");
		for ( i=0;i<8;i++) { if(i%2==0) pstroutput+=sprintf(pstroutput,"\t");pstroutput+=sprintf(pstroutput,"%0X",message[i]);}
		pstroutput+=sprintf(pstroutput,"\n");
		
		pstroutput+=sprintf(pstroutput,"Key ASCII String:%s \n",key);
		pstroutput+=sprintf(pstroutput,"Key Bit String: \n");
		for ( i=0;i<8;i++) { if(i%2==0) pstroutput+=sprintf(pstroutput,"\t");pstroutput+=sprintf(pstroutput,"%0X",key[i]);;}
		pstroutput+=sprintf(pstroutput,"\n");
		DesEncrypt(key,message);
		
		pstroutput+=sprintf(pstroutput,"密文Cipher Bit String:");
		for (i=0;i<8;i++)
		{ 
			if(i%2==0) pstroutput+=sprintf(pstroutput,"\t");
			pstroutput+=sprintf(pstroutput,"0X%0X",message[i]);
		}
		pstroutput+=sprintf(pstroutput,"\n");
		
		DesDecrypt(key,message);
		pstroutput+=sprintf(pstroutput,"Decrypted Bit String:");
		printf(pstroutput,"Decrypted Bit String:");
		
		for (i=0;i<8;i++)
		{
			if(i%2==0) 
			{
				pstroutput+=sprintf(pstroutput,"\t");
				printf("\t");
			}
			pstroutput+=sprintf(pstroutput,"%0X",message[i]);
			printf("%0X",message[i]);
		}
		pstroutput+=sprintf(pstroutput,"\n");printf("\n");
		pstroutput+=sprintf(pstroutput,"Decrypted ASCII String:%s \n",message);
		printf("Decrypted ASCII String:%s \n",message);
		printf("\n");
		pstroutput+=sprintf(pstroutput,"\n");
		
		// printf("%s",pstroutput0);//输出到屏幕
		fstream  filetemp;
		
		char strtempfile[256]={0};
		GetCurrentDirectory(256,strtempfile);
		char strfilename[256]="\\Des全过程.txt";
		strcat(strtempfile,strfilename);
		
		
		filetemp.open(strtempfile,ios::out,filebuf::openprot);
		filetemp.write((unsigned char const *)pstroutput0,pstroutput-pstroutput0);
		filetemp.close();
		
		free(pstroutput0);
		
		
		return 0;
	}
	

⌨️ 快捷键说明

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