Word格式
完美整理
DES实验报告
实验目的
实现DES算法。
实验过程
按照DES的算法流程设计,具体实施详见附件。
使用方法
首先输入密钥,八位ASCII长,否则报错。然后输入读入文件名和写入文件名,必须以ASCII编码,否则不能使用。
实验结果
将自身cpp文件进行加密解密,前后文件完全一样。见文件
附录
源代码:
// 滴一欸死.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include
#include
#include "table.h"
/* Constant */
#define ENCRYPT_LENGTH8//length of each unit in encryption
#define DECIPHER_LENGTH 4//length of each unit in decipher
#define MAX320xFFFFFFFF//mask of 32 bits
/* Declaration */
typedef unsigned long longbit64;
typedef unsigned long longbit56;
typedef unsigned long longbit48;
typedef unsigned intbit32;
typedef unsigned intbit28;
/* 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 bit64 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 */
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,// left 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// me