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

📄 des.c

📁 MD5加密解密,包装为c++ 类,方便使用.
💻 C
📖 第 1 页 / 共 2 页
字号:

        ctx->esk[i + 32] = ctx->dsk[62 - i];
        ctx->esk[i + 33] = ctx->dsk[63 - i];

        ctx->esk[i + 64] = ctx->esk[     i];
        ctx->esk[i + 65] = ctx->esk[ 1 + i];

        ctx->dsk[i + 64] = ctx->dsk[     i];
        ctx->dsk[i + 65] = ctx->dsk[ 1 + i];
    }

    return( 0 );
}

int des3_set_3keys( des3_context *ctx, uint8 key1[8], uint8 key2[8],
                                       uint8 key3[8] )
{
    int i;

    des_main_ks( ctx->esk     , key1 );
    des_main_ks( ctx->dsk + 32, key2 );
    des_main_ks( ctx->esk + 64, key3 );

    for( i = 0; i < 32; i += 2 )
    {
        ctx->dsk[i     ] = ctx->esk[94 - i];
        ctx->dsk[i +  1] = ctx->esk[95 - i];

        ctx->esk[i + 32] = ctx->dsk[62 - i];
        ctx->esk[i + 33] = ctx->dsk[63 - i];

        ctx->dsk[i + 64] = ctx->esk[30 - i];
        ctx->dsk[i + 65] = ctx->esk[31 - i];
    }

    return( 0 );
}

/* Triple-DES 64-bit block encryption/decryption */

void des3_crypt( uint32 SK[96], uint8 input[8], uint8 output[8] )
{
    uint32 X, Y, T;

    GET_UINT32( X, input, 0 );
    GET_UINT32( Y, input, 4 );

    DES_IP( X, Y );

    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );

    DES_ROUND( X, Y );  DES_ROUND( Y, X );
    DES_ROUND( X, Y );  DES_ROUND( Y, X );
    DES_ROUND( X, Y );  DES_ROUND( Y, X );
    DES_ROUND( X, Y );  DES_ROUND( Y, X );
    DES_ROUND( X, Y );  DES_ROUND( Y, X );
    DES_ROUND( X, Y );  DES_ROUND( Y, X );
    DES_ROUND( X, Y );  DES_ROUND( Y, X );
    DES_ROUND( X, Y );  DES_ROUND( Y, X );

    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );
    DES_ROUND( Y, X );  DES_ROUND( X, Y );

    DES_FP( Y, X );

    PUT_UINT32( Y, output, 0 );
    PUT_UINT32( X, output, 4 );
}

void des3_encrypt( des3_context *ctx, uint8 input[8], uint8 output[8] )
{
    des3_crypt( ctx->esk, input, output );
}

void des3_decrypt( des3_context *ctx, uint8 input[8], uint8 output[8] )
{
    des3_crypt( ctx->dsk, input, output );
}


