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

📄 des.h

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

#define EXTRACT(x,l,r) (x << l) >> r

#define DES_LONG unsigned int
typedef unsigned char des_cblock[8];
typedef struct des_ks_struct
	{
	union   {
		des_cblock _;
		} ks;
#undef _
#define _       ks._
	} des_key_schedule[17];   /*  should be 16, but added 1 pad. */ 
	
#define DES_KEY_SZ      (sizeof(des_cblock))

extern const DES_LONG des_SPtrans[8][64]; 


/******************************************************************************/
/*     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_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);

 

/******************************************************************************/
/*     des_cbc_encrypt                                                        */
/*          This is the Data Encryption Standard (DES) routine which encrypts */
/*          a data array of arbitrary  length in the Cipher Block Chaining    */
/*          (CBC) mode.  It assumes that the input array has a length         */
/*          divisible by 8 bytes.  If this data array does not have a length  */
/*          divisible by 8 bytes, it should be zero padded to prevent         */
/*          overflow.  The output array should be similarly padded.           */
/*                                                                            */
/*          The initialization vector, "ivec" is overwritten with the last    */
/*          8 bytes of encrypted output.  This new "ivec" may be used as the  */
/*          initialization vector for the next block of data.                 */
/******************************************************************************/
void des_cbc_encrypt(
					 des_cblock (*input),
					 des_cblock (*ivec),
					 des_key_schedule ks,
					 des_cblock (*output),
					 int length);

/******************************************************************************/
/*     des_cbc_decrypt                                                        */
/*          This is the Data Encryption Standard (DES) routine which decrypts */
/*          a data array of arbitrary length in the Cipher Block Chaining mode*/
/*          (CBC).  It assumes that the input array has a length divisible by */
/*          8 bytes.  If this data array does not have a length divisible by  */
/*          8 bytes, it should be zero padded to prevent overflow.  The       */
/*          output array should be similarly padded.  The variable "length"   */
/*          does not need to be divisible by 8 as long as the input and       */
/*          output arrays are properly padded.                                */
/*                                                                            */
/*          The initialization vector, "ivec" is overwritten with the last    */
/*          8 bytes of de-encrypted output.  This new "ivec" may be used as   */
/*          the initialization vector for the next block of data.             */
/*																			  */
/*          The key schedule provided to this function should be the same key */
/*          schedule that is provided to des_cbc_encrypt.  The function       */
/*          reorganizes the key schedule to decrypt using the block           */
/*          encryption function.  (It is a property of the DES algorithm that */
/*          the core encryption routine is also used to decrypt, but with a   */
/*          modification to the key schedule.)                                */
/******************************************************************************/			
void des_cbc_decrypt(
					 des_cblock (*input),
					 des_cblock (*ivec),
					 des_key_schedule ks,
					 des_cblock (*output),
					 int length);





void des_set_odd_parity(des_cblock *key);
int des_is_weak_key(des_cblock *key);
int des_set_key(des_cblock *key, volatile des_key_schedule schedule);



void set_key(void);
void encrypt(void);
void decrypt(void); 
void verify(void);

extern char en_de;
extern unsigned char ivec[8];

#define BLOCK_SIZE 8
#define NUM_OF_BLOCKS 1


⌨️ 快捷键说明

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