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

📄 sms4.c

📁 SMS4的源代码
💻 C
字号:
#include <stdio.h>#define ROUND 32  /*轮数*/#define ROL(x,y) ((x)<<(y)|(x)>>(32-(y))) /*将X循环左移Y位的运算*/#include "rejindelt.h"BYTE mkey[32];static unsigned long FK[4]={    0xA3B1BAC6,0x56AA3350,0x677D9197,0xB27022DC};static unsigned long CK[ROUND]={    0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,    0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,    0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,    0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,    0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229,    0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,    0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,    0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279};static unsigned char Sbox[6][256]={    {		0xd6,0x90,0xe9,0xfe,0xcc,0xe1,0x3d,0xb7,0x16,0xb6,0x14,0xc2,0x28,0xfb,0x2c,0x05,		0x2b,0x67,0x9a,0x76,0x2a,0xbe,0x04,0xc3,0xaa,0x44,0x13,0x26,0x49,0x86,0x06,0x99,	    0x9c,0x42,0x50,0xf4,0x91,0xef,0x98,0x7a,0x33,0x54,0x0b,0x43,0xed,0xcf,0xac,0x62,	    0xe4,0xb3,0x1c,0xa9,0xc9,0x08,0xe8,0x95,0x80,0xdf,0x94,0xfa,0x75,0x8f,0x3f,0xa6,	    0x47,0x07,0xa7,0xfc,0xf3,0x73,0x17,0xba,0x83,0x59,0x3c,0x19,0xe6,0x85,0x4f,0xa8,	    0x68,0x6b,0x81,0xb2,0x71,0x64,0xda,0x8b,0xf8,0xeb,0x0f,0x4b,0x70,0x56,0x9d,0x35,	    0x1e,0x24,0x0e,0x5e,0x63,0x58,0xd1,0xa2,0x25,0x22,0x7c,0x3b,0x01,0x21,0x78,0x87,	    0xd4,0x00,0x46,0x57,0x9f,0xd3,0x27,0x52,0x4c,0x36,0x02,0xe7,0xa0,0xc4,0xc8,0x9e,	    0xea,0xbf,0x8a,0xd2,0x40,0xc7,0x38,0xb5,0xa3,0xf7,0xf2,0xce,0xf9,0x61,0x15,0xa1,	    0xe0,0xae,0x5d,0xa4,0x9b,0x34,0x1a,0x55,0xad,0x93,0x32,0x30,0xf5,0x8c,0xb1,0xe3,	    0x1d,0xf6,0xe2,0x2e,0x82,0x66,0xca,0x60,0xc0,0x29,0x23,0xab,0x0d,0x53,0x4e,0x6f,	    0xd5,0xdb,0x37,0x45,0xde,0xfd,0x8e,0x2f,0x03,0xff,0x6a,0x72,0x6d,0x6c,0x5b,0x51,	    0x8d,0x1b,0xaf,0x92,0xbb,0xdd,0xbc,0x7f,0x11,0xd9,0x5c,0x41,0x1f,0x10,0x5a,0xd8,	    0x0a,0xc1,0x31,0x88,0xa5,0xcd,0x7b,0xbd,0x2d,0x74,0xd0,0x12,0xb8,0xe5,0xb4,0xb0,	    0x89,0x69,0x97,0x4a,0x0c,0x96,0x77,0x7e,0x65,0xb9,0xf1,0x09,0xc5,0x6e,0xc6,0x84,	    0x18,0xf0,0x7d,0xec,0x3a,0xdc,0x4d,0x20,0x79,0xee,0x5f,0x3e,0xd7,0xcb,0x39,0x48	},	{		0x8d,0x5b,0x89,0x21,0x1b,0x4f,0xe0,0xeb,0xc7,0x14,0x73,0x19,0x74,0x68,0x07,0x63,		0xaf,0x3b,0x08,0xa9,0x84,0xf9,0xda,0x23,0x5f,0x58,0x64,0xa2,0x6c,0x01,0xf8,0x77,		0xc5,0xbc,0x54,0x7b,0xe8,0x05,0xc9,0x99,0xd3,0x62,0x9e,0x0d,0x2d,0x00,0x72,0xac,		0x2e,0x87,0x27,0x1e,0x86,0x6a,0x31,0xd8,0xa3,0xf6,0x2a,0xde,0xae,0xb8,0x5a,0x40,		0x8a,0x65,0xdf,0x06,0xb3,0x18,0xc3,0x95,0x9f,0x81,0x20,0x48,0x39,0xba,0x45,0x02,		0xc2,0xee,0xd1,0x30,0x8f,0xa7,0x60,0xed,0x11,0xab,0x12,0x59,0xe5,0xc6,0x46,0x76,		0xd4,0xb6,0x75,0x83,0x92,0x3e,0x15,0xd9,0x3f,0x8b,0x0c,0xb9,0x9a,0x47,0x3a,0x0e,		0x4e,0xaa,0x5c,0xfe,0xbe,0x97,0x0b,0xfc,0xca,0xcf,0xea,0x44,0x7a,0x9d,0x4d,0xb7,		0x70,0x16,0x1c,0x41,0x82,0xdd,0x42,0x51,0xa1,0xe1,0xf2,0x9c,0xd0,0xf4,0x7e,0x09,		0x28,0x4b,0xe2,0x1a,0xcb,0x61,0xf3,0x13,0x4c,0x4a,0xe4,0xbf,0xd7,0xf1,0xa8,0x6b,		0x49,0xe6,0x8c,0x32,0xc0,0xff,0xe7,0xa5,0x3c,0x33,0xdc,0x29,0x26,0x34,0x0a,0x79,		0x8e,0xfb,0x66,0x80,0x67,0x6f,0xb5,0x5e,0xa0,0xd6,0x85,0xb0,0x78,0x2b,0xc1,0x7d,		0x9b,0x96,0x56,0x3d,0x90,0xef,0x03,0xb2,0x22,0x0f,0x53,0x17,0x5d,0x57,0xad,0xc8,		0xce,0x2c,0x2f,0xd2,0xc4,0x43,0x6e,0x55,0x52,0xcc,0x1d,0x04,0x36,0xe9,0xb4,0xfd,		0xcd,0x10,0x7c,0x24,0x6d,0xfa,0xbb,0xf5,0x7f,0x35,0xf7,0x98,0xa4,0x69,0x1f,0xa6,		0x91,0x37,0x94,0xd5,0x50,0x88,0xec,0x71,0xbd,0xf0,0x25,0xdb,0xb1,0x38,0x93,0xe3 	},	{		0x05,0x4b,0xa3,0xeb,0xf2,0xe5,0x6e,0x36,0x00,0x21,0x11,0xea,0x09,0x58,0xfa,0xdf,		0xf6,0xc3,0x73,0xcc,0xb8,0xac,0x94,0x28,0x03,0xe7,0x24,0xce,0x50,0x92,0x74,0x5d,		0xa7,0x1e,0xc6,0xb4,0xb5,0xb1,0x2f,0x40,0x01,0xed,0x72,0xf3,0x9d,0x13,0xe8,0x91,		0xd3,0x35,0xd9,0x85,0xc5,0x9c,0x9b,0x70,0xf4,0xf1,0x60,0xa8,0x38,0x44,0xe9,0x0e,		0x4f,0xd1,0x08,0xe6,0xfb,0x7d,0x49,0x96,0x8f,0x2e,0x77,0xbe,0xb6,0x34,0x78,0x81,		0x5b,0x04,0x59,0xfd,0x16,0xdc,0xaf,0xc9,0x52,0x15,0x80,0x51,0xec,0x66,0xd5,0xbd,		0x93,0x71,0xad,0xe2,0x75,0xcd,0x88,0x0f,0xf5,0x33,0x79,0xae,0xbf,0xa4,0xdb,0x54,		0x63,0x47,0xc1,0xff,0x4c,0x99,0xb7,0xf7,0x68,0x3e,0x1b,0x23,0x6d,0x17,0x43,0x9a,		0xd4,0x6f,0x95,0xc8,0xb3,0xa0,0x41,0xca,0x46,0x53,0x98,0x86,0x3a,0xb9,0xd0,0x82,		0x7c,0x83,0x3f,0x76,0x25,0x3c,0xc4,0xf8,0x26,0xe3,0x69,0x14,0x8b,0x1f,0xfc,0x4e,		0xf9,0x32,0xbc,0xd2,0x6b,0x1c,0x5f,0x62,0xa6,0x8c,0x0b,0xe4,0xcf,0x64,0x29,0xa1,		0x84,0x1d,0xe1,0xda,0x56,0xab,0x65,0x42,0x2c,0xf0,0x8d,0xe0,0x2d,0x2a,0x7f,0x45,		0x5e,0x8a,0xdd,0x8e,0x6c,0x1a,0xa5,0x12,0x7b,0xee,0x18,0xef,0x3d,0x9f,0xd8,0x2b,		0x3b,0xc7,0x67,0xba,0xcb,0x5c,0x06,0xd6,0x48,0x87,0x39,0xaa,0x57,0x89,0xfe,0xde,		0x37,0x27,0x0a,0x02,0x20,0x55,0xc2,0xc0,0xbb,0xb2,0x90,0x6a,0x0c,0xa2,0x10,0x4a,		0xa9,0x9e,0x4d,0x07,0x5a,0x97,0x7a,0xd7,0x30,0xb0,0x22,0x0d,0x61,0x31,0x7e,0x19	},	{		0x53,0xcb,0x49,0x6c,0x55,0x45,0x3f,0xf3,0xa3,0x3e,0x11,0x90,0xfb,0x8d,0x7a,0xca,		0xbe,0xcf,0x9d,0xce,0xcc,0x42,0x78,0x39,0x37,0xc1,0xc4,0xc6,0xec,0x00,0xd4,0xda,		0x65,0x9a,0x1a,0x06,0x74,0x47,0xd2,0x0b,0xc8,0x5b,0xa2,0x3d,0x26,0x01,0xd9,0x08,		0x6e,0xe4,0x2c,0x2a,0xb0,0xb3,0x48,0x73,0x9f,0xc9,0x4a,0x91,0xb7,0x64,0x63,0x5f,		0x2d,0xb9,0x19,0xba,0xee,0xde,0xa5,0x9b,0xc7,0x1b,0xed,0x88,0x50,0x7b,0x92,0xf4,		0x35,0x6a,0x29,0x89,0x02,0x46,0x57,0x6d,0xd8,0x0d,0x58,0x13,0x86,0x9e,0x04,0x79,		0x43,0xaf,0xe9,0xa1,0x4f,0xe1,0xe0,0x10,0x15,0x27,0xaa,0xc5,0x94,0xa6,0xa9,0x96,		0x93,0x3c,0x0e,0xfd,0xb6,0x0f,0xdd,0xff,0x5a,0xa8,0xb4,0x4e,0xbb,0xac,0x16,0xbf,		0xd1,0x66,0x83,0x36,0x1d,0x99,0xd5,0x38,0x1c,0x14,0xea,0x09,0x71,0x8f,0xfc,0xad,		0xd0,0x2b,0xf0,0x80,0x62,0x24,0xb5,0xc0,0x67,0x1e,0xb1,0xae,0xd7,0xe3,0x3b,0xef,		0x30,0xf1,0x69,0x2e,0x59,0x7e,0xe2,0x34,0x0c,0x6f,0x0a,0x70,0x8a,0xc3,0x98,0xdb,		0xf5,0xe8,0x4d,0x54,0x17,0x2f,0xeb,0x41,0x82,0xd6,0x7c,0xe5,0x77,0x1f,0x12,0x84,		0xbd,0x5c,0x61,0xab,0xdf,0xb2,0x8e,0x6b,0x51,0x5d,0x68,0x4c,0x97,0x25,0x9c,0xf7,		0x3a,0x8b,0x33,0xf6,0xb8,0xf8,0x32,0x81,0x8c,0x7d,0xa0,0xa7,0x5e,0xfa,0xf2,0x7f,		0x85,0x72,0xcd,0x56,0x20,0x75,0x07,0x03,0xfe,0xa4,0xd3,0xdc,0xc2,0x52,0x4b,0x05,		0x18,0x40,0xbc,0xf9,0x23,0x31,0x21,0x28,0x95,0xe7,0x60,0x76,0x87,0x22,0x44,0xe6	},	{		0x00, 0x01, 0x8e, 0xf4, 0x47, 0xa7, 0x7a, 0xba, 0xad, 0x9d, 0xdd, 0x98, 0x3d, 0xaa, 0x5d, 0x96, 		0xd8, 0x72, 0xc0, 0x58, 0xe0, 0x3e, 0x4c, 0x66, 0x90, 0xde, 0x55, 0x80, 0xa0, 0x83, 0x4b, 0x2a, 		0x6c, 0xed, 0x39, 0x51, 0x60, 0x56, 0x2c, 0x8a, 0x70, 0xd0, 0x1f, 0x4a, 0x26, 0x8b, 0x33, 0x6e,		0x48, 0x89, 0x6f, 0x2e, 0xa4, 0xc3, 0x40, 0x5e, 0x50, 0x22, 0xcf, 0xa9, 0xab, 0x0c, 0x15, 0xe1, 		0x36, 0x5f, 0xf8, 0xd5, 0x92, 0x4e, 0xa6, 0x04, 0x30, 0x88, 0x2b, 0x1e, 0x16, 0x67, 0x45, 0x93, 		0x38, 0x23, 0x68, 0x8c, 0x81, 0x1a, 0x25, 0x61, 0x13, 0xc1, 0xcb, 0x63, 0x97, 0x0e, 0x37, 0x41, 		0x24, 0x57, 0xca, 0x5b, 0xb9, 0xc4, 0x17, 0x4d, 0x52, 0x8d, 0xef, 0xb3, 0x20, 0xec, 0x2f, 0x32, 		0x28, 0xd1, 0x11, 0xd9, 0xe9, 0xfb, 0xda, 0x79, 0xdb, 0x77, 0x06, 0xbb, 0x84, 0xcd, 0xfe, 0xfc, 		0x1b, 0x54, 0xa1, 0x1d, 0x7c, 0xcc, 0xe4, 0xb0, 0x49, 0x31, 0x27, 0x2d, 0x53, 0x69, 0x02, 0xf5, 		0x18, 0xdf, 0x44, 0x4f, 0x9b, 0xbc, 0x0f, 0x5c, 0x0b, 0xdc, 0xbd, 0x94, 0xac, 0x09, 0xc7, 0xa2, 		0x1c, 0x82, 0x9f, 0xc6, 0x34, 0xc2, 0x46, 0x05, 0xce, 0x3b, 0x0d, 0x3c, 0x9c, 0x08, 0xbe, 0xb7, 		0x87, 0xe5, 0xee, 0x6b, 0xeb, 0xf2, 0xbf, 0xaf, 0xc5, 0x64, 0x07, 0x7b, 0x95, 0x9a, 0xae, 0xb6, 		0x12, 0x59, 0xa5, 0x35, 0x65, 0xb8, 0xa3, 0x9e, 0xd2, 0xf7, 0x62, 0x5a, 0x85, 0x7d, 0xa8, 0x3a, 		0x29, 0x71, 0xc8, 0xf6, 0xf9, 0x43, 0xd7, 0xd6, 0x10, 0x73, 0x76, 0x78, 0x99, 0x0a, 0x19, 0x91, 		0x14, 0x3f, 0xe6, 0xf0, 0x86, 0xb1, 0xe2, 0xf1, 0xfa, 0x74, 0xf3, 0xb4, 0x6d, 0x21, 0xb2, 0x6a, 		0xe3, 0xe7, 0xb5, 0xea, 0x03, 0x8f, 0xd3, 0xc9, 0x42, 0xd4, 0xe8, 0x75, 0x7f, 0xff, 0x7e, 0xfd	},	{		0xa7, 0xce, 0xf8, 0x6a, 0x2d, 0x2c, 0x85, 0x57, 0x47, 0x3b, 0xd5, 0xb2, 0x27, 0x8a, 0x7d, 0x24,		0xba, 0x0d, 0x36, 0x21, 0x31, 0x60, 0xa8, 0x5b, 0x7a, 0x3c, 0x5f, 0x6d, 0x49, 0x56, 0x3a, 0xca,		0x01, 0x9c, 0xa6, 0xb4, 0xf5, 0x39, 0xd2, 0xc4, 0x00, 0x08, 0x04, 0xf3, 0x02, 0x51, 0xf7, 0xb9,		0xf4, 0xbe, 0x92, 0x74, 0x69, 0x6c, 0x62, 0x4d, 0x8e, 0xb7, 0x4e, 0xfa, 0x53, 0xed, 0x5a, 0x17,		0xaa, 0x1c, 0x2b, 0xf1, 0x1d, 0x33, 0x65, 0x8d, 0x3d, 0x82, 0x1e, 0xe2, 0xa1, 0x6e, 0xb8, 0x52,		0x96, 0x9f, 0x30, 0xb1, 0x54, 0x26, 0xa3, 0xb3, 0x5d, 0xc6, 0x88, 0x86, 0x1b, 0x8b, 0x9e, 0xef,		0x9d, 0x34, 0x45, 0xf0, 0xb0, 0x1f, 0x12, 0xec, 0xad, 0xc2, 0x93, 0xe6, 0xe4, 0x4a, 0x59, 0x20,		0x98, 0x46, 0x16, 0x3f, 0xcc, 0x70, 0xa5, 0x32, 0xdd, 0x05, 0x67, 0x14, 0x7c, 0xd0, 0x35, 0x2f,		0x3e, 0xc5, 0x68, 0xfd, 0x94, 0x40, 0x99, 0xd1, 0xe0, 0x64, 0x8c, 0x7e, 0xbd, 0x5e, 0x0a, 0x28,		0x66, 0x07, 0x38, 0xff, 0xdc, 0xa4, 0x19, 0xd9, 0x4c, 0x7b, 0x23, 0x7f, 0x0b, 0xc3, 0x91, 0x11,		0x72, 0x95, 0x25, 0x75, 0xa2, 0x6f, 0x10, 0xfb, 0xd8, 0x9a, 0x61, 0xe8, 0xc7, 0x2e, 0x73, 0xe9,		0x58, 0xae, 0x81, 0xd4, 0x09, 0x48, 0x76, 0x79, 0xc0, 0xb6, 0x1a, 0x42, 0xac, 0x89, 0x78, 0xda,		0x83, 0x87, 0xcb, 0xc9, 0x4f, 0x15, 0xf9, 0x77, 0xa0, 0xe5, 0x63, 0xd3, 0x44, 0xe1, 0x43, 0xdb,		0x2a, 0xee, 0x13, 0x8f, 0xdf, 0xab, 0xd7, 0xbb, 0x4b, 0x6b, 0xc1, 0x03, 0x18, 0x0c, 0xd6, 0x06,		0xde, 0xeb, 0x37, 0xea, 0x5c, 0xcf, 0x29, 0xcd, 0x90, 0xf2, 0x41, 0xb5, 0x0f, 0xa9, 0x71, 0x84,		0x80, 0xbf, 0x97, 0xe7, 0xbc, 0x50, 0xc8, 0xfc, 0x55, 0xaf, 0x0e, 0xe3, 0x9b, 0x22, 0xf6, 0xfe	}}; //不能通过nonoverlapp测试/*static unsigned char Sbox[256]={};*/unsigned long t(unsigned long A,int k) /*非线性变换t*/{    unsigned char a[4]={0};    unsigned char b[4]={0};    unsigned long B=0;    int i=0;    for(i=0;i<4;i++)    {        a[i]=(A>>(i*8))&0xff; /*此处a[0]实际是标准文档中的a[3], 0xb[0]实际是标准文档中的b[3]*/        b[i]=Sbox[k][a[i]];        B|=(b[i]<<(i*8));    }    return B;}unsigned long T1(unsigned long A,int k) /*合成置换T*/{    unsigned long B=0;    unsigned long C=0;    B=t(A,k);    C=B^ROL(B,2)^ROL(B,10)^ROL(B,18)^ROL(B,24);/*线性变换L*/    return C;}unsigned long T2(unsigned long A,int k) /*合成置换T'*/{    unsigned long B=0;    unsigned long C=0;    B=t(A,k);    C=B^ROL(B,13)^ROL(B,23);/*线性变换L'*/    return C;}/*MK[4]是加密密钥, 0xrk[32]是轮密钥*/void Key_Expansion(unsigned long MK[],unsigned long rk[],int k){    unsigned long K[4]={0};    int i=0;    for(i=0;i<4;i++)        K[i]=MK[i]^FK[i];    for(i=0;i<ROUND;i++)    {        K[i%4]^=T2(K[(i+1)%4]^K[(i+2)%4]^K[(i+3)%4]^CK[i],k);        rk[i]=K[i%4];    }}/* X[4] 是明文, rk[32] 为轮密钥, Y[4] 是密文,k指示使用的S盒*/void ECB_Encryption(unsigned long X[], unsigned long rk[], unsigned long Y[],int k){    unsigned long tempX[4]={0};    int i=0;    for (i=0;i<4;i++)         tempX[i]=X[i];    for (i=0;i<ROUND;i++)         tempX[i%4]^=T1(tempX[(i+1)%4]^tempX[(i+2)%4]^tempX[(i+3)%4]^rk[i],k);    for (i=0;i<4;i++)         Y[i]=tempX[3-i];}/* X[4] 是密文, rk[32] 为轮密钥, Y[4] 是明文*/void ECB_Decryption(unsigned long X[], unsigned long rk[], unsigned long Y[],int k){    unsigned long tempX[4]={0};    int i=0;    for (i=0;i<4;i++)         tempX[i]=X[i];    for (i=0;i<ROUND;i++)         tempX[i%4]^=T1(tempX[(i+1)%4]^tempX[(i+2)%4]^tempX[(i+3)%4]^rk[(31-i)],k);    for (i=0;i<4;i++)         Y[i]=tempX[3-i];}int Is_equal(unsigned long X[],unsigned long Y[]){	int i=0;	for(i=0;i<4;i++)		if(X[i]!=Y[i]) return 0;	return 1;}//MK是256bit的密钥void SMS4256_Encryption(unsigned long X[], unsigned long MK[2][4], unsigned long Y[],int k){	ENUM_KEYSIZE keysize=BIT256;	unsigned long rk[32];	int i;//	Key_Expansion(MK[0],rk,k);	for(i=0;i<32;i++)	{		mkey[i]=MK[i>>4][i>>3];	}	KeyExpand( keysize, mkey);	for(i=0;i<32;i++)	{		rk[i]=((unsigned long)(w[59-i].w[0]))^((unsigned long)(w[59-i].w[1])<<8)^((unsigned long)(w[59-i].w[2])<<16)^((unsigned long)(w[59-i].w[3])<<24);	}	ECB_Encryption(X,rk,Y,k);//	Key_Expansion(MK[1],rk,k);/*	for(i=0;i<32;i++)	{		mkey[i]=MK[i>>4][i>>3];	}	KeyExpand( keysize, mkey);	for(i=0;i<32;i++)	{		rk[i]=((unsigned long)(w[59-i].w[0]))^((unsigned long)(w[59-i].w[1])<<8)^((unsigned long)(w[59-i].w[2])<<16)^((unsigned long)(w[59-i].w[3])<<24);	}	ECB_Encryption(Y,rk,X,k);	for(i=0;i<4;i++)		Y[i]=X[i];*/	free(key);	free(w);}/*int main(){    unsigned long key[4]={0x01234567,0x89abcdef,0x1234567,0x89abcde};    unsigned long roundkey[32]={0};    unsigned long plaintext[4]={0x01234567,0x89abcdef,0x01234567,0x89abcdef};    unsigned long P[128][4]={0};    unsigned long E[128][4]={0};    unsigned long C[128][4]={0};    int bits[128]={0};    unsigned long ciphertext[4]={0};    unsigned long test[4]={0};	unsigned long temp;    int i=0;    int j=0;    int k=0;	unsigned long sum=0;	__int64 jie=0;    for(i=0;i<128;i++)   {	for(j=0;j<4;j++)	{		P[i][j]=plaintext[j];	}	P[i][i>>5]=(P[i][i>>5])^(0x00000001<<(i%32));   }	for(i=0;i<128;i++)	{		for(j=0;j<4;j++)		{			C[i][j]=P[i][j]^plaintext[j];			for(k=0;k<32;k++)				{if((C[i][j]>>k)&1)  bits[i]++;}		}		if(bits[i]!=1) printf("ERROR:not one!");	}    //printf("RoundKeys:\n");    Key_Expansion(key,roundkey);   // for (i=0;i<ROUND;i++)//	 printf("rk[ %d ] = %08lx\n ", i,roundkey[i]); //   printf("Encrypt: \n");    ECB_Encryption(plaintext,roundkey,ciphertext);    for(i=0;i<128;i++)    {	 ECB_Encryption(P[i],roundkey,E[i]);         for(j=0;j<4;j++)	{		C[i][j]=E[i][j]^ciphertext[j];	}    }    for(i=0;i<128;i++)    {	for(j=0;j<4;j++)	{		for(k=0;k<32;k++)		{if((C[i][j]>>k)&1)  bits[i]++;}	}       printf("bits[%d]=%d ",i,bits[i]);	   if(i%6==0) printf("\n");     }    for (i=0;i<4;i++)	printf("ciphertext[ %d ] = %08lx\n ", i,ciphertext[i]);   for(i=0;i<128;i++)	for(j=0;j<4;j++)	{printf("P[%d][%d]=%08lx ",i,j,P[i][j]);if(j%3==0) printf("\n");}   // printf("Decrypt:\n ");//    ECB_Decryption(ciphertext,roundkey,test);   // for (i=0;i<4;i++)//	 printf("X[ %d ] = %08lx\n ", i,test[i]);    /*printf("Encrypt 2 times: \n");    for (i=0;i<2;i++)    {        ECB_Encryption(plaintext,roundkey,ciphertext);        for (j=0;j<4;j++)             plaintext[j]=ciphertext[j];    }    for (i=0;i<4;i++)	 printf("Y[ %d ] = %08lx \n",i,ciphertext[i]);	for(i=0;i<128;i++)		sum+=bits[i];	sum=sum>>7;	printf("平均改变位数:%d\n",sum);	jie=0;	while(!(Is_equal(plaintext,ciphertext)))	{		ECB_Encryption(plaintext,roundkey,ciphertext);		for(i=0;i<4;i++)		{			temp=plaintext[i];			plaintext[i]=ciphertext[i];			ciphertext[i]=temp;		}		jie++;		if(jie%10000==0) 	printf("阶数:%016x\r",jie); 	}	printf("阶数:%016x\n",jie);	getchar();    return 0;}*/

⌨️ 快捷键说明

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