您的当前位置:首页DES加密算法C语言 实验报告材料

DES加密算法C语言 实验报告材料

2020-02-07 来源:小侦探旅游网
实用标准

DES实验报告

一、 实验目的

实现DES算法。 二、 实验过程

按照DES的算法流程设计,具体实施详见附件。 三、 使用方法

首先输入密钥,八位ASCII长,否则报错。然后输入读入文件名和写入文件名,必须以ASCII编码,否则不能使用。 四、 实验结果

将自身cpp文件进行加密解密,前后文件完全一样。见文件

附录 源代码:

// 滴一欸死.cpp : 定义控制台应用程序的入口点。 //

#include \"stdafx.h\" #include #include #include #include #include #include \"table.h\"

/* Constant */

#define ENCRYPT_LENGTH 8 #define MAX32

/* Declaration */

typedef unsigned long long bit64; typedef unsigned long long bit56; typedef unsigned long long bit48; typedef unsigned int typedef unsigned int

/* File stream */ FILE *fin, *fout;

/* For debug */

inline void printBite(bit64 num) {

while (num) {

bit32; bit28;

//length of each unit in encryption //length of each unit in decipher //mask of 32 bits

#define DECIPHER_LENGTH 4

0xFFFFFFFF

文档大全

实用标准

}

/* Transfer from char to bit in Encrtption */ inline bit64 ToBit(

/* Transfer from char to bit in Deciphtering */ inline bit64 DeToBit(

/* Transfer from bit to char */ inline void ToBite(

/* Permutation */ inline bit64 substitute(

/* Bit recycle loop to left */ inline bit28 MoveLeft(

/* Bit recycle loop to right */ inline bit28 MoveRight(

/* Divide bits into two parts */ inline void divide(

bit64 num,

// source bits

bit28 key, // source bits int len );

// bits length

bit28 key, int len );

// source bits // bits length

bit64 num, size_t len );

// source bits // bits length

const int *table, // Permutation table char *out, bit64 num );

// out string // source bits

char *in // source string );

char *in // source string ); }

printf(\"\\n\");

printf(\"%d\", num % 2); num >>= 1;

文档大全

实用标准

/* S box */

inline bit32 SChange(

/* F box */

inline bit32 FChange(

/* Key initialization */ inline void SetKey(

/* Enryption */ inline void DES(

/* Deciphering */ inline void Decipher(

/* Initialization */ inline void init();

int main() { }

/* Initialization */ inline void init() {

init();

system(\"pause\");

char *message // messages to be deciphered );

char *message // messages to be encrypted );

char *in // string of key );

bit32 num, bit48 key );

// source bits // secret key

bit48 num );

// source bits

int len, // length of each bits bit32 *L, );

// left out bits

bit32 *R // right out bits

return 0;

文档大全

实用标准

/* Read file */

while (gets_s(message)) {

for (int i = 0; i < strlen(message); i += delta) {

p(message + i);

/* If success */ if (!fin || !fout) { }

printf(\"Error open file!\\n\"); return; /* Load file */

printf(\"Please input the path of the in file:\\n\"); char message[10000], in[100], out[100]; 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); /* 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 = Decipher; delta = 16; break; default: printf(\"ERROR!\\n\"); return; }

/* Set secret key */

printf(\"Please input your secret key (8 digits):\\n\"); char key[10000]; scanf(\"%s\", key); if (strlen(key) != 8) { }

SetKey(key);

printf(\"ERROR Key\\n\"); return;

文档大全

实用标准

}

/* Transfer from char to bit in Encrtption */ inline bit64 ToBit(char *in) { }

/* Transfer from char to bit in Deciphtering */ inline bit64 DeToBit(char *in) {

/* If valid */ if (!in) {

return key;

/* Transfer to bit */ bit64 key = 0x0;

for (int i = 0; i < ENCRYPT_LENGTH; i++) { }

key |= ((bit64)temp[i] << (ENCRYPT_LENGTH * i)); /* If valid */ if (!in) { }

/* Copy char* */ char temp[8];

memset(temp, ' ', 8 * sizeof(char));

for (int i = 0; i < strlen(in) && i < ENCRYPT_LENGTH; i++) { }

temp[i] = in[i]; return 0; /* Close stream */ fclose(stdin); fclose(stdout); fclose(fin); fclose(fout); }

}

printf(\"\\n\");

文档大全

实用标准

}

/* Transfer from bit to char */

inline void ToBite(char *out, bit64 num) {

for (int i = 0; i < ENCRYPT_LENGTH; i++) {

out[i] = num & 0xFF; num >>= 8;

if (strlen(out) <= ENCRYPT_LENGTH) { }

memset(out, 0, sizeof(char) * (ENCRYPT_LENGTH + 1));

out = (char*)malloc(sizeof(char) * (ENCRYPT_LENGTH + 1)); return key;

/* Transfer to bit */ bit64 key = 0x0;

for (int i = 0; i < 64 / DECIPHER_LENGTH; i++) { }

key |= ((bit64)temp[i] << (DECIPHER_LENGTH * i)); /* Copy char* */

char temp[64 / DECIPHER_LENGTH]; memset(temp, ' ', 8 * sizeof(char));

for (int i = 0; i < 64 / DECIPHER_LENGTH; i++) { }

if (in[i] >= 'A') { } else { }

if (in[i] >= '0') { }

temp[i] = in[i] - '0'; temp[i] = in[i] - '7';

}

return 0;

文档大全

实用标准

}

/* Permutation */

inline bit64 substitute(bit64 num, const int *table, size_t len) { }

/* Bit recycle loop to left */

inline bit28 MoveLeft(bit28 key, int len) { }

/* Bit recycle loop to right */

inline bit28 MoveRight(bit28 key, int len) { }

/* Divide bits into two parts */

inline void divide(bit64 num, int len, bit32 *L, bit32 *R) {

*L = *R = 0; *L = num & MAX32; num >>= len; bit28 temp = 0;

temp = key >> (28 - len); // right bits key = key << len; key |= temp; return key;

// left bits // compare

bit28 temp = 0;

temp = key << (28 - len); // right bits key = key >> len; key |= temp; return key;

key &= 0x0FFFFFFF;

// left bits // compare

// delete highest four bits

return out; /* Calculation */

for (int i = 0; i < len; i++) { }

out |= ((bit64)((num >> (table[i] - 1)) & 1) << i); bit64 out = 0; }

文档大全

实用标准

}

/* S box */

inline bit32 SChange(bit48 num) { }

/* F box */

inline bit32 FChange(bit32 num, bit48 key) { }

/* Key initialization */ inline void SetKey(char *in) {

key = substitute(key, PC1, sizeof(PC1) / sizeof(PC1[0])); divide(key, 28, &C, &D); for (int i = 0; i < 16; i++) { }

C = MoveLeft(C, Move[i]); D = MoveLeft(D, Move[i]);

key = (bit64)C | ((bit64)D << 28); SubKey[i] = substitute(key, PC2, 48); bit64 key = ToBit(in); bit28 C, D;

bit48 temp = substitute(num, E, sizeof(E) / sizeof(E[0])); temp ^= key;

num = SChange(temp);

return substitute(num, P, sizeof(P) / sizeof(P[0])); return key; bit32 key = 0;

for (int i = 0; i < 8; i++) { }

bit32 x, y;

x = (num >> 1) & 0x0F;

// the middle four bits // the first and the last bits

// permutate // change to next

y = (((num >> 5) & 1) << 1) | (num & 1); key |= (S[i][y][x] << (i * 4)); num >>= 6;

*R = num & MAX32;

文档大全

实用标准

}

/* Enryption */

inline void DES(char *message) { }

/* Deciphering */

inline void Decipher(char *message) {

/* 16 rounds */

for (int i = 15; i >= 0; i--) {

temp = L;

L = FChange(L, SubKey[i]); L ^= R; R = temp;

bit64 BitMes = substitute(DeToBit(message), IP, sizeof(IP) / sizeof(IP[0])); bit32 L, R, temp;

divide(BitMes, 32, &L, &R);

BitMes = (bit64)L | ((bit64)R << 32);

BitMes = substitute(BitMes, IPR, sizeof(IPR) / sizeof(IPR[0]));

/* print encrypted message */ for (int i = 0; i < 16; i++) { }

char temp = (0xF & (BitMes >> (i * 4))); temp += (temp > 9 ? '7' : '0'); printf(\"%c\", temp); /* 16 rounds */

for (int i = 0; i < 16; i++) { }

temp = R;

R = FChange(R, SubKey[i]); R ^= L; L = temp;

bit64 BitMes = substitute(ToBit(message), IP, sizeof(IP) / sizeof(IP[0])); bit32 L, R, temp;

divide(BitMes, 32, &L, &R);

文档大全

实用标准

}

table.h文件 #pragma once

/* IP permutation for plaintext */ const int IP[64] = { };

/* IPR permutation to print */ const int IPR[64] = { };

/*--------------------------- premutation ----------------------------*/

/* the expansion permutation */ static int E[48] = { };

/* Compression permutation */ static int PC1[56] = {

57,49,41,33,25,17, 9, 1,58,50,42,34,26,18, 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

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 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,19,11, 3, 61,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 7 /* print deciphered messages */ for (int i = 0; i < 8; i++) { }

printf(\"%c\", (0xFF & (BitMes >> (i * 8)))); BitMes = (bit64)L | ((bit64)R << 32);

BitMes = substitute(BitMes, IPR, sizeof(IPR) / sizeof(IPR[0])); }

文档大全

实用标准

};

/* Number of key bits shifted per round */ static int Move[16] = { };

/* Compression permutation */ static int PC2[48] = { };

/*------------- F function ---------------*/

/* S boxes permutation */ static int S[8][4][16] = {

//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, 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,10, 1, 2, 8, 5,11,12, 4,15, 13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9, 10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4, 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14, //S5

2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9, 14,17,11,24, 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

1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 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

文档大全

实用标准

};

/* P boxes permutation */ static int P[32] = { };

/* 16 subkey undefined */

static unsigned long long SubKey[16];

16, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10, 2, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25 14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6, 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14, 11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3, //S6

12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11, 10,15, 4, 2, 7,12, 0, 5, 6, 1,13,14, 0,11, 3, 8, 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6, 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13, //S7

4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1, 13, 0,11, 7, 4, 0, 1,10,14, 3, 5,12, 2,15, 8, 6, 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2, 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12, //S8

13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7, 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2, 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8, 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11

文档大全

因篇幅问题不能全部显示,请点此查看更多更全内容