/****************************************************************************
*   [Name]                                                                  *
*      EncryptFile:encrypt file function                                    *
*                                                                           *
*   [Form]                                                                  *
*      int EncryptFile(char *pcToEncryFileName, char *pcKey,                *
*      char *pcEncryedFileName,CString pcTempPath)                          *
*                                                                           *
*   [Parameter]                                                             *
*      char *pcToEncryFileName:file name need to encrypt                    *
*      char *pcKey: encrypt key                                             *
*      char *pcEncryedFileName: file name after encrypted                   *
*      char *pcTempPath: temporary directory                                *
*   [Description]                                                           *
*      encrypt file                                                         *
*                                                                           *
*   [Return Value]                                                          *
*      int                                                                  *
*                                                                           *
*   [Modified History]                                                      *
*       2005/04/04             Auther丗wang,bin               Edit          *
*       2005/04/05             Auther丗wang,bin               Edit          *
*       2005/05/28             Auther: sun,jun                Edit          *
*****************************************************************************/
int EncryptFile(FILE *pfToZippedFile,char *pcKey,FILE *pfEncryedFile,FILE *pfTempFile)
{	
	unsigned char ucChar;									
	uint8 u8Buf[8];											
	int iCounter;											
	des3_context ctx3;							
	uint8 ukey[3][8];							
	int i,k,j;										
	int iKeySize = strlen(pcKey);		
	int iDegree;									
	int iFillkey=0;                           //fill in secret key 
	int iExchangekey=24;                        //exchange zero character


    //Determine	Whether To Encrypt File Opened
    if(pfToZippedFile==NULL)
	{	
        return -1;						//open failed
	}
    //Determine Whether Encrypted File Opened
    if(pfEncryedFile==NULL)
	{	
        return -1;						//open failed
	}
	//Determine Whether Temp File Opened
    if(pfTempFile==NULL)
	{
        return -1;						//open failed
	}
    //Create key
    for(i=0,k=0;i<3;i++)
	{	
	    for(j=0;j<8;j++)
		{	
			if(k<iKeySize)
			{	
				if(*(pcKey+k)=='0')
				{	
					ukey[i][j]=iExchangekey;
					iExchangekey--;
					k++;
				}
				else
				{	
					ukey[i][j] = *(pcKey+k);
					k++;
				}
			}
			else if(k>=iKeySize)
			{	
				ukey[i][j] = iFillkey;
				iFillkey++;
			}			
		}
	}
	//3DES Encrypt
	des3_set_3keys( &ctx3, ukey[0],ukey[1],ukey[2] );	

    ucChar=fgetc(pfToZippedFile);
	iCounter=0;
    //Encrypt the To Encrypt File per 8 bytes,if not enough 8 bytes add 0.
	while(!feof(pfToZippedFile))
	{	
		if(iCounter>=8)
		{	
			iCounter=0;
			for( iDegree = 0; iDegree < 1; iDegree++ )
			{	
				des3_encrypt( &ctx3, u8Buf, u8Buf );
			}
			for(i=0;i<8;i++)
			{	
				fputc(u8Buf[i],pfTempFile);
			}
			u8Buf[iCounter]=ucChar;			
		}
		else
		{	
			u8Buf[iCounter]=ucChar;
		}
        ucChar=fgetc(pfToZippedFile);
		iCounter++;
	}
	//store the number of remain bytes which is not added in last 8 bytes.
	if(iCounter>0)
	{	
		for(j=iCounter;j<8;j++)
		{	
			u8Buf[j] = '0';
		}
        for( iDegree = 0; iDegree < 1; iDegree++ )
		{	
			des3_encrypt( &ctx3, u8Buf, u8Buf );
		}
		for(i=0;i<8;i++)
		{	
			fputc(u8Buf[i],pfTempFile);
		}
		switch(iCounter)
		{	
		case 1:
			fputc('1',pfTempFile);break;
		case 2:
			fputc('2',pfTempFile);break;
        case 3:
			fputc('3',pfTempFile);break;
		case 4:
			fputc('4',pfTempFile);break;
		case 5:
			fputc('5',pfTempFile);break;
		case 6:
			fputc('6',pfTempFile);break;
		case 7:
			fputc('7',pfTempFile);break;
		case 8:
			fputc('0',pfTempFile);break;
		default:
			break;
		}
	}
	else
	{	
		fputc('0',pfTempFile);
	}

    rewind(pfTempFile);			//move to top of temp file
    ucChar=fgetc(pfTempFile);
    while(!feof(pfTempFile))
    {	
		fputc(ucChar,pfEncryedFile);   //char insert goal file
		ucChar=fgetc(pfTempFile);
    }

    return 0;
}


/****************************************************************************
*   [NAME]                                                                  *
*      EncryptString:encrypt String function                                *
*                                                                           *
*   [FORM]                                                                  *
*      int EncryptString(const uint8 *pcStringToEncrypt,                    *
*      int iLenOfStringToEncrypt, const uint8 *pcKey, int iLenOfKey,        *
*      uint8 *pcEncryedString)                                              *
*                                                                           *
*   [PARAMETERS]                                                            *
*      const uint8 *pcStringToEncrypt:String name need to encrypt           *
*      int iLenOfStringToEncrypt:the length of String                       *
*      const uint8 *pcKey: encrypt key                                      *
*      int iLenOfKey: the length of Key                                     *
*      uint8 *pcEncryedString: Zipped String name                           *
*    [DISCRIPTION]                                                          *
*       encrypt string                                                      *
*                                                                           *
*    [RETURN VALUE]                                                         *
*       int                                                                 *
*                                                                           *
*    [MODIFY HISTORY]                                                       *
*       2005/04/15        AUTHOR丗sun,jun      EDIT                         *
*****************************************************************************/
int EncryptString(const uint8 *pcStringToEncrypt, int iLenOfStringToEncrypt, const uint8 *pcKey, int iLenOfKey, uint8 *pcEncryedString)//return the length of the encrypted string, -1 for error
{
	int i;
	int j;
	int k;
	int iFlag=0;
	int idegree=0;
	int iMaxLenOfStringToEncrypt = 0;
	int iRemainOfStringToEncrypt = 0;
	int iFillkey=0;                           //fill in secret key 
	int iExchangekey=24;                        //exchange zero character
	uint8* puTempStringToEncrypt;   //memory
	uint8 ukey[3][8];
	uint8 ubuf[8];					//buffer
	des3_context ctx3;
	//Get the number add char
	if (iLenOfStringToEncrypt%8==0)
	{
		iMaxLenOfStringToEncrypt=iLenOfStringToEncrypt;
		iRemainOfStringToEncrypt=0;
	}
	else
	{
		iRemainOfStringToEncrypt=iLenOfStringToEncrypt%8;
		iMaxLenOfStringToEncrypt=iLenOfStringToEncrypt + (8-iRemainOfStringToEncrypt);
	}

	puTempStringToEncrypt = (uint8 *)malloc(iMaxLenOfStringToEncrypt);
	//create integer array to build array 8bit
	for(i=0,k=0;i<iMaxLenOfStringToEncrypt;i++)
	{
		if(iFlag == 0)
		{
			for(j=0;j<8;j++)
			{
			   if(k<iLenOfStringToEncrypt)
			   {
					*(puTempStringToEncrypt+i*8+j)=*(pcStringToEncrypt+k);
					k++;
			   }
			   else
			   {
					*(puTempStringToEncrypt+i*8+j)='0';
					iFlag = 1;
			   }
			}
		}
		else if(iFlag ==1)
		{
			break;
		}
	}

	//Create Key
	for (i=0,k=0;i<3;i++)
	{
		for (j=0;j<8;j++)
		{
			if (k<iLenOfKey)
			{
				if(*(pcKey+k)=='0')
				{	
					ukey[i][j]=iExchangekey;
					iExchangekey--;
					k++;
				}
				else
				{	
					ukey[i][j] = *(pcKey+k);
					k++;
				}
			}
			else if(k>=iLenOfKey)
			{
				ukey[i][j] = iFillkey;
				iFillkey++;
			}
		}
	}
	//3DES Encrypt
	des3_set_3keys( &ctx3, ukey[0],ukey[1],ukey[2] );
	
	//EncryptString
	for(i=0;i<iMaxLenOfStringToEncrypt/8;i++)
	{
		for(j=0;j<8;j++)
		{
			ubuf[j]=*(puTempStringToEncrypt+i*8+j);
		}
		for( idegree = 0; idegree < 1; idegree++ )
		{
			 des3_decrypt( &ctx3, ubuf, ubuf );
		}
		for(j=0;j<8;j++)
		{
			*(pcEncryedString+i*8+j)=ubuf[j];
		}
	}
	//add char to end according to the number of add char
	if (iRemainOfStringToEncrypt==0)
	{
		*(pcEncryedString+iMaxLenOfStringToEncrypt)=0;
	}
	else
	{
		*(pcEncryedString+iMaxLenOfStringToEncrypt)=8-iRemainOfStringToEncrypt;
	}
	return iMaxLenOfStringToEncrypt+1;
}

⌨️ 快捷键说明

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