logo资料库

软件学院密码学实验二DES.doc

第1页 / 共30页
第2页 / 共30页
第3页 / 共30页
第4页 / 共30页
第5页 / 共30页
第6页 / 共30页
第7页 / 共30页
第8页 / 共30页
资料共30页,剩余部分请下载后查看
School of Software, Yunnan University
云南大学软件学院 School of Software, Yunnan University 成绩 学 期: 2013 秋季学期 课程名称: 密码技术 任课教师: 学生姓名: 学 号: 实验项目: 联系电话: 电子邮件: 实验二 完成提交时间:2013 年 10 月 5 日
密码技术实验报告 实验项目:实验二,第二题 实验要求 (内容) 实验环境 实现功能 实现 DES 密码对任意文件的加解密(命令行方式) Win7 32 位,Visual studio 2010(c 语言环境) 明文与密钥通过 DES 算法加密,密文与密钥通过 DES 算法解密。加解密形式为 cipher -e/-d key inputfile outputfile。对于加密来说,明文就是 inputfile, 密文就是 outputfile;对于解密来说,密文就是 inputfile,明文就是 outputfile。 Key 的长度必须为 8 个字节,否则会提示用户重新输入。另外,加解密形式不符合 要求时,会提示用户重新输入。达到题目要求。 //保存明文块、密文块、 char input[1024]={'\0'};//保存输入的加解密字符串 char inputfile[128]={'\0'};//保存输入文件路径名 char outputfile[128]={'\0'};//保存输出文件路径名 char plainBlock[8],cipherBlock[8],keyBlock[8]; 密钥块 char bKey[64]; char subKeys[16][48]; //保存 16 组子密钥 //保存 64 位密钥中每位的 1、0 值 //初始置换表IP int IP_Table[64] = {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, 56,48,40,32,24,16,8,0, 58,50,42,34,26,18,10,2, 60,52,44,36,28,20,12,4, 62,54,46,38,30,22,14,6}; //初始逆置换表P^-1 int IP_1_Table[64] = {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, 32,0,40,8,48,16,56,24};
//扩充置换表 int E_Table[48] = {31,0,1,2,3,4,3,4,5,6,7,8,7,8,9,10,11,12,11,12,13,14,15,16,15,16,17,18 ,19,20,19,20,21,22,23,24,23,24,25,26,27,28,27,28,29,30,31,0}; //置换函数P int P_Table[32] = {15,6,19,20,28,11,27,16, 0,14,22,25,4,17,30,9, 1,7,23,13,31,26,2,8, 18,12,29,5,21,10,3,24}; //S盒 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,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,9,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,9,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}}}; //置换选择1 int PC_1[56] = {56,48,40,32,24,16,8, 0,57,49,41,33,25,17, 9,1,58,50,42,34,26, 18,10,2,59,51,43,35, 62,54,46,38,30,22,14, 6,61,53,45,37,29,21, 13,5,60,52,44,36,28, 20,12,4,27,19,11,3}; //置换选择2 int PC_2[48] = {13,16,10,23,0,4,2,27, 14,5,20,9,22,18,11,3, 25,7,15,6,26,19,12,1, 40,51,30,36,46,54,29,39, 50,44,32,46,43,48,38,55, 33,52,45,41,49,35,28,31}; //对左移次数的规定 int MOVE_TIMES[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
//此处画模块结构图,函数的调用关系,并指明每个函数的接口 程序流程 代 码 与 代 码分析 Function1: Calls: int main(int argc, char *argv[ ]) DES_Encrypt() DES_Decrypt() //主函数 Called By: 无
Input(type):char input[1024] Output(type):路径为 outputfile 的 txt 文档 Return(type):0 Function2: *cipherFile)//加密文件 int DES_Encrypt(char *plainFile, char *keyStr,char Calls: Char8ToBit64(keyBlock,bKey); DES_MakeSubKeys(bKey,subKeys); DES_EncryptBlock(plainBlock,subKeys,cipherBlock); main() Called By: Input(type): char plainFile[128] char key[8] Output(type): char Return(type): OK cipherFile[128] Function3: subKeys[16][48], char cipherBlock[8])//加密块函数 DES_EncryptBlock(char int plainBlock[8], char Calls: Char8ToBit64(plainBlock,plainBits); DES_IP_Transform(plainBits); DES_E_Transform(copyRight); DES_XOR(copyRight,subKeys[cnt],48); DES_SBOX(copyRight); DES_P_Transform(copyRight); DES_Swap(plainBits,plainBits+32); DES_IP_1_Transform(plainBits); Bit64ToChar8(plainBits,cipherBlock); Called By: DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile) Input(type): char plainBlock[8] Output(type): Return(type): */ char subKeys[16][48] char cipherBlock[8] 0 Function4: 64 位 int Char8ToBit64(plainBlock,plainBits); //8 位字符串转化为 Calls: Called By: DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], ByteToBit(*(ch+cnt),bit+(cnt<<3)); char cipherBlock[8]) Input(type): char ch[8]
Output(type): char bit[64] Return(type): 0 */ Function5: int DES_IP_Transform(char data[64])//初始置换IP 无 Calls: Called By: DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], char cipherBlock[8]) Input(type): char data[64] char data[64] Output(type): Return(type): 0 */ Function6:int DES_E_Transform(char data[48]) //扩展置换(E表) Calls: 无 Called By: DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], char cipherBlock[8]) Input(type): char copyRight [32] Output(type): Return(type): char data[48] 0 */ Function7: int DES_XOR(char R[48], char L[48] ,int count) //XOR 异或 Calls: Called By:DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], 无 char cipherBlock[8]) Input(type): char L[48] int count Output(type):char R[48] Return(type): 0 */ Function8: int DES_SBOX(char data[48])//替代选择S盒 Calls: Called By: DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], 无 char cipherBlock[8]) Input(type):int S[8][4][16] Output(type): char data[48] Return(type): */ 0 Function9: int DES_P_Transform(char data[32])//置换P
Calls: Called 无 By:int DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], char cipherBlock[8]) Input(type): int P_Table[32] Output(type): Return(type): */ char data[32] 0 Function10: 交换 int DES_Swap(char left[32], char right[32])//左右32位互相 Calls: Called By: DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], 无 char cipherBlock[8]) Input(type): Output(type): Return(type): char left[32] char right[32] char left[32] char right[32] 0 */ Function11: int DES_IP_1_Transform(char data[64])//逆初始置换IP-1 Calls: Called By:DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], 无 char cipherBlock[8]) Input(type): int IP_1_Table[64] Output(type): Return(type): */ char data[64] 0 Function12: int Bit64ToChar8(char bit[64],char ch[8])//64位转8位 无 Calls: Called By: DES_EncryptBlock(char plainBlock[8], char subKeys[16][48], char cipherBlock[8]) Input(type): char bit[64] Output(type): Return(type): */ char ch[8] 0 Function13: *plainFile)//解密文件 int DES_Decrypt(char *cipherFile, char *keyStr,char Calls: DES_DecryptBlock(cipherBlock,subKeys,plainBlock) Char8ToBit64(keyBlock,bKey);
分享到:
收藏