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

📄 destest.c

📁 这是一个DES加解密的C程序。其中实现了一重DES、三重DES、DES的两种工作模式:ECB和CBC。
💻 C
📖 第 1 页 / 共 2 页
字号:
	0x22,0x13,0x76,0x3C,0x1C,0xBD,0x4C,0xDC,	0x79,0x96,0x57,0xC0,0x64,0xEC,0xF5,0xD4,	0x1C,0x67,0x38,0x12,0xCF,0xDE,0x96,0x75};static unsigned char pcbc_ok[32]={	0xcc,0xd1,0x73,0xff,0xab,0x20,0x39,0xf4,	0x6d,0xec,0xb4,0x70,0xa0,0xe5,0x6b,0x15,	0xae,0xa6,0xbf,0x61,0xed,0x7d,0x9c,0x9f,	0xf7,0x17,0x46,0x3b,0x8a,0xb3,0xcc,0x88};static unsigned char plain[24]=	{	0x4e,0x6f,0x77,0x20,0x69,0x73,	0x20,0x74,0x68,0x65,0x20,0x74,	0x69,0x6d,0x65,0x20,0x66,0x6f,	0x72,0x20,0x61,0x6c,0x6c,0x20	};static unsigned char	KEY[4][8] = 		{	{0x08,0x04,0x02,0x01,0x80,0x40,0x20,0x10},	//key1
												{0x37,0x54,0x02,0xf7,0x38,0x4a,0x6b,0xc2},	//key2
												{0x15,0xc8,0xba,0xe9,0x10,0x73,0xcd,0x02},	//key3
												{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}};	//only for cbc one-key
												//CBC one key use 0x01010101,other use these key												static unsigned char	CBC_plain[24]={//	0x80000000,0x00000000,0x40000000,0x00000000,
												//0x20000000,0x00000000,	//one-key condition
                       // 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f,
                       // 0x10111213,0x14151617,	//two-key condition
                        0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
                        0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17};	//three-key condition
                        
static unsigned char	CBC_crypt[24]={//	0x95f8a5e5,0xdd31d900,0x0dbc203b,0x3e678af3,         
                      //  0xAD16A113,0x7EC7E221,	//one-key condition                        
                      //  0x45ca8bbb,0xafd00a96,0x9b8b5772,0x67e4780c,         
                      //  0x045AB7C0,0xFD572086,	//two-key condition
                        0xad,0xef,0xf7,0xed,0x15,0x5a,0x46,0x0f,0xf7,0xc0,0x52,0xf5,0x6a,0x49,0x39,0xa4,         
                        0xb1,0xe3,0x1b,0x16,0xb7,0x21,0xe7,0x24};	//three-key condtion        
                      
static unsigned char	CBC_IV[8]	=		{	//0x00000000,0x00000000,	//cbc one-key
												//0x08000000,0x00000000,	//cbc two-key
												0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a};	//cbc three-keystatic char *pt(unsigned char *p)
	{
	return p;
	};int main(void)	{	int j,err=0;	unsigned int i;	des_cblock in,out,outin,iv3,iv2;	des_key_schedule ks,ks2,ks3;	unsigned char cbc_in[40];	unsigned char cbc_out[40];

	//ecb	printf("Doing ecb\n");	for (i=0; i<NUM_TESTS; i++)		{		DES_set_key_unchecked(&key_data[i],&ks);		memcpy(in,plain_data[i],8);		memset(out,0,8);		memset(outin,0,8);		DES_ecb_encrypt(&in,&out,&ks,DES_ENCRYPT);		DES_ecb_encrypt(&out,&outin,&ks,DES_DECRYPT);		if (memcmp(out,cipher_data[i],8) != 0)			{			printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",				i+1,pt(key_data[i]),pt(in),pt(cipher_data[i]),				pt(out));			err=1;			}		if (memcmp(in,outin,8) != 0)			{			printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",				i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));			err=1;			}		}
	//ecb2	printf("Doing ecb2\n");
	for (i=0; i<(NUM_TESTS-2); i++)
		{
		DES_set_key_unchecked(&key_data[i],&ks);
		DES_set_key_unchecked(&key_data[i+1],&ks2);
		DES_set_key_unchecked(&key_data[i+2],&ks3);
		memcpy(in,plain_data[i],8);
		memset(out,0,8);
		memset(outin,0,8);
		DES_ecb2_encrypt(&in,&out,&ks,&ks2,DES_ENCRYPT);
		DES_ecb2_encrypt(&out,&outin,&ks,&ks2,DES_DECRYPT);

		if (memcmp(out,cipher_ecb2[i],8) != 0)
			{
			printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
				i+1,pt(key_data[i]),pt(in),pt(cipher_ecb2[i]),
				pt(out));
			err=1;
			}
		if (memcmp(in,outin,8) != 0)
			{
			printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
				i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
			err=1;
			}
		}

	printf("Doing cbc\n");	if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)		{		printf("Key error %d\n",j);		err=1;		}	memset(cbc_out,0,40);	memset(cbc_in,0,40);	memcpy(iv3,cbc_iv,sizeof(cbc_iv));	DES_cbc_encrypt(cbc_data,cbc_out,strlen((char *)cbc_data)+1,&ks,			 &iv3,DES_ENCRYPT);	if (memcmp(cbc_out,cbc_ok,32) != 0)		{		printf("cbc_encrypt encrypt error\n");		err=1;		}	memcpy(iv3,cbc_iv,sizeof(cbc_iv));	DES_cbc_encrypt(cbc_out,cbc_in,strlen((char *)cbc_data)+1,&ks,			 &iv3,DES_DECRYPT);	if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)) != 0)		{		printf("cbc_encrypt decrypt error\n");		err=1;		}	printf("Doing cbc3\n");
	if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
		{
		printf("Key error %d\n",j);
		err=1;
		}
	if ((j=DES_set_key_checked(&cbc2_key,&ks2)) != 0)
		{
		printf("Key error %d\n",j);
		err=1;
		}
	if ((j=DES_set_key_checked(&cbc3_key,&ks3)) != 0)
		{
		printf("Key error %d\n",j);
		err=1;
		}
	memset(cbc_out,0,40);
	memset(cbc_in,0,40);
	i=strlen((char *)cbc_data)+1;
	/* i=((i+7)/8)*8; */
	memcpy(iv3,cbc_iv,sizeof(cbc_iv));

	DES_ede3_cbc_encrypt(cbc_data,cbc_out,i,&ks,&ks2,&ks3,&iv3,DES_ENCRYPT);
	if (memcmp(cbc_out,cbc3_ok,
		(unsigned int)(strlen((char *)cbc_data)+1+7)/8*8) != 0)
		{
		unsigned int n;

		printf("des_ede3_cbc_encrypt encrypt error\n");
		for(n=0 ; n < i ; ++n)
		    printf(" %02x",cbc_out[n]);
		printf("\n");
		for(n=0 ; n < i ; ++n)
		    printf(" %02x",cbc3_ok[n]);
		printf("\n");
		err=1;
		}

	memcpy(iv3,cbc_iv,sizeof(cbc_iv));
	DES_ede3_cbc_encrypt(cbc_out,cbc_in,i,&ks,&ks2,&ks3,&iv3,DES_DECRYPT);
	if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
		{
		unsigned int n;

		printf("des_ede3_cbc_encrypt decrypt error\n");
		for(n=0 ; n < i ; ++n)
		    printf(" %02x",cbc_data[n]);
		printf("\n");
		for(n=0 ; n < i ; ++n)
		    printf(" %02x",cbc_in[n]);
		printf("\n");
		err=1;
		}			printf("Doing cbc3 add by sunjian\n");
	if ((j=DES_set_key_checked(&KEY[0],&ks)) != 0)
		{
		printf("Key error %d\n",j);
		err=1;
		}
	if ((j=DES_set_key_checked(&KEY[1],&ks2)) != 0)
		{
		printf("Key error %d\n",j);
		err=1;
		}
	if ((j=DES_set_key_checked(&KEY[2],&ks3)) != 0)		{
		printf("Key error %d\n",j);
		err=1;
		}
	memset(cbc_out,0,40);
	memset(cbc_in,0,40);
//	i=strlen((char *)CBC_plain)+1;
	i=24;
	memcpy(iv3,CBC_IV,sizeof(CBC_IV));

	DES_ede3_cbc_encrypt(CBC_plain,cbc_out,i,&ks,&ks2,&ks3,&iv3,DES_ENCRYPT);
	if (memcmp(cbc_out,CBC_crypt,24) != 0)
		{
		unsigned int n;

		printf("des_ede3_cbc_encrypt encrypt error\n");
		for(n=0 ; n < i ; ++n)
		    printf(" %02x",cbc_out[n]);
		printf("\n");
		for(n=0 ; n < i ; ++n)
		    printf(" %02x",CBC_crypt[n]);
		printf("\n");
		err=1;
		}

	memcpy(iv3,CBC_IV,sizeof(CBC_IV));
	DES_ede3_cbc_encrypt(CBC_crypt,cbc_in,i,&ks,&ks2,&ks3,&iv3,DES_DECRYPT);
	if (memcmp(cbc_in,CBC_plain,24) != 0)
		{
		unsigned int n;

		printf("des_ede3_cbc_encrypt decrypt error\n");
		for(n=0 ; n < i ; ++n)
		    printf(" %02x",CBC_plain[n]);
		printf("\n");
		for(n=0 ; n < i ; ++n)
		    printf(" %02x",cbc_in[n]);
		printf("\n");
		err=1;
		}		return(err);	}
    

⌨️ 快捷键说明

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