1、DES加密算法C语言 实验报告材料DES实验报告一、 实验目的实现DES算法。二、 实验过程按照DES的算法流程设计,具体实施详见附件。三、 使用方法首先输入密钥,八位ASCII长,否则报错。然后输入读入文件名和写入文件名,必须以ASCII编码,否则不能使用。四、 实验结果将自身cpp文件进行加密解密,前后文件完全一样。见文件附录源代码:/ 滴一欸死.cpp : 定义控制台应用程序的入口点。/#include stdafx.h#include #include #include #include #include #include table.h/* Constant */#define EN
2、CRYPT_LENGTH 8 /length of each unit in encryption#define DECIPHER_LENGTH 4 /length of each unit in decipher#define MAX32 0xFFFFFFFF /mask of 32 bits/* Declaration */typedef unsigned long long bit64;typedef unsigned long long bit56;typedef unsigned long long bit48;typedef unsigned int bit32;typedef u
3、nsigned int bit28;/* File stream */FILE *fin, *fout;/* For debug */inline void printBite(bit64 num) while (num) printf(%d, num % 2); num = 1; printf(n);/* Transfer from char to bit in Encrtption */inline bit64 ToBit( char *in / source string );/* Transfer from char to bit in Deciphtering */inline bi
4、t64 DeToBit( char *in / source string );/* Transfer from bit to char */inline void ToBite( char *out, / out string bit64 num / source bits );/* Permutation */inline bit64 substitute( bit64 num, / source bits const int *table, / Permutation table size_t len / bits length );/* Bit recycle loop to left
5、 */inline bit28 MoveLeft( bit28 key, / source bits int len / bits length );/* Bit recycle loop to right */inline bit28 MoveRight( bit28 key, / source bits int len / bits length );/* Divide bits into two parts */inline void divide( bit64 num, / source bits int len, / length of each bits bit32 *L, / l
6、eft out bits bit32 *R / right out bits );/* S box */inline bit32 SChange( bit48 num / source bits );/* F box */inline bit32 FChange( bit32 num, / source bits bit48 key / secret key );/* Key initialization */inline void SetKey( char *in / string of key );/* Enryption */inline void DES( char *message
7、/ messages to be encrypted );/* Deciphering */inline void Decipher( char *message / messages to be deciphered );/* Initialization */inline void init();int main() init(); system(pause); return 0;/* Initialization */inline void init() /* Set secret key */ printf(Please input your secret key (8 digits)
8、:n); char key10000; scanf(%s, key); if (strlen(key) != 8) printf(ERROR Keyn); return; SetKey(key); /* Set mode Encryption or Deciphering */ printf(Please input the mode (E for Encrypt, D for Decipher):n); void (*p)(char*); int delta = 8; switch (getch() case E: p = DES; delta = 8; break; case D: p =
9、 Decipher; delta = 16; break; default: printf(ERROR!n); return; /* Load file */ printf(Please input the path of the in file:n); char message10000, in100, out100; scanf(%s, in); printf(Please input the path of the out file:n); scanf(%s, out); fin = freopen(in, r, stdin); fout = freopen(out, w, stdout
10、); /* If success */ if (!fin | !fout) printf(Error open file!n); return; /* Read file */ while (gets_s(message) for (int i = 0; i strlen(message); i += delta) p(message + i); printf(n); /* Close stream */ fclose(stdin); fclose(stdout); fclose(fin); fclose(fout);/* Transfer from char to bit in Encrtp
11、tion */inline bit64 ToBit(char *in) /* If valid */ if (!in) return 0; /* Copy char* */ char temp8; memset(temp, , 8 * sizeof(char); for (int i = 0; i strlen(in) & i ENCRYPT_LENGTH; i+) tempi = ini; /* Transfer to bit */ bit64 key = 0x0; for (int i = 0; i ENCRYPT_LENGTH; i+) key |= (bit64)tempi (ENCR
12、YPT_LENGTH * i); return key;/* Transfer from char to bit in Deciphtering */inline bit64 DeToBit(char *in) /* If valid */ if (!in) return 0; /* Copy char* */ char temp64 / DECIPHER_LENGTH; memset(temp, , 8 * sizeof(char); for (int i = 0; i = A) tempi = ini - 7; else if (ini = 0) tempi = ini - 0; /* T
13、ransfer to bit */ bit64 key = 0x0; for (int i = 0; i 64 / DECIPHER_LENGTH; i+) key |= (bit64)tempi (DECIPHER_LENGTH * i); return key;/* Transfer from bit to char */inline void ToBite(char *out, bit64 num) if (strlen(out) = ENCRYPT_LENGTH) out = (char*)malloc(sizeof(char) * (ENCRYPT_LENGTH + 1); mems
14、et(out, 0, sizeof(char) * (ENCRYPT_LENGTH + 1); for (int i = 0; i = 8; /* Permutation */inline bit64 substitute(bit64 num, const int *table, size_t len) bit64 out = 0; /* Calculation */ for (int i = 0; i (tablei - 1) & 1) i); return out;/* Bit recycle loop to left */inline bit28 MoveLeft(bit28 key,
15、int len) bit28 temp = 0; temp = key len; / left bits key |= temp; / compare key &= 0x0FFFFFFF; / delete highest four bits return key;/* Bit recycle loop to right */inline bit28 MoveRight(bit28 key, int len) bit28 temp = 0; temp = key (28 - len); / right bits key = key = len; *R = num & MAX32;/* S bo
16、x */inline bit32 SChange(bit48 num) bit32 key = 0; for (int i = 0; i 1) & 0x0F; / the middle four bits y = (num 5) & 1) 1) | (num & 1); / the first and the last bits key |= (Siyx = 6; / change to next return key;/* F box */inline bit32 FChange(bit32 num, bit48 key) bit48 temp = substitute(num, E, si
17、zeof(E) / sizeof(E0); temp = key; num = SChange(temp); return substitute(num, P, sizeof(P) / sizeof(P0);/* Key initialization */inline void SetKey(char *in) bit64 key = ToBit(in); bit28 C, D; key = substitute(key, PC1, sizeof(PC1) / sizeof(PC10); divide(key, 28, &C, &D); for (int i = 0; i 16; i+) C
18、= MoveLeft(C, Movei); D = MoveLeft(D, Movei); key = (bit64)C | (bit64)D 28); SubKeyi = substitute(key, PC2, 48); /* Enryption */inline void DES(char *message) bit64 BitMes = substitute(ToBit(message), IP, sizeof(IP) / sizeof(IP0); bit32 L, R, temp; divide(BitMes, 32, &L, &R); /* 16 rounds */ for (in
19、t i = 0; i 16; i+) temp = R; R = FChange(R, SubKeyi); R = L; L = temp; BitMes = (bit64)L | (bit64)R 32); BitMes = substitute(BitMes, IPR, sizeof(IPR) / sizeof(IPR0); /* print encrypted message */ for (int i = 0; i (i * 4); temp += (temp 9 ? 7 : 0); printf(%c, temp); /* Deciphering */inline void Deci
20、pher(char *message) bit64 BitMes = substitute(DeToBit(message), IP, sizeof(IP) / sizeof(IP0); bit32 L, R, temp; divide(BitMes, 32, &L, &R); /* 16 rounds */ for (int i = 15; i = 0; i-) temp = L; L = FChange(L, SubKeyi); L = R; R = temp; BitMes = (bit64)L | (bit64)R 32); BitMes = substitute(BitMes, IP
21、R, sizeof(IPR) / sizeof(IPR0); /* print deciphered messages */ for (int i = 0; i (i * 8); table.h文件#pragma once/* IP permutation for plaintext */const int IP64 = 58,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4, 62,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8, 57,49,41,33,25,17, 9, 1,59,51,43,35,27,1
22、9,11, 3, 61,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 7;/* IPR permutation to print */const int IPR64 = 40, 8,48,16,56,24,64,32,39, 7,47,15,55,23,63,31, 38, 6,46,14,54,22,62,30,37, 5,45,13,53,21,61,29, 36, 4,44,12,52,20,60,28,35, 3,43,11,51,19,59,27, 34, 2,42,10,50,18,58,26,33, 1,41, 9,49,17,57,25;
23、/*- premutation -*/* the expansion permutation */static int E48 = 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9,10,11,12,13,12,13,14,15,16,17, 16,17,18,19,20,21,20,21,22,23,24,25, 24,25,26,27,28,29,28,29,30,31,32, 1;/* Compression permutation */static int PC156 = 57,49,41,33,25,17, 9, 1,58,50,42,34,26,1
24、8, 10, 2,59,51,43,35,27,19,11, 3,60,52,44,36, 63,55,47,39,31,23,15, 7,62,54,46,38,30,22, 14, 6,61,53,45,37,29,21,13, 5,28,20,12, 4;/* Number of key bits shifted per round */static int Move16 = 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1;/* Compression permutation */static int PC248 = 14,17,11,24,
25、 1, 5, 3,28,15, 6,21,10, 23,19,12, 4,26, 8,16, 7,27,20,13, 2, 41,52,31,37,47,55,30,40,51,34,33,48, 44,49,39,56,34,53,46,42,50,36,29,32;/*- F function -*/* S boxes permutation */static int S8416 = /S1 14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7, 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8, 4,
26、1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0, 15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13, /S2 15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10, 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5, 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15, 13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9, /S3 10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1, 13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7, 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12, /S4 7,13,14, 3, 0, 6, 9