计算程序执行10万次需要的时间:
总共需要175秒
加解密一次的时间小于:0.00175秒
纯计算加解密的时间会更短
去除IO操作后的时间
也就是说加解密一次的时间为0.07毫秒
1 /*------------------------------------------------------- 2 Data Encryption Standard 56位密钥加密64位数据 3 --------------------------------------------------------*/ 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <time.h> 7 #include "bool.h" // 位处理 8 #include "tables.h" 9 10 void BitsCopy(bool *DatOut, bool *DatIn, int Len); // 数组复制 11 12 void ByteToBit(bool *DatOut, char *DatIn, int Num); // 字节到位 13 void BitToByte(char *DatOut, bool *DatIn, int Num); // 位到字节 14 15 void BitToHex(char *DatOut, bool *DatIn, int Num); // 二进制到十六进制 64位 to 4*16字符 16 void HexToBit(bool *DatOut, char *DatIn, int Num); // 十六进制到二进制 17 18 void TablePermute(bool *DatOut, bool *DatIn, const char *Table, int Num); // 位表置换函数 19 void LoopMove(bool *DatIn, int Len, int Num); // 循环左移 Len长度 Num移动位数 20 void Xor(bool *DatA, bool *DatB, int Num); // 异或函数 21 22 void S_Change(bool DatOut[32], bool DatIn[48]); // S盒变换 23 void F_Change(bool DatIn[32], bool DatKi[48]); // F函数 24 25 void SetKey(char KeyIn[8]); // 设置密钥 26 void PlayDes(char MesOut[8], char MesIn[8]); // 执行DES加密 27 void KickDes(char MesOut[8], char MesIn[8]); // 执行DES解密 28 29 30 31 int main() 32 { 33 clock_t aaa, bbb; 34 int jjj = 0; 35 aaa = time(NULL); 36 while (jjj <100000) 37 { 38 int i = 0; 39 char MesHex[16] = { 0 }; // 16个字符数组用于存放 64位16进制的密文 40 char MyKey[8] = { 0 }; // 初始密钥 8字节*8 41 char YourKey[8] = { 0 }; // 输入的解密密钥 8字节*8 42 char MyMessage[8] = { 0 }; // 初始明文 43 44 /*-----------------------------------------------*/ 45 46 printf("Welcome! Please input your Message(64 bit):\n"); 47 //gets(MyMessage); // 明文 48 MyMessage[0] = '1'; 49 MyMessage[1] = '2'; 50 MyMessage[2] = '3'; 51 MyMessage[3] = '4'; 52 MyMessage[4] = '5'; 53 MyMessage[5] = '6'; 54 MyMessage[6] = '7'; 55 MyMessage[7] = '8'; 56 //MyMessage[0] = '\0'; 57 printf("Please input your Secret Key:\n"); 58 MyKey[0] = '1'; // 密钥 59 MyKey[1] = '2'; 60 MyKey[2] = '3'; 61 MyKey[3] = '4'; 62 MyKey[4] = '5'; 63 MyKey[5] = '6'; 64 MyKey[6] = '7'; 65 MyKey[7] = '8'; 66 //MyKey[8] = '\0'; 67 while (MyKey[i] != '\0') // 计算密钥长度 68 { 69 i++; 70 } 71 /* 72 while (i != 8) // 不是8 提示错误 73 { 74 printf("Please input a correct Secret Key!\n"); 75 gets(MyKey); 76 i = 0; 77 while (MyKey[i] != '\0') // 再次检测 78 { 79 i++; 80 } 81 }*/ 82 83 SetKey(MyKey); // 设置密钥 得到子密钥Ki 84 85 PlayDes(MesHex, MyMessage); // 执行DES加密 86 87 printf("Your Message is Encrypted!:\n"); // 信息已加密 88 for (i = 0; i < 16; i++) 89 { 90 printf("%c ", MesHex[i]); 91 } 92 printf("\n\n"); 93 94 printf("Please input your Secret Key to Deciphering:\n"); // 请输入密钥以解密 95 //gets(YourKey); // 得到密钥 96 YourKey[0] = '1'; 97 YourKey[1] = '2'; 98 YourKey[2] = '3'; 99 YourKey[3] = '4'; 100 YourKey[4] = '5'; 101 YourKey[5] = '6'; 102 YourKey[6] = '7'; 103 YourKey[7] = '8'; 104 //YourKey[8] = '\0'; 105 SetKey(YourKey); // 设置密钥 106 107 KickDes(MyMessage, MesHex); // 解密输出到MyMessage 108 109 printf("Deciphering Over !!:\n"); // 解密结束 110 for (i = 0; i < 8; i++) 111 { 112 printf("%c ", MyMessage[i]); 113 } 114 printf("\n\n"); 115 116 jjj++; 117 } 118 bbb = time(NULL); 119 printf("bbb-aaa= %f",(double)(bbb - aaa)); 120 system("pause"); 121 /*------------------------------------------------*/ 122 } 123 124 /*------------------------------- 125 把DatIn开始的长度位Len位的二进制 126 复制到DatOut后 127 --------------------------------*/ 128 void BitsCopy(bool *DatOut, bool *DatIn, int Len) // 数组复制 OK 129 { 130 int i = 0; 131 for (i = 0; i<Len; i++) 132 { 133 DatOut[i] = DatIn[i]; 134 } 135 } 136 137 /*------------------------------- 138 字节转换成位函数 139 每8次换一个字节 每次向右移一位 140 和1与取最后一位 共64位 141 --------------------------------*/ 142 void ByteToBit(bool *DatOut, char *DatIn, int Num) // OK 143 { 144 int i = 0; 145 for (i = 0; i<Num; i++) 146 { 147 DatOut[i] = (DatIn[i / 8] >> (i % 8)) & 0x01; 148 } 149 } 150 151 /*------------------------------- 152 位转换成字节函数 153 字节数组每8次移一位 154 位每次向左移 与上一次或 155 ---------------------------------*/ 156 void BitToByte(char *DatOut, bool *DatIn, int Num) // OK 157 { 158 int i = 0; 159 for (i = 0; i<(Num / 8); i++) 160 { 161 DatOut[i] = 0; 162 } 163 for (i = 0; i<Num; i++) 164 { 165 DatOut[i / 8] |= DatIn[i] << (i % 8); 166 } 167 } 168 169 170 /*---------------------------------- 171 二进制密文转换为十六进制 172 需要16个字符表示 173 -----------------------------------*/ 174 void BitToHex(char *DatOut, bool *DatIn, int Num) 175 { 176 int i = 0; 177 for (i = 0; i<Num / 4; i++) 178 { 179 DatOut[i] = 0; 180 } 181 for (i = 0; i<Num / 4; i++) 182 { 183 DatOut[i] = DatIn[i * 4] + (DatIn[i * 4 + 1] << 1) 184 + (DatIn[i * 4 + 2] << 2) + (DatIn[i * 4 + 3] << 3); 185 if ((DatOut[i] % 16)>9) 186 { 187 DatOut[i] = DatOut[i] % 16 + '7'; // 余数大于9时处理 10-15 to A-F 188 } // 输出字符 189 else 190 { 191 DatOut[i] = DatOut[i] % 16 + '0'; // 输出字符 192 } 193 } 194 195 } 196 197 /*--------------------------------------------- 198 十六进制字符转二进制 199 ----------------------------------------------*/ 200 void HexToBit(bool *DatOut, char *DatIn, int Num) 201 { 202 int i = 0; // 字符型输入 203 for (i = 0; i<Num; i++) 204 { 205 if ((DatIn[i / 4])>'9') // 大于9 206 { 207 DatOut[i] = ((DatIn[i / 4] - '7') >> (i % 4)) & 0x01; 208 } 209 else 210 { 211 DatOut[i] = ((DatIn[i / 4] - '0') >> (i % 4)) & 0x01; 212 } 213 } 214 } 215 216 // 表置换函数 OK 217 void TablePermute(bool *DatOut, bool *DatIn, const char *Table, int Num) 218 { 219 int i = 0; 220 static bool Temp[256] = { 0 }; 221 for (i = 0; i<Num; i++) // Num为置换的长度 222 { 223 Temp[i] = DatIn[Table[i] - 1]; // 原来的数据按对应的表上的位置排列 224 } 225 BitsCopy(DatOut, Temp, Num); // 把缓存Temp的值输出 226 } 227 228 // 子密钥的移位 229 void LoopMove(bool *DatIn, int Len, int Num) // 循环左移 Len数据长度 Num移动位数 230 { 231 static bool Temp[256] = { 0 }; // 缓存 OK 232 BitsCopy(Temp, DatIn, Num); // 将数据最左边的Num位(被移出去的)存入Temp 233 BitsCopy(DatIn, DatIn + Num, Len - Num); // 将数据左边开始的第Num移入原来的空间 234 BitsCopy(DatIn + Len - Num, Temp, Num); // 将缓存中移出去的数据加到最右边 235 } 236 237 // 按位异或 238 void Xor(bool *DatA, bool *DatB, int Num) // 异或函数 239 { 240 int i = 0; 241 for (i = 0; i<Num; i++) 242 { 243 DatA[i] = DatA[i] ^ DatB[i]; // 异或 244 } 245 } 246 247 // 输入48位 输出32位 与Ri异或 248 void S_Change(bool DatOut[32], bool DatIn[48]) // S盒变换 249 { 250 int i, X, Y; // i为8个S盒 251 for (i = 0, Y = 0, X = 0; i<8; i++, DatIn += 6, DatOut += 4) // 每执行一次,输入数据偏移6位 252 { // 每执行一次,输出数据偏移4位 253 Y = (DatIn[0] << 1) + DatIn[5]; // af代表第几行 254 X = (DatIn[1] << 3) + (DatIn[2] << 2) + (DatIn[3] << 1) + DatIn[4]; // bcde代表第几列 255 ByteToBit(DatOut, &S_Box[i][Y][X], 4); // 把找到的点数据换为二进制 256 } 257 } 258 259 // F函数 260 void F_Change(bool DatIn[32], bool DatKi[48]) // F函数 261 { 262 static bool MiR[48] = { 0 }; // 输入32位通过E选位变为48位 263 TablePermute(MiR, DatIn, E_Table, 48); 264 Xor(MiR, DatKi, 48); // 和子密钥异或 265 S_Change(DatIn, MiR); // S盒变换 266 TablePermute(DatIn, DatIn, P_Table, 32); // P置换后输出 267 } 268 269 270 271 void SetKey(char KeyIn[8]) // 设置密钥 获取子密钥Ki 272 { 273 int i = 0; 274 static bool KeyBit[64] = { 0 }; // 密钥二进制存储空间 275 static bool *KiL = &KeyBit[0], *KiR = &KeyBit[28]; // 前28,后28共56 276 ByteToBit(KeyBit, KeyIn, 64); // 把密钥转为二进制存入KeyBit 277 TablePermute(KeyBit, KeyBit, PC1_Table, 56); // PC1表置换 56次 278 for (i = 0; i<16; i++) 279 { 280 LoopMove(KiL, 28, Move_Table[i]); // 前28位左移 281 LoopMove(KiR, 28, Move_Table[i]); // 后28位左移 282 TablePermute(SubKey[i], KeyBit, PC2_Table, 48); 283 // 二维数组 SubKey[i]为每一行起始地址 284 // 每移一次位进行PC2置换得 Ki 48位 285 } 286 } 287 288 void PlayDes(char MesOut[8], char MesIn[8]) // 执行DES加密 289 { // 字节输入 Bin运算 Hex输出 290 int i = 0; 291 static bool MesBit[64] = { 0 }; // 明文二进制存储空间 64位 292 static bool Temp[32] = { 0 }; 293 static bool *MiL = &MesBit[0], *MiR = &MesBit[32]; // 前32位 后32位 294 ByteToBit(MesBit, MesIn, 64); // 把明文换成二进制存入MesBit 295 TablePermute(MesBit, MesBit, IP_Table, 64); // IP置换 296 for (i = 0; i<16; i++) // 迭代16次 297 { 298 BitsCopy(Temp, MiR, 32); // 临时存储 299 F_Change(MiR, SubKey[i]); // F函数变换 300 Xor(MiR, MiL, 32); // 得到Ri 301 BitsCopy(MiL, Temp, 32); // 得到Li 302 } 303 TablePermute(MesBit, MesBit, IPR_Table, 64); 304 BitToHex(MesOut, MesBit, 64); 305 } 306 307 void KickDes(char MesOut[8], char MesIn[8]) // 执行DES解密 308 { // Hex输入 Bin运算 字节输出 309 int i = 0; 310 static bool MesBit[64] = { 0 }; // 密文二进制存储空间 64位 311 static bool Temp[32] = { 0 }; 312 static bool *MiL = &MesBit[0], *MiR = &MesBit[32]; // 前32位 后32位 313 HexToBit(MesBit, MesIn, 64); // 把密文换成二进制存入MesBit 314 TablePermute(MesBit, MesBit, IP_Table, 64); // IP置换 315 for (i = 15; i >= 0; i--) 316 { 317 BitsCopy(Temp, MiL, 32); 318 F_Change(MiL, SubKey[i]); 319 Xor(MiL, MiR, 32); 320 BitsCopy(MiR, Temp, 32); 321 } 322 TablePermute(MesBit, MesBit, IPR_Table, 64); 323 BitToByte(MesOut, MesBit, 64); 324 }