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

📄 des.cpp

📁 des 密码算法 能够实现des的密钥生成和 数据加密
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	for(i=0;i<32;i++)
	{
		pstroutput+=sprintf(pstroutput,"%02d ", i+1);
		if ((i+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");	
	}
	pstroutput+=sprintf(pstroutput,"\n");
	
	for(i=0;i<64;i++)
	{
		pstroutput+=sprintf(pstroutput,"%d  ", *(data+i/8) & (byteBit[i%8 ]) ? 1:0);
		if ((i+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");	
		if ((i==31))
		{
			pstroutput+=sprintf(pstroutput,"\n");
			for(int tempi=32;tempi<64;tempi++)
			{
				pstroutput+=sprintf(pstroutput,"%02d ", tempi+1);
				if ((tempi+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");	
			}
			
			pstroutput+=sprintf(pstroutput,"\n");
		}
	}
	pstroutput+=sprintf(pstroutput,"\n");
	/* Initial Permutation */
	//强制类型转换,会引起正反序发生变化。
	Permute(data, iperm, (unsigned char *)work);
	//整型要保持原业的字节序,也要变为正序。
	work[0] = ByteSwap(work[0]);
	work[1] = ByteSwap(work[1]);
	
	unsigned char* ipchar=(unsigned char *)work;
	
	if (doEncrypt)pstroutput+=sprintf(pstroutput,"加密步骤2-1:64bit初始排列后:\n");
	else pstroutput+=sprintf(pstroutput,"解密步骤2-1:64bit初始排列后:\n");
	for(i=0;i<32;i++)
	{
		pstroutput+=sprintf(pstroutput,"%02d ", ip[i]);
		if ((i+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");	
		
	}
	pstroutput+=sprintf(pstroutput,"\n");
	
	for(i=0;i<64;i++)
	{
		pstroutput+=sprintf(pstroutput,"%u  ", (work[i/32]>>(3-(i%32)/8)*8) & (byteBit[i%8 ]) ? 1:0);
		
		//			pstroutput+=sprintf(pstroutput,"%d  ", *(ipchar+i/8) & (byteBit[i%8 ]) ? 1:0);
		if ((i+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");	
		if (i==31) 
		{
			pstroutput+=sprintf(pstroutput,"\n");
			for(int tempi=32;tempi<64;tempi++)
			{
				pstroutput+=sprintf(pstroutput,"%02d ", ip[tempi]);
				if ((tempi+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");
			}
			
			pstroutput+=sprintf(pstroutput,"\n");
		}
	}
	//可以不要,算法上未做要求
	pstroutput+=sprintf(pstroutput,"\n");
	//加密
	if (doEncrypt)
	{
		
		/* Do the 16 rounds */
		pstroutput+=sprintf(pstroutput,"加密步骤2-2:F处理***************************\n");//) & 07
		for (i = 0; i < 16; i++)//从小到大使用密钥
			Round(doEncrypt,i, work);
		
		/* Left/right half swap */
		tmp 	= work[0];
		work[0] = work[1];
		work[1] = tmp;
		int j(0);
		
		
		pstroutput+=sprintf(pstroutput,"√√√√加密左右互换√√√√\n");
		pstroutput+=sprintf(pstroutput,"work[0]\n");
		
		
		for( i=0;i<4;i++)
		{
			for(j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%u",(work[0]>>(3-i)*8 & byteBit[j]) ? 1:0);
			pstroutput+=sprintf(pstroutput,"	");//8位一制表符
			if ((i+1)%4==0) pstroutput+=sprintf(pstroutput,"\n");	
			
		}			 
		
		pstroutput+=sprintf(pstroutput,"work[1]\n");	
		for( i=0;i<4;i++)
		{
			for(j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%u",(work[1]>>(3-i)*8 & byteBit[j]) ? 1:0);
			pstroutput+=sprintf(pstroutput,"	");//8位一制表符
			if ((i+1)%4==0) pstroutput+=sprintf(pstroutput,"\n");	
			
		}			 
		
	} /* end of if */
	else //解密
	{		
		pstroutput+=sprintf(pstroutput,"解密步骤2-2:F处理***************************\n");//) & 07
		/* Do the 16 rounds in reverse order */
		for (i = 15; i >= 0; i--)//从大到小使用密钥
			Round(doEncrypt,i, work);
		/* Left/right half swap */
		tmp 	= work[0];
		work[0] = work[1];
		work[1] = tmp;
		int j(0);
		
		
		pstroutput+=sprintf(pstroutput,"√√√√解密左右互换√√√√\n");
		pstroutput+=sprintf(pstroutput,"work[0]\n");
		
		
		for( i=0;i<4;i++)
		{
			for(j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%u",(work[0]>>(3-i)*8 & byteBit[j]) ? 1:0);
			pstroutput+=sprintf(pstroutput,"	");//8位一制表符
			if ((i+1)%4==0) pstroutput+=sprintf(pstroutput,"\n");	
			
		}			 
		
		pstroutput+=sprintf(pstroutput,"work[1]\n");	
		for( i=0;i<4;i++)
		{
			for(j=0;j<8;j++)pstroutput+=sprintf(pstroutput,"%u",(work[1]>>(3-i)*8 & byteBit[j]) ? 1:0);
			pstroutput+=sprintf(pstroutput,"	");//8位一制表符
			if ((i+1)%4==0) pstroutput+=sprintf(pstroutput,"\n");	
			
		}	
	} /* end of else */
	
	
	if (doEncrypt)pstroutput+=sprintf(pstroutput,"加密末64位数据:\n");
	else pstroutput+=sprintf(pstroutput,"解密末64位数据:\n");
	
	/* Inverse initial permutation */
	for(i=0;i<32;i++)
	{
		pstroutput+=sprintf(pstroutput,"%02d ", i+1);
		if ((i+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");	
		
	}
	pstroutput+=sprintf(pstroutput,"\n");
	
	for(i=0;i<64;i++)
	{
		pstroutput+=sprintf(pstroutput,"%u  ", (work[i/32]>>(3-(i%32)/8)*8) & (byteBit[i%8 ]) ? 1:0);
		
		//			pstroutput+=sprintf(pstroutput,"%d  ", *(ipchar+i/8) & (byteBit[i%8 ]) ? 1:0);
		if ((i+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");	
		if (i==31) 
		{
			pstroutput+=sprintf(pstroutput,"\n");
			for(int tempi=32;tempi<64;tempi++)
			{
				pstroutput+=sprintf(pstroutput,"%02d ", tempi+1);
				if ((tempi+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");
			}
			
			pstroutput+=sprintf(pstroutput,"\n");
		}
	}
				pstroutput+=sprintf(pstroutput,"\n");
				work[0] = ByteSwap(work[0]);
				work[1] = ByteSwap(work[1]);
				//整型作为字节型,先反序变为正序,
				Permute((unsigned char *)work, fperm, data);
				
				if (doEncrypt)pstroutput+=sprintf(pstroutput,"加密末64位的逆排列后:\n");
				else pstroutput+=sprintf(pstroutput,"解密末64位的逆排列后:\n");
				for(i=0;i<32;i++)
				{
					pstroutput+=sprintf(pstroutput,"%02d ", fp[i]);
					if ((i+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");	
					
				}
				pstroutput+=sprintf(pstroutput,"\n");
				for(i=0;i<64;i++)
				{
					pstroutput+=sprintf(pstroutput,"%d  ", *((unsigned char *)data+i/8) & (byteBit[i & 07 ]) ? 1:0);
					if ((i+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");
					if (i==31) 
					{
						pstroutput+=sprintf(pstroutput,"\n");
						for(int tempi=32;tempi<64;tempi++)
						{
							pstroutput+=sprintf(pstroutput,"%02d ", fp[tempi]);
							if ((tempi+1)%8==0) pstroutput+=sprintf(pstroutput,"|  ");
						}
						
						pstroutput+=sprintf(pstroutput,"\n");
					}
				}
				pstroutput+=sprintf(pstroutput,"\n");
				
} /* DES() */

  /************************************************************************
  *  Function name	 : SetKey
  *  Description	 : initialize key schedule array
  * 				 :
  *  Parameters 	 : key
  *  Returns		 : -
  *  Author 		 : Richard Shen
  * ----------------------------------------------------------------------
  *  Date	  By	   Description
  * ----------------------------------------------------------------------
  *  20Jan99  RCS	   Created.
  ************************************************************************/
  void SetKey(int doEncrypt,unsigned char *key)
  {
	  char			 pc1m[56];		/* Place to modify pc1 into */
	  char			 pcr[56];		/* Place to rotate pc1 into */
	  register int	 i ,j , k;
	  int			 m;
	  if(doEncrypt) pstroutput+=sprintf(pstroutput,"用户输入的64位密钥如下:\n二进制									十六进制\n");
	  if (doEncrypt)
	  {
		  pstroutput+=sprintf(pstroutput,"\n");
		  for (j = 0; j < 8; j++)	pstroutput+=sprintf(pstroutput,"%d\t",j);
	  }
	  if (doEncrypt) pstroutput+=sprintf(pstroutput,"\n");
	  if (doEncrypt) pstroutput+=sprintf(pstroutput,"\n");
	  
	  for (j = 0; j < 64; j++)
	  {
		  k = j;	  /* Integer bit location 将起点定到从0起*/
		  m = k & 07;		   /* find bit	归到一个字节里的哪一位(000-111  */
		  
		  if(doEncrypt) pstroutput+=sprintf(pstroutput,"%d\t", (key[k >> 3] & byteBit[m]) ? 1 : 0);//k >> 3找到所第几位所属字节,再求于字节中第几位
		  if ((j+1)%8==0 && doEncrypt)	pstroutput+=sprintf(pstroutput,"	0X%0X\n",key[k >> 3]);
	  } /* end of for j */
	  if (doEncrypt)
	  {
		  pstroutput+=sprintf(pstroutput,"\n");
		  for (j = 56; j < 64; j++) pstroutput+=sprintf(pstroutput,"%d\t",j);
	  }
	  if (doEncrypt) pstroutput+=sprintf(pstroutput,"\n");
	  
#if 0
	  /*
	  In mode 2, the 128 bytes of subkey are set directly from the
	  user's key, allowing him to use completely independent
	  subkeys for each round. Note that the user MUST specify a
	  full 128 bytes.
	  
		I would like to think that this technique gives the NSA a real
		headache, but I'm not THAT naive.
	  */
	  if (desmode == 2)
	  {
		  for (i = 0; i < 16; i++)
		  {
			  for (j = 0; j < 8; j++)
				  kn[i][j] = *key++;
		  } /* end of for i */
		  
		  return;
	  } /* end of if */
#endif
	  
	  /* Clear key schedule */
	  if(doEncrypt)
	  {
		  pstroutput+=sprintf(pstroutput,"/******************步骤1:子密钥处理开始**************************/\n");
		  pstroutput+=sprintf(pstroutput,"步骤1-1:去奇偶校验位(每个字节最左边的位),而后重排56位:\n");
	  }
	  for (i = 0; i < 16; i++)
	  {
		  for (j = 0; j < 8; j++)
			  kn[i][j] = 0;//密钥清零
	  } /* end of for i */
	  if (doEncrypt) pstroutput+=sprintf(pstroutput,"L 28位:\n");
	  for (j = 0; j < 28; j++)
	  {
		  if (doEncrypt)
		  {
			  pstroutput+=sprintf(pstroutput,"%02d ", pc1[j]);
			  if ((j+1)%7==0 && doEncrypt) pstroutput+=sprintf(pstroutput,"|  ");
			  //if ((j+1)%24==0 && doEncrypt)pstroutput+=sprintf(pstroutput,"\n");
		  } 
	  }
	  if (doEncrypt) pstroutput+=sprintf(pstroutput,"\n");
	  for (j = 0; j < 56; j++)
	  {//pc1[0]=57,k=56,m=0x00,56>>3=0x7在第7字节,第0位,
		  //每个字节的起始编号位不再是校验位而是其右1位,56个位都左移1位即-1
		  //校验位:0,8,16,8k位
		  k = pc1[j];	   /* Integer bit location 每字节都去了1校验位,*/
		  m = k & 07;		   /* find bit	归到一个字节里的哪一位(000-111  */
		  if(doEncrypt) pstroutput+=sprintf(pstroutput,"%d  ", (key[k >> 3] & byteBit[m]) ? 1 : 0);
		  //k >> 3找到所第几位所属字节,再求于字节中第几位
		  if (j==27 && doEncrypt)
		  {
			  pstroutput+=sprintf(pstroutput,"\nR 28位:\n");
			  for (int tempj = 28; tempj < 56; tempj++)
			  {
				  if (doEncrypt)
				  {
					  pstroutput+=sprintf(pstroutput,"%02d ", pc1[tempj]);
					  if ((tempj+1)%7==0 && doEncrypt) pstroutput+=sprintf(pstroutput,"|  ");
					  //if ((j+1)%24==0 && doEncrypt)pstroutput+=sprintf(pstroutput,"\n");
				  } 
			  }
			  //if (doEncrypt) pstroutput+=sprintf(pstroutput,"\n");
		  }
		  if ((j+1)%7==0 && doEncrypt) pstroutput+=sprintf(pstroutput,"	");
		  if ((j+1)%28==0 && doEncrypt) pstroutput+=sprintf(pstroutput,"\n");
		  
	  } /* end of for j */
	  
	  /* Convert pc1 to bits of key */
	  for (j = 0; j < 56; j++)
	  {
		  k = pc1[j];	   /* Integer bit location 将起点定到从0起*/
		  m = k & 07;		   /* find bit	归到一个字节里的哪一位(000-111  */
		  pc1m[j] = (key[k >> 3] & byteBit[m]) ? 1 : 0;//k >> 3找到所第几位所属字节,再求于字节中第几位
	  } /* end of for j */
	  
	  /* Key chunk for each iteration */
	  for (i = 0; i < 16; i++)
	  {
		  /* rotate pc1 the right amount */
		  for (j = 0; j < 56; j++)
		  {
			  //循环左移:即用右边(编号大)的位去写左边的(编号小)的位
			  k 	 = j + totRot[i];/*static const char totRot[] = 1, 2(1+1), 4(1+1+2+2,教材上前面左移几项位数之和), 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28};*/
			  pcr[j] = pc1m[(k < (j < 28 ? 28 : 56)) ? k : k - 28];//(j < 28 ? 28 : 56):取28则左半,取56右半,k - 28表示循环移位的
			  
			  if( doEncrypt)

⌨️ 快捷键说明

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