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

📄 descore.c

📁 DES加密解密源代码
💻 C
字号:

#include "des.h" 
#include "spr.h"   
                                        
#define ROTATE(a,n)     (((a)>>(n))+((a)<<(32-(n))))  

#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
	(b)^=(t),\
	(a)^=((t)<<(n)))

#define IP(l,r) \
	{ \
	register DES_LONG tt; \
	PERM_OP(r,l,tt, 4,0x0f0f0f0f); \
	PERM_OP(l,r,tt,16,0x0000ffff); \
	PERM_OP(r,l,tt, 2,0x33333333); \
	PERM_OP(l,r,tt, 8,0x00ff00ff); \
	PERM_OP(r,l,tt, 1,0x55555555); \
	}

#define FP(l,r) \
	{ \
	register DES_LONG tt; \
	PERM_OP(l,r,tt, 1,0x55555555); \
	PERM_OP(r,l,tt, 8,0x00ff00ff); \
	PERM_OP(l,r,tt, 2,0x33333333); \
	PERM_OP(r,l,tt,16,0x0000ffff); \
	PERM_OP(l,r,tt, 4,0x0f0f0f0f); \
	}


/******************************************************************************/
/*     des_encrypt_core                                                       */
/*          This is the core Data Encryption Standard (DES) routine which     */
/*          encrypts 64 bits of data.  It does not perform the initial        */
/*          permutations or rotations of this data which are characteristic.  */
/*			Of the DES algorithm.  These rotations and permutations are done  */
/*          before this algorithm is called for every example included in     */
/*			this package, although these rotations and permutations do not    */
/*			increase the security of the algorithm and have predominantly     */ 
/*          historical value.  They are included because they are part of the */
/*          definition of DES but may be removed to increase performance.     */
/*                                                                            */
/*          This code also includes a compiler flag option "DUAL_BANK." If    */
/*          -dDUAL_BANK is specified as a compiler option, the code makes a   */
/*          second copy of the des_SPtrans lookup table (the S- and P-Box     */
/*          transformations.)  It places on copy in the memory block from     */
/*          0x8000 0000 - 0x8000 7FFF and the second copy in the second       */
/*          memory block from 0x8000 8000 - 0x8000 FFFF.  This greatly        */
/*          the number of bank hits incurred by this random lookup process.   */
/*          The -dDUAL_BANK option should not be used with the 'C6211 as the  */
/*          second copy of the des_SPtrans lookup table will only decrease    */
/*          efficiency of the cache.                                          */
/*                                                                            */
/*          des_encrypt_core is called by des_encrypt_block and               */
/*          des3_encrypt_block.  See also the description of these functions  */
/*          below in this file.                                               */
/******************************************************************************/

void des_encrypt_core(
					  DES_LONG *data,
					  des_key_schedule ks)
{
	DES_LONG l,r,t,u,temp, *s;   
	DES_LONG lor1, lor2, lor3, lor4;
	unsigned int u1, u2, u3, u4;
	unsigned int t1, t2, t3, t4;
    int i;


	s=(DES_LONG *)ks;
    r= data[0];
    l= data[1];
	for (i=0; i<32; i+=2) 
		{       
		u=r^s[i  ]; 
		t=r^s[i+1];
		t=ROTATE(t,4); 
		
		u1 = EXTRACT(u, 24, 26); 
		u2 = EXTRACT(u, 16, 26); 
		u3 = EXTRACT(u, 8, 26); 
		u4 = EXTRACT(u, 0, 26); 

		lor1 = des_SPtrans[0][u1]; 
#ifdef DUAL_BANK
		lor2 = des_SPtrans2[4][u3]; 		  
#else
		lor2 = des_SPtrans[4][u3]; 		  
#endif
		lor1 ^= des_SPtrans[2][u2];
#ifdef DUAL_BANK
		lor2 ^= des_SPtrans2[6][u4]; 
#else
		lor2 ^= des_SPtrans[6][u4]; 
#endif
		t1 = EXTRACT(t, 24, 26); 
		t2 = EXTRACT(t, 16, 26); 
		t3 = EXTRACT(t, 8, 26); 
		t4 = EXTRACT(t, 0, 26); 

		lor3 = des_SPtrans[1][t1]; 
#ifdef DUAL_BANK
		lor4 = des_SPtrans2[5][t3]; 
#else
		lor4 = des_SPtrans[5][t3]; 
#endif
		lor3 ^= des_SPtrans[3][t2]; 
#ifdef DUAL_BANK
		lor4 ^= des_SPtrans2[7][t4]; 
#else
		lor4 ^= des_SPtrans[7][t4]; 
#endif
		
		lor1 ^= lor2;
		lor3 ^= lor4;
		lor1 ^= lor3;
		 
		temp = l ^ lor1;
		l = r;
		r = temp;       
		
		}
	data[0] = l;
	data[1] = r;    
	
}

/******************************************************************************/
/*     des_encrypt_block                                                      */
/*          This is the Data Encryption Standard (DES) block encryption       */
/*          routine.  des3_encrypt_block(...) encrypts 64 bits of data using  */
/*          a single 56 bit key.  This algorithm performs the initial         */
/*          permutations and rotations of this data which are characteristic  */
/*          of the DES algorithm before calling the core DES routine          */
/*          (des_encrypt_core).  These rotations and permutations do not      */
/*          increase the security of the algorithm and have predominantly     */ 
/*          historical value.  They are included because they are part of the */
/*          definition of DES but may be removed to increase performance.     */
/******************************************************************************/

void des_encrypt_block(
					  DES_LONG *data,
					  des_key_schedule ks)
{
	register DES_LONG l,r,temp;  
	
	l=data[0];
	r=data[1]; 
	IP(l,r);
	data[0]=ROTATE(l,29);
	data[1]=ROTATE(r,29);

	des_encrypt_core(data, ks);

	temp=data[0]; 
	l=ROTATE(temp,3);
	temp=data[1];  
	r=ROTATE(temp,3);
	FP(r,l);   
	data[0]=l;
	data[1]=r;     	
}     


	

⌨️ 快捷键说明

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