📄 des1.c
字号:
des_crypt( ctx->esk, input, output );
}
void des_decrypt( des_context *ctx, uint8 input[8], uint8 output[8] )
{
des_crypt( ctx->dsk, input, output );
}
/* Triple-DES key schedule */
int des3_set_2keys( des3_context *ctx, uint8 key1[8], uint8 key2[8] )
{
int i;
des_main_ks( ctx->esk , key1 );
des_main_ks( ctx->dsk + 32, key2 );
for( i = 0; i < 32; i += 2 )
{
ctx->dsk[i ] = ctx->esk[30 - i];
ctx->dsk[i + 1] = ctx->esk[31 - i];
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] *
* DecryptFile Function *
* *
* [FORM] *
* INT DecryptFile(char *strToDecryFileName, char *strKey, *
* char *strDecryedFileName,char *pcTempFolder) *
* *
* [PARAMETER] *
* CHAR *strToDecryFileName:the file which is going to Decrypt *
* CHAR *strKey: The key which user input *
* CHAR *strDecryedFileName:the file which save the Decrypted Content *
* *
* [DESCRIPTION] *
* IF the password which user input is right,Then decrypt from file *
* source to file dest until EOF on source in right way.otherwise the *
* contants of the decrypt file is wrong. *
* [RETURN VALUE] *
* INT *
* [MODIFICATION] *
* 2005/03/24 AUTHOR:sunjun *
* 2005/04/05 AUTHOR:sunjun *
* 2005/04/06 AUTHOR:sunjun *
*****************************************************************************/
int DecryptFile(FILE *pfToDecryFileName, char *strKey, FILE *pfDecryedFileName,FILE *pfTempFolder)//sun
{
//read file in 8 bytes per times,once not enough 8 bytes save the remain;
int iRemain= 0;
int iMakeUp= 0; //iMakeUp is equal 8 byte subtraction Remain
int i= 0;
int j= 0;
int k= 0;
int iKeySize= 0;
int iDecryCounter= 0;
int iDecryDegree= 0;
int iNumOfEightbyte= 0;
int iKey= 0;
int iKeyword= 24; //if input is '0' fill iKeyword instead
unsigned char ucContent;
unsigned char ucKey[3][8];
unsigned char ucBuf[8];
des3_context ctx3;
//open the encrypt file
if(pfToDecryFileName==NULL)
{
return -1; //fail
}
fseek(pfToDecryFileName,-1,2); //reach the end of the encrypt file
iMakeUp = fgetc(pfToDecryFileName); //save the remain
iMakeUp = iMakeUp - 48; //convert char to int
if(pfDecryedFileName==NULL)
{
return -1; //fail
}
if(pfTempFolder==NULL)
{
return -1; //fail
}
/* create the key, If it don't enough 24 byte fill with iKey util
* it's Size reach the 24 byte.if input is '0' then fill with iKeyword
*/
iKeySize = strlen(strKey);
for(i=0,k=0;i<3;i++)
{
for(j=0;j<8;j++)
{
if(k<iKeySize)
{
if(*(strKey+k)=='0')
{
ucKey[i][j] = iKeyword;
iKeyword--;
k++;
}
else
{
ucKey[i][j] = *(strKey+k);
k++;
}
}
else if(k>=iKeySize)
{
ucKey[i][j] = iKey;
iKey++;
}
}
}
//3DES Encrypt
des3_set_3keys( &ctx3, ucKey[0],ucKey[1],ucKey[2] );
//Decrypt the file by 8 byte per times,and save to temp file
rewind(pfToDecryFileName);
while (!feof(pfToDecryFileName))
{
if(iDecryCounter>=8)
{
iDecryCounter=0;
for( iDecryDegree = 0; iDecryDegree < 1; iDecryDegree++ )
{
des3_decrypt( &ctx3, ucBuf, ucBuf );
}
for(i=0;i<8;i++)
{
fputc(ucBuf[i],pfTempFolder);
}
ucBuf[iDecryCounter]=fgetc(pfToDecryFileName);
iNumOfEightbyte++;
}
else
{
ucBuf[iDecryCounter]=fgetc(pfToDecryFileName);
}
iDecryCounter++;
}
//Save the number of char when filled druing encrypt file
iRemain = 8 - iMakeUp;
rewind(pfTempFolder);
//copy the temp file to decrypted file though filtration
iDecryCounter = 0;
if(iMakeUp == 0) //don't fill char
{
while (!feof(pfTempFolder))
{
if(iDecryCounter >= iNumOfEightbyte*8) break;
ucContent = fgetc(pfTempFolder);
fputc(ucContent,pfDecryedFileName);
iDecryCounter++;
}
}
else // fill char
{
while(!feof(pfTempFolder))
{
if(iDecryCounter >= iNumOfEightbyte*8 - iRemain) break;
ucContent = fgetc(pfTempFolder);
fputc(ucContent,pfDecryedFileName);
iDecryCounter++;
}
}
return 0;
}
/****************************************************************************
* [NAME] *
* *
* *
* [FORM] *
* *
* *
* [PARAMETERS] *
* *
* *
* [DESCRIBTION] *
* *
* *
* *
* [RETURN VALUE] *
* *
* *
* [MODIFY HISTORY] *
* *
*****************************************************************************/
int DecryptString(const uint8 *pcStringToDecrypt, int iLenOfStringToDecrypt, const uint8 *pcKey, int iLenOfKey, uint8 *pcDecryedString)//return the length of the decrypted string, -1 for error
{
int i;
int j;
int k;
int idegree;
int iNum;
int iLenOfDecryedString;
int iFillkey=0; //fill in secret key
int iExchangekey=24; //exchange zero character
des3_context ctx3;
uint8 ukey[3][8];
uint8 ubuf[8]; //buffer
uint8 iRemainOfStringToEncrypt;
uint8* puTempDecryedString; //memory
iRemainOfStringToEncrypt = *(pcStringToDecrypt+iLenOfStringToDecrypt-1);
iNum = iLenOfStringToDecrypt/8;
//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++;
}
}
}
des3_set_3keys( &ctx3, ukey[0],ukey[1],ukey[2] );
puTempDecryedString = (uint8 *)malloc(iLenOfStringToDecrypt);
for(i=0;i<iNum;i++)
{
for(j=0;j<8;j++)
{
ubuf[j]=*(pcStringToDecrypt+i*8+j);
}
for( idegree = 0; idegree < 1; idegree++ )
{
des3_encrypt( &ctx3, ubuf, ubuf );
}
for(j=0;j<8;j++)
{
*(puTempDecryedString+i*8+j)=ubuf[j];
}
}
iLenOfDecryedString=0;
if(iRemainOfStringToEncrypt==0) //add 0
{
for(i=0;i<iLenOfStringToDecrypt-1;i++)
{
*(pcDecryedString+i)=*(puTempDecryedString+i);
iLenOfDecryedString++;
}
}
else //not add 0
{
for(i=0;i<iLenOfStringToDecrypt-1;i++)
{
if (iLenOfDecryedString >= iNum*8 - iRemainOfStringToEncrypt) break;
*(pcDecryedString+i)=*(puTempDecryedString+i);
iLenOfDecryedString++;
}
}
return iLenOfDecryedString;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -