📄 des.c
字号:
else
{
X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
}
*SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
| ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
| ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
| ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
| ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
| ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
| ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
| ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
| ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
| ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
| ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
*SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
| ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
| ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
| ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
| ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
| ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
| ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
| ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
| ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
| ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
| ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
}
return( 0 );
}
int des_set_key( des_context *ctx, uint8 key[8] )
{
int i;
/* setup encryption subkeys */
des_main_ks( ctx->esk, key );
/* setup decryption subkeys */
for( i = 0; i < 32; i += 2 )
{
ctx->dsk[i ] = ctx->esk[30 - i];
ctx->dsk[i + 1] = ctx->esk[31 - i];
}
return( 0 );
}
/* DES 64-bit block encryption/decryption */
void des_crypt( uint32 SK[32], 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_FP( Y, X );
PUT_UINT32( Y, output, 0 );
PUT_UINT32( X, output, 4 );
}
void des_encrypt( des_context *ctx, uint8 input[8], uint8 output[8] )
{
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 );
}
static unsigned char DES3_keys[3][8] =
{
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
{ 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01 },
{ 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 }
};
static unsigned char DES3_enc_test[3][8] =
{
{ 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B },
{ 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 },
{ 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 }
};
static unsigned char DES3_dec_test[3][8] =
{
{ 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D },
{ 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB },
{ 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A }
};
typedef unsigned int uint;
typedef unsigned char byte;
#define NULL 0
// Print a byte array in hex notation to the screen
void PrintHex(unsigned char* pbtSequence, uint uiBytesLength)
{
uint uiPos;
for (uiPos=0; uiPos < 8; uiPos++)
{
printf("%02x ",*pbtSequence);
pbtSequence++;
}
printf("\n");
}
void XorBytes(unsigned char* pbtDest, unsigned char* pbtSrc, uint uiLen)
{
uint ui;
for (ui=0; ui<uiLen; ui++)
{
pbtDest[ui] ^= pbtSrc[ui];
}
}
int main(int argc, const char* argv[])
{
int m, n, i;
des_context ctx;
des3_context ctx3;
unsigned char buf[8];
char sLine[255];
// unsigned char DES_key[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
unsigned char DES_key[8] = { 0xa1, 0x0a, 0xaa, 0xd5, 0xe6, 0xbd, 0x46, 0x87 };
FILE* f;
// test-data: a1 0a aa d5 e6 bd 46 87
// static unsigned char DES_test[8] = { 0xa1, 0x0a, 0xaa, 0xd5, 0xe6, 0xbd, 0x46, 0x87 };
static unsigned char DES_test[8];
// static unsigned char DES_block[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// static unsigned char DES_block[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static unsigned char DES_block[8] = { 0xa1, 0x0a, 0xaa, 0xd5, 0xe6, 0xbd, 0x46, 0x87 };
des_set_key(&ctx,DES_key);
des_encrypt(&ctx,DES_block,buf);
PrintHex(buf,8);
XorBytes(buf+2,DES_key,6);
PrintHex(buf,8);
des_set_key(&ctx,DES_block);
des_encrypt(&ctx,DES_key,buf);
PrintHex(buf,8);
DES_block[1]++;
des_set_key(&ctx,DES_key);
des_encrypt(&ctx,DES_block,buf);
PrintHex(buf,8);
DES_block[1]++;
des_set_key(&ctx,DES_key);
des_encrypt(&ctx,DES_block,buf);
PrintHex(buf,8);
DES_block[7]++;
des_set_key(&ctx,DES_key);
des_encrypt(&ctx,DES_block,buf);
PrintHex(buf,8);
/*
if (argc != 2)
{
printf("syntax: des <DES-Random-File.txt>\n");
return 1;
}
f = fopen(argv[1],"r+");
while (fgets(sLine,255,f) != NULL)
{
// Simple test if we are dealing with a 8 byte hex notated value
if (strlen(sLine) == 24 || strlen(sLine) == 25)
{
sscanf(sLine,"%2x %2x %2x %2x %2x %2x %2x %2x",DES_test,DES_test+1,DES_test+2,DES_test+3,DES_test+4,DES_test+5,DES_test+6,DES_test+7);
des_set_key(&ctx,DES_key);
des_encrypt(&ctx,DES_test,buf);
PrintHex(buf,8);
}
}
fclose(f);
*/
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -