关于DES加密算法的C语言实现

合集下载

DES加解密算法C语言实现

DES加解密算法C语言实现

DES加解密算法C语言实现/s/blog_65d6476a0101k9ot.html 2013#include "stdio.h"#include "time.h"#include "stdlib.h"#define PLAIN_FILE_OPEN_ERROR -1#define KEY_FILE_OPEN_ERROR -2#define CIPHER_FILE_OPEN_ERROR -3#define OK 1;typedef char ElemType;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};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};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};int S[8][4][16] ={{{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}},{{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}},{{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}},{{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}},{{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}},{{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}},{{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}},{{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}}};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};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};int ByteT oBit(ElemType ch,ElemType bit[8]);int BitT oByte(ElemType bit[8],ElemType *ch);int Char8ToBit64(ElemType ch[8],ElemType bit[64]);int Bit64T oChar8(ElemType bit[64],ElemType ch[8]);int DES_MakeSubKeys(ElemType key[64],ElemType subKeys[16][48]);int DES_PC1_Transform(ElemType key[64], ElemType tempbts[56]);int DES_PC2_Transform(ElemType key[56], ElemType tempbts[48]);int DES_ROL(ElemType data[56], int time);int DES_IP_Transform(ElemType data[64]);int DES_IP_1_Transform(ElemType data[64]);int DES_E_Transform(ElemType data[48]);int DES_P_Transform(ElemType data[32]);int DES_SBOX(ElemType data[48]);int DES_XOR(ElemType R[48], ElemType L[48],int count);int DES_Swap(ElemType left[32],ElemType right[32]);int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]);int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48], ElemType plainBlock[8]);int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile);int DES_Decrypt(char *cipherFile, char *keyStr,char *plainFile);int ByteT oBit(ElemType ch, ElemType bit[8]){int cnt;for(cnt = 0;cnt < 8; cnt++){*(bit+cnt) = (ch>>cnt)&1;}return 0;}int BitT oByte(ElemType bit[8],ElemType *ch){int cnt;for(cnt = 0;cnt < 8; cnt++){*ch |= *(bit + cnt)<<cnt;}return 0;}int Char8ToBit64(ElemType ch[8],ElemType bit[64]){int cnt;for(cnt = 0; cnt < 8; cnt++){ByteToBit(*(ch+cnt),bit+(cnt<<3));}}int Bit64T oChar8(ElemType bit[64],ElemType ch[8]){int cnt;memset(ch,0,8);for(cnt = 0; cnt < 8; cnt++){BitToByte(bit+(cnt<<3),ch+cnt);}return 0;}int DES_MakeSubKeys(ElemType key[64],ElemType subKeys[16][48]){ElemType temp[56];int cnt;DES_PC1_Transform(key,temp);for(cnt = 0; cnt < 16; cnt++){DES_ROL(temp,MOVE_TIMES[cnt]);DES_PC2_Transform(temp,subKeys[cnt]);}return 0;}int DES_PC1_Transform(ElemType key[64], ElemType tempbts[56]){int cnt;for(cnt = 0; cnt < 56; cnt++){tempbts[cnt] = key[PC_1[cnt]];}}int DES_PC2_Transform(ElemType key[56], ElemType tempbts[48]){int cnt;for(cnt = 0; cnt < 48; cnt++){tempbts[cnt] = key[PC_2[cnt]];}return 0;}int DES_ROL(ElemType data[56], int time){ElemType temp[56];memcpy(temp,data,time);memcpy(temp+time,data+28,time);memcpy(data,data+time,28-time);memcpy(data+28-time,temp,time);memcpy(data+28,data+28+time,28-time);memcpy(data+56-time,temp+time,time);return 0;}int cnt;ElemType temp[64];for(cnt = 0; cnt < 64; cnt++){temp[cnt] = data[IP_Table[cnt]];}memcpy(data,temp,64);return 0;}int DES_IP_1_Transform(ElemType data[64]){ int cnt;ElemType temp[64];for(cnt = 0; cnt < 64; cnt++){temp[cnt] = data[IP_1_Table[cnt]];}memcpy(data,temp,64);return 0;}int DES_E_Transform(ElemType data[48]){ int cnt;ElemType temp[48];for(cnt = 0; cnt < 48; cnt++){temp[cnt] = data[E_Table[cnt]];}memcpy(data,temp,48);return 0;}int cnt;ElemType temp[32];for(cnt = 0; cnt < 32; cnt++){temp[cnt] = data[P_Table[cnt]];}memcpy(data,temp,32);return 0;}int DES_XOR(ElemType R[48], ElemType L[48] ,int count){ int cnt;for(cnt = 0; cnt < count; cnt++){R[cnt] ^= L[cnt];}return 0;}int DES_SBOX(ElemType data[48]){int cnt;int line,row,output;int cur1,cur2;for(cnt = 0; cnt < 8; cnt++){cur1 = cnt*6;cur2 = cnt<<2;line = (data[cur1]<<1) + data[cur1+5];row = (data[cur1+1]<<3) + (data[cur1+2]<<2)+ (data[cur1+3]<<1) + data[cur1+4];output = S[cnt][line][row];data[cur2] = (output&0X08)>>3;data[cur2+1] = (output&0X04)>>2;data[cur2+2] = (output&0X02)>>1;data[cur2+3] = output&0x01;}return 0;}int DES_Swap(ElemType left[32], ElemType right[32]){ElemType temp[32];memcpy(temp,left,32);memcpy(left,right,32);memcpy(right,temp,32);return 0;}int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]){ElemType plainBits[64];ElemType copyRight[48];int cnt;Char8T oBit64(plainBlock,plainBits);DES_IP_Transform(plainBits);for(cnt = 0; cnt < 16; cnt++){memcpy(copyRight,plainBits+32,32);DES_E_Transform(copyRight);DES_XOR(copyRight,subKeys[cnt],48);DES_SBOX(copyRight);DES_P_Transform(copyRight);DES_XOR(plainBits,copyRight,32);if(cnt != 15){DES_Swap(plainBits,plainBits+32);}}DES_IP_1_Transform(plainBits);Bit64ToChar8(plainBits,cipherBlock);return 0;}int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48],ElemType plainBlock[8]){ElemType cipherBits[64];ElemType copyRight[48];int cnt;Char8T oBit64(cipherBlock,cipherBits);DES_IP_Transform(cipherBits);for(cnt = 15; cnt >= 0; cnt--){memcpy(copyRight,cipherBits+32,32);DES_E_Transform(copyRight);DES_XOR(copyRight,subKeys[cnt],48);DES_SBOX(copyRight);DES_P_Transform(copyRight);DES_XOR(cipherBits,copyRight,32);if(cnt != 0){DES_Swap(cipherBits,cipherBits+32);}}DES_IP_1_Transform(cipherBits);Bit64ToChar8(cipherBits,plainBlock);return 0;}int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile){ FILE *plain,*cipher;int count;ElemType plainBlock[8],cipherBlock[8],keyBlock[8]; ElemType bKey[64];ElemType subKeys[16][48];if((plain = fopen(plainFile,"rb")) == NULL){return PLAIN_FILE_OPEN_ERROR;}if((cipher = fopen(cipherFile,"wb")) == NULL){return CIPHER_FILE_OPEN_ERROR;}memcpy(keyBlock,keyStr,8);Char8T oBit64(keyBlock,bKey);DES_MakeSubKeys(bKey,subKeys);while(!feof(plain)){if((count = fread(plainBlock,sizeof(char),8,plain)) == 8){ DES_EncryptBlock(plainBlock,subKeys,cipherBlock); fwrite(cipherBlock,sizeof(char),8,cipher);}}if(count){memset(plainBlock + count,'\0',7 - count);plainBlock[7] = 8 - count;DES_EncryptBlock(plainBlock,subKeys,cipherBlock); fwrite(cipherBlock,sizeof(char),8,cipher);}fclose(plain);fclose(cipher);return OK;}int DES_Decrypt(char *cipherFile, char *keyStr,char *plainFile){FILE *plain, *cipher;int count,times = 0;long fileLen;ElemType plainBlock[8],cipherBlock[8],keyBlock[8];ElemType bKey[64];ElemType subKeys[16][48];if((cipher = fopen(cipherFile,"rb")) == NULL){return CIPHER_FILE_OPEN_ERROR;}if((plain = fopen(plainFile,"wb")) == NULL){return PLAIN_FILE_OPEN_ERROR;}memcpy(keyBlock,keyStr,8);Char8T oBit64(keyBlock,bKey);DES_MakeSubKeys(bKey,subKeys);fseek(cipher,0,SEEK_END);fileLen = ftell(cipher);rewind(cipher);while(1){fread(cipherBlock,sizeof(char),8,cipher);DES_DecryptBlock(cipherBlock,subKeys,plainBlock);times += 8;if(times < fileLen){fwrite(plainBlock,sizeof(char),8,plain);}else{break;}}if(plainBlock[7] < 8){for(count = 8 - plainBlock[7]; count < 7; count++){if(plainBlock[count] != '\0'){break;}}}if(count == 7){fwrite(plainBlock,sizeof(char),8 - plainBlock[7],plain);}else{fwrite(plainBlock,sizeof(char),8,plain);}fclose(plain);fclose(cipher);return OK;}int main(){ DES_Encrypt("1.txt","key.txt","2.txt"); system("pause");DES_Decrypt("2.txt","key.txt","3.txt"); getchar();return 0;}。

DES加密算法的C语言实现

DES加密算法的C语言实现

试验名称:DES加密算法实现实验环境:VC++6.0试验原理:DES 使用一个56 位的密钥以及附加的8 位奇偶校验位,产生最大64 位的分组大小。

这是一个迭代的分组密码,使用称为Feistel 的技术,其中将加密的文本块分成两半。

使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去,但最后一个循环不交换。

DES 使用16 个循环,使用异或,置换,代换,移位操作四种基本运算。

算法流程设计:数据结构:#define PLAIN_FILE_OPEN_ERROR -1#define CIPHER_FILE_OPEN_ERROR -2#define OK 1//初始置换表IPint 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}; //逆初始置换表IP^-1int 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};//扩充置换表Eint 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};//置换函数Pint 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}}}; //置换选择1int 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};//置换选择2int 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};部分代码://加密单个分组int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]){ElemType plainBits[64];ElemType copyRight[48];int cnt;Char8ToBit64(plainBlock,plainBits);//初始置换(IP置换)DES_IP_Transform(plainBits);//16轮迭代for(cnt = 0; cnt < 16; cnt++){memcpy(copyRight,plainBits+32,32);//将右半部分进行扩展置换,从32位扩展到48位DES_E_Transform(copyRight);//将右半部分与子密钥进行异或操作DES_XOR(copyRight,subKeys[cnt],48);//异或结果进入S盒,输出32位结果DES_SBOX(copyRight);//P置换DES_P_Transform(copyRight);//将明文左半部分与右半部分进行异或DES_XOR(plainBits,copyRight,32);if(cnt != 15){//最终完成左右部的交换DES_Swap(plainBits,plainBits+32);}}//逆初始置换(IP^1置换)DES_IP_1_Transform(plainBits);Bit64ToChar8(plainBits,cipherBlock);return 0;}//解密单个分组int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48],ElemType plainBlock[8]){ElemType cipherBits[64];ElemType copyRight[48];int cnt;Char8ToBit64(cipherBlock,cipherBits);//初始置换(IP置换)DES_IP_Transform(cipherBits);//16轮迭代for(cnt = 15; cnt >= 0; cnt--){memcpy(copyRight,cipherBits+32,32);//将右半部分进行扩展置换,从32位扩展到48位DES_E_Transform(copyRight);//将右半部分与子密钥进行异或操作DES_XOR(copyRight,subKeys[cnt],48);//异或结果进入S盒,输出32位结果DES_SBOX(copyRight);//P置换DES_P_Transform(copyRight);//将明文左半部分与右半部分进行异或DES_XOR(cipherBits,copyRight,32);if(cnt != 0){//最终完成左右部的交换DES_Swap(cipherBits,cipherBits+32);}}//逆初始置换(IP^1置换)DES_IP_1_Transform(cipherBits);Bit64ToChar8(cipherBits,plainBlock);return 0;}//加密文件int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile){FILE *plain,*cipher;int count;ElemType plainBlock[8],cipherBlock[8],keyBlock[8];ElemType bKey[64];ElemType subKeys[16][48];if((plain = fopen(plainFile,"rb")) == NULL){return PLAIN_FILE_OPEN_ERROR;}if((cipher = fopen(cipherFile,"wb")) == NULL){return CIPHER_FILE_OPEN_ERROR;}//设置密钥memcpy(keyBlock,keyStr,8);//将密钥转换为二进制流Char8ToBit64(keyBlock,bKey);//生成子密钥DES_MakeSubKeys(bKey,subKeys);while(!feof(plain)){//每次读8个字节,并返回成功读取的字节数if((count = fread(plainBlock,sizeof(char),8,plain)) == 8){ DES_EncryptBlock(plainBlock,subKeys,cipherBlock);fwrite(cipherBlock,sizeof(char),8,cipher);}}if(count){//填充memset(plainBlock + count,'\0',7 - count);//最后一个字符保存包括最后一个字符在内的所填充的字符数量plainBlock[7] = 8 - count;DES_EncryptBlock(plainBlock,subKeys,cipherBlock);fwrite(cipherBlock,sizeof(char),8,cipher);}fclose(plain);fclose(cipher);return OK;}//解密文件int DES_Decrypt(char *cipherFile, char *keyStr,char *plainFile){ FILE *plain, *cipher;int count,times = 0;long fileLen;ElemType plainBlock[8],cipherBlock[8],keyBlock[8];ElemType bKey[64];ElemType subKeys[16][48];if((cipher = fopen(cipherFile,"rb")) == NULL){return CIPHER_FILE_OPEN_ERROR;}if((plain = fopen(plainFile,"wb")) == NULL){return PLAIN_FILE_OPEN_ERROR;}//设置密钥memcpy(keyBlock,keyStr,8);//将密钥转换为二进制流Char8ToBit64(keyBlock,bKey);//生成子密钥DES_MakeSubKeys(bKey,subKeys);//取文件长度fseek(cipher,0,SEEK_END); //将文件指针置尾fileLen = ftell(cipher); //取文件指针当前位置rewind(cipher); //将文件指针重指向文件头while(1){//密文的字节数一定是8的整数倍fread(cipherBlock,sizeof(char),8,cipher);DES_DecryptBlock(cipherBlock,subKeys,plainBlock); times += 8;if(times < fileLen){fwrite(plainBlock,sizeof(char),8,plain);}else{break;}}//判断末尾是否被填充if(plainBlock[7] < 8){for(count = 8 - plainBlock[7]; count < 7; count++){if(plainBlock[count] != '\0'){break;}}}if(count == 7){//有填充fwrite(plainBlock,sizeof(char),8 - plainBlock[7],plain);}else{//无填充fwrite(plainBlock,sizeof(char),8,plain);}fclose(plain);fclose(cipher);return OK;}试验结果:程序运行过程:。

des子密钥生成算法c语言

des子密钥生成算法c语言

des子密钥生成算法c语言在C语言中,可以使用DES(Data Encryption Standard)算法生成子密钥。

以下是一个简单的示例代码,展示如何使用DES算法生成子密钥。

```cinclude <>include <>include <>include <openssl/>int main() {// 初始化DES算法的密钥和数据DES_cblock key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; DES_cblock data = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE,0xF0};// 初始化DES算法的密钥和数据长度int key_length = sizeof(key);int data_length = sizeof(data);// 生成子密钥DES_key_schedule key_schedule;DES_set_key(&key, &key_schedule);// 输出子密钥for (int i = 0; i < DES_NUM_SUBKEYS; i++) {printf("Subkey %d: ", i);for (int j = 0; j < DES_BLOCK_SIZE; j++) {printf("%02X ", key_[i].b[j]);}printf("\n");}return 0;}```在上面的代码中,我们使用了OpenSSL库中的DES算法函数来生成子密钥。

具体来说,我们使用了`DES_set_key`函数来初始化DES算法的密钥和数据,并使用`DES_key_schedule`结构体来存储子密钥。

最后,我们使用循环输出了每个子密钥的内容。

DES算法的C语言实现

DES算法的C语言实现

DES算法的C语⾔实现利⽤C语⾔实现DES算法,分组密码原理过程很简单,但是在写的过程中检查了好久才发现错误原因,主要有两点:1.在加密过程16轮迭代过程中,最后⼀轮迭代运算后的结果并没有进⾏交换,即C=IP-1(R16,L16),这样做的⽬的是为了加密解密使⽤同⼀个算法2.在S盒的过程中,移位后应该加括号,否则+的优先级⾼于<<,会出错,下⾯是算法源码:1 #include "des.h"2 #include <stdio.h>3 #include <stdlib.h>45const unsigned char IP_table[64] = {658, 50, 42, 34, 26, 18, 10, 2,760, 52, 44, 36, 28, 20, 12, 4,862, 54, 46, 38, 30, 22, 14, 6,964, 56, 48, 40, 32, 24, 16, 8,1057, 49, 41, 33, 25, 17, 9, 1,1159, 51, 43, 35, 27, 19, 11, 3,1261, 53, 45, 37, 29, 21, 13, 5,1363, 55, 47, 39, 31, 23, 15, 714 };1516const unsigned char IPR_table[64] = {1740, 8, 48, 16, 56, 24, 64, 32,1839, 7, 47, 15, 55, 23, 63, 31,1938, 6, 46, 14, 54, 22, 62, 30,2037, 5, 45, 13, 53, 21, 61, 29,2136, 4, 44, 12, 52, 20, 60, 28,2235, 3, 43, 11, 51, 19, 59, 27,2334, 2, 42, 10, 50, 18, 58, 26,2433, 1, 41, 9, 49, 17, 57, 2525 };2627const unsigned char E_table[48] = {2832, 1, 2, 3, 4, 5,294, 5, 6, 7, 8, 9,308, 9, 10, 11, 12, 13,3112, 13, 14, 15, 16, 17,3216, 17, 18, 19, 20, 21,3320, 21, 22, 23, 24, 25,3424, 25, 26, 27, 28, 29,3528, 29, 30, 31, 32, 136 };3738const unsigned char P_table[32] = {3916, 7, 20, 21, 29, 12, 28, 17,401, 15, 23, 26, 5, 18, 31, 10,412, 8, 24, 14, 32, 27, 3, 9,4219, 13, 30, 6, 22, 11, 4, 2543 };4445const unsigned char PC1_table[56] = {4657, 49, 41, 33, 25, 17, 9,471, 58, 50, 42, 34, 26, 18,4810, 2, 59, 51, 43, 35, 27,4919, 11, 3, 60, 52, 44, 36,5063, 55, 47, 39, 31, 23, 15,517, 62, 54, 46, 38, 30, 22,5214, 6, 61, 53, 45, 37, 29,5321, 13, 5, 28, 20, 12, 454 };5556const unsigned char LOOP_table[16] = {571, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 158 };5960const unsigned char PC2_table[48] = {6114, 17, 11, 24, 1, 5,623, 28, 15, 6, 21, 10,6323, 19, 12, 4, 26, 8,6416, 7, 27, 20, 13, 2,6541, 52, 31, 37, 47, 55,6630, 40, 51, 45, 33, 48,6744, 49, 39, 56, 34, 53,6846, 42, 50, 36, 29, 3269 };7071const unsigned char sbox[8][4][16] = {72// S17314, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,740, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,754, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,7615, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,77//S27815, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,793, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,800, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,8113, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,82//S38310, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,8413, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,8513, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,861, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,87//S4887, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,8913, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,9010, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,913, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,92//S5932, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,9414, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,954, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,9611, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,97//S69812, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,9910, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,1009, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,1014, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,102//S71034, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,10413, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,1051, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,1066, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,107//S810813, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,1091, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,1107, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,1112, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11112 };113114void ByteToBit(unsigned char* out, const unsigned char* in, const int bits){115for(int i=0;i<bits;i++)116out[i]=(in[i/8]>>(7-i%8))&1;117 }118119void BitToByte(unsigned char* out, const unsigned char* in, const int bits){120 memset(out, 0, (bits + 7) / 8);121for(int i=0;i<bits;i++)122out[i/8]|=in[i]<<(7-i%8);123 }124125void Transform(unsigned char* out,const unsigned char* in, const unsigned char* table, const int len){ 126 unsigned char tmp[64] = {0};127for (int i = 0; i < len; i++)128 tmp[i] = in[table[i] - 1];129 memcpy(out, tmp, len);130 }131132void RotateL(unsigned char* in, const int len, int loop){133static unsigned char tmp[64];134 memcpy(tmp, in, len);135 memcpy(in, in + loop, len - loop);136 memcpy(in + len - loop, tmp, loop);137 }138139static unsigned char subKey[16][48] = { 0 };140void setKey(const unsigned char* in){141char key[64] = { 0 };142 ByteToBit(key, in, 64);143char temp[56]={0};144 Transform(temp, key, PC1_table, 56);145for(int i=0;i<16;i++){146 RotateL(temp, 28, LOOP_table[i]);147 RotateL(temp + 28, 28, LOOP_table[i]);148 Transform(subKey[i], temp, PC2_table, 48);149 }150 }151152void xor(unsigned char* in1,const unsigned char* in2,int len){153for(int i=0;i<len;i++)154 in1[i]^=in2[i];155 }156157void sbox_exchange(unsigned char* out,const unsigned char* in){158char row, column;159for (int i = 0; i < 8; i++){160char num = 0;161 row = (in[6 * i]<<1)+ in[6 * i + 5];162 column = (in[6 * i + 1] << 3) + (in[6 * i + 2] << 2) + (in[6 * i + 3] << 1) + in[6 * i + 4]; 163 num = sbox[i][row][column];164for (int j = 0; j < 4; j++)165 {166out[4 * i + j] = (num >> (3 - j)) & 1;167 }168 }169 }170171void F_func(unsigned char* out,const unsigned char* in,unsigned char* subKey){172 unsigned char temp[48]={0};173 unsigned char res[32]={0};174 Transform(temp, in, E_table, 48);175 xor(temp,subKey,48);176 sbox_exchange(res,temp);177 Transform(out, res, P_table, 32);178 }179180void encryptDES(unsigned char* out,const unsigned char* in, const unsigned char* key){ 181 unsigned char input[64] = { 0 };182 unsigned char output[64] = { 0 };183 unsigned char tmp[64] = { 0 };184 ByteToBit(input, in, 64);185 Transform(tmp, input, IP_table, 64);186char* Li = &tmp[0], *Ri = &tmp[32];187 setKey(key);188for(int i=0;i<16;i++){189char temp[32]={0};190 memcpy(temp,Ri,32);191 F_func(Ri, Ri,subKey[i]);192 xor(Ri, Li, 32);193 memcpy(Li,temp,32);194 }195 RotateL(tmp, 64, 32);//the input is LR,output is RL196 Transform(output, tmp, IPR_table, 64);197 BitToByte(out, output,64);198 }199200void decryptDES(unsigned char* out,const unsigned char* in, const unsigned char* key){ 201 unsigned char input[64] = { 0 };202 unsigned char output[64] = { 0 };203 unsigned char tmp[64] = { 0 };204 ByteToBit(input, in, 64);205 Transform(tmp, input, IP_table, 64);206char* Li = &tmp[0], *Ri = &tmp[32];207 setKey(key);208 RotateL(tmp, 64, 32);209for (int i = 0; i < 16; i++){210char temp[32] = { 0 };211 memcpy(temp, Li, 32);212 F_func(Li, Li,subKey[15 - i]);213 xor(Li, Ri, 32);214 memcpy(Ri, temp, 32);215 }216 Transform(output, tmp, IPR_table, 64);217 BitToByte(out, output, 64);218 }。

DES加解密算法C语言源代码

DES加解密算法C语言源代码

DES加解密算法C语言源代码以下是一个实现DES加解密算法的C语言源代码,包含了加密和解密函数。

请注意,这个代码只是为了演示DES算法的工作原理,并不是一个完整的、安全的加密算法实现。

```c#include <stdio.h>#include <stdint.h>typedef structuint8_t key[8];uint8_t subkeys[16][6];} DESKey;void generateSubkeys(uint8_t* key, uint8_t subkeys[16][6]) //略过子密钥生成算法的具体实现//这里只是假设生成的子密钥都是随机的,实际生成过程要更复杂for (int i = 0; i < 16; i++)for (int j = 0; j < 6; j++)subkeys[i][j] = (i+j) % 256;}}void DES(uint8_t* input, uint8_t key[8], uint8_t* output, int encrypt)//略过DES加密算法的具体实现DESKey desKey;for (int i = 0; i < 8; i++)desKey.key[i] = key[i];}generateSubkeys(key, desKey.subkeys);//这里只是假设输入输出是8字节长,实际上可以支持任意长度//执行加解密操作if (encrypt)printf("Encrypting: ");} elseprintf("Decrypting: ");}for (int i = 0; i < 8; i++)output[i] = encrypt ? input[i] ^ desKey.subkeys[0][i%6] : input[i] ^ desKey.subkeys[15][i%6];printf("%02X ", output[i]);}printf("\n");int maiuint8_t input[8] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0};uint8_t key[8] = {0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0x07,0x08};uint8_t output[8];DES(input, key, output, 1);DES(output, key, output, 0);return 0;```在这个代码中,`generateSubkeys` 函数用于生成 16 个子密钥,之后分别在加密和解密函数 `DES` 中使用。

C实现DES加密算法

C实现DES加密算法

C实现DES加密算法#include <stdio.h>#include <string.h>/***计算机网络数据加密算法* 加密算法:DES(Data Encryption Standard)*加密算法是对称密码算法,即加解密使用相同密钥。

**选择密钥:*DES加密算法和RC4加密算法都是对称加密,对称加密算法的典型代表,*在DES加密算法中,一个64位的密钥被进一步分解为16个子密钥*每个子密钥有48比特,因此这个子密钥总共有2^48=281,474,976,710,656个可能的值。

*分组:*并将明文按照每64比特一组进行处理。

*加密算法:*DES加密算法的加密算法包括16轮迭代过程*在每轮迭代中,64比特的明文被分成32位的左右两部分*经过16轮迭代,输出64位的密文。

**下面是实现DES算法的C语言代码:*///生成子密钥K1-K16void GenerateSubKey(unsigned char key[],unsigned char sub_key[][48]);//S盒代替void S_Replace(unsigned char out_data[],unsigned char in_data[]);//P盒代替void P_Replace(unsigned char out_data[],unsigned char in_data[]);//32位左移void L_Shift(unsigned char in_data[],unsigned char c);//XORvoid XOR(unsigned char out_data[],unsigned charin_data1[],unsigned char in_data2[],int len);//DES算法,加密void DES_Encrypt(unsigned char plain_text[],unsigned char cipher_text[],unsigned char key[])unsigned char L_i[17][32],R_i[17][32],tmp[32];int i;unsigned char sub_key[16][48];//生成子密钥GenerateSubKey(key,sub_key);//初始化左右两部分L_0\R_0for(i=0;i<64;i++)if(i<32)L_i[0][i] = plain_text[i];elseR_i[0][i-32] = plain_text[i];}//16轮迭代for(i=1;i<=16;i++)//备份R_i-1。

DES算法的C语言实现

DES算法的C语言实现

DES算法的程序实现说明:在VC++6.0中新建基于控制台的Win32应用程序,程序清单如下:#iinclude “memory.h”#include “stdio.h”Enum {ENCRYPT,DECRYPT}Void Des_Run(char Out[8],char In[8],bool Type=ENCRYPT); //设置密钥Void Des_SetKey(const char Key[8]);Static void F_func(bool In[32],const bool Ki[48]); //f函数Static void s_func(bool Out[32],const bool In[48]); //S盒代替Static void Transform(bool *out,bool *In,const char *Table,int len); //变换Static void Xor(bool *InA,const bool *InB,int len); //异或Static void RotateL(bool *In,int len,int loop);//循环左移//字节组转换成位组Static void ByteToBit(bool *out,const char *In,int bits);//位组转换成字节组Static void BitToByte(char *Out,const bool *In,int bits);//置换IP表Const static char IP_Table[64]={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};//逆置换IP-1表Const static char IPR_Table[64]={40,8,48,16,56,24,64,32,39,7,47,15,55,3138,6,46,14,54,22,62,30,37,5,45,13,53,21,61,2936,4,44,12,52,20,60,28,35,3,43,11,51,19,59,2734,2,42,10,50,18,58,26,23,1,41,9,49,17,57,25};//逆置换IP-1表Const static char IPR_Table[64]={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};//E位选择表Static const char E_Table[48]={32,1,2,3,4,5,6,7,8,9,8,9,10,11,12,13,14,15,16,17,16,17,18,19,20,21,22,23,24,25,24,25,26,27,28,29,30,31,32,1};//P换位表Const static char P_Table[32]={Const static char P_Table[32]={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 };//PC1选位表Const static char PC1_Table[56]={57,49,41,33,25,17,9,1,58,50,42,34,26,18, 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};//PC2选位表Const static char PC2_Table[48]={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};//左移位数表Const static char LOOP_Table[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};//S盒Const static char S_Box[8][4][16]={//S114,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,//S215,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,//S310,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,//S47,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,//S52,12,4,1,7,10,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,//S612,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,//S74,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,26,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,//S813,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};Static bool SubKey[16][48]; //16圈子密钥Void Des_Run(char Out[8],char In[8],bool Type){Static bool M[64],Tmp[32],*Li=&M[0],*Ri=&M[32]; ByteToBit(M,In,64);Transform (M,M,IP_Table,64);If (Type==ENCRYPT){For (int i=0;i<16;i++){memcpy(Tmp,ri,32);F_func(Ri,SubKey[i]);Xor(Ri,Li,32);Memcpy(Li,Tmp,32);}}else{For (int i=15;i>=0;i--) {Memcpy(Tmp,Li,32);F_func(Li,SubKey[i]);Xor(Li,Ri,32);Memcpy(Ri,Tmp,32);}}Transform(M,M,IPR_Table,64);BitToByte(Out,M,64);}Void Des_SetKey(const char Key[8]){Static bool K[64],*KL=&K[0],*KR=&K[28];ByteToBit(K,Key,64);Transform(K,K,PC1_Table,56);For (int i=0;i<16;i++){RotateL(KL,28,LOOP_Table[i]);RotateL(KR,28,LOOP_Table[i]);Transform(SubKey[i],K,PC2_Table,48);}}Void F_func(bool In[32],const bool Ki[48]){static bool MR[48];Transform(MR,In,E_Table,48);Xor(MR,Ki,48);S_func(In,MR);Transform(In,In,P_Table,32);}Void S_func(bool Out[32],const bool In[48]){For (char i=0,j,k;i<8;i++;In+=6,Out+=4){J=(In[0]<<1)+In[5];K=(In[1]<<3)+(In[2]<<2)+(In[3]<<1)+In[4];ByteToBit(Out,&S_Box[i][j][k],4);}}Void Transform(bool *Out,bool *In,const char *Table,int len) {Static bool Tmp[256];For (int i=0;i<len;i++)Tmp[i]=In[Table[i]-1];Memcpy(Out,Tmp,len);}Void Xor(bool *InA,const bool *InB,int len){For (int i=0;i<len;i++)InA[i]^=InB[i];}Void RotateL(bool *In,int len,int loop){Static bool Tmp[256];Memcpy(Tmp,In,loop);Memcpy(In,In+loop,len-loop);Memcpy(In+len-loop,Tmp,loop);}}Void ByteToBit(bool *Out,const char *In,int bits){For (int i=0;i<bits;i++)Out[i]=(In[i/8]>>(i%8)) &1;}Void BitToByte(char *Out,const bool *In,int bits){Memset (Out,0,(bits+7)/8);For (int i=0;i<bits;i++)Out[i/8]!=In[i]<<(i%8);}Void main(){char key[8]={1,9,8,0,9,1,7,2},str[]=”Hello”;Puts(“Before encrypting”);Puts(str);Des_SetKey(key);Des_Run(str,str,ENCRYPT); //调用Des_Run函数对输入的明文进行加密//其中第1个参数str是输出的密文,第2个参数str是输入的明文Puts(“After encrypting”);Puts(str);Puts(“After decrypting”);Des_Run(str,str,DECRYPT);Puts(str);}。

des密码算法程序c语言

des密码算法程序c语言

des密码算法程序c语言一、概述DES(数据加密标准)是一种常用的对称加密算法,它采用64位的密钥,对数据进行加密和解密。

本程序使用C语言实现DES算法,包括密钥生成、数据加密和解密等操作。

二、算法实现1.密钥生成:使用初始置换算法IP(56位)将明文转化为56位的分组,再将该分组经过一系列的逻辑函数F进行6轮处理,最终生成一个56位的密文。

其中密钥包括56位数据位和8位奇偶校验位。

2.数据加密:将需要加密的数据转化为56位的分组,再经过DES 算法处理,得到密文。

3.数据解密:将密文经过DES算法处理,还原成原始明文。

三、程序代码```c#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>//DES算法参数定义#defineITERATIONS6//加密轮数#defineKEY_LENGTH8//密钥长度,单位为字节#defineBLOCK_SIZE8//数据分组长度,单位为字节#definePADDINGPKCS7Padding//填充方式#defineMAX_INPUT_LENGTH(BLOCK_SIZE*2)//数据输入的最大长度//初始置换函数voidinit_permutation(unsignedcharinput[BLOCK_SIZE]){inti;for(i=0;i<BLOCK_SIZE;i++){input[i]=i;}}//逻辑函数F的定义voidlogic_function(unsignedcharinput[BLOCK_SIZE],unsigned charoutput[BLOCK_SIZE]){inti;for(i=0;i<BLOCK_SIZE;i++){output[i]=input[(i+1)%BLOCK_SIZE]^input[i]^(i+1)/BLOCK_SI ZE;}}//DES算法主函数voiddes_encrypt(unsignedchar*input,unsignedchar*output){ unsignedcharkey[KEY_LENGTH];//密钥数组unsignedchariv[BLOCK_SIZE];//初始置换的输入数组unsignedcharciphertext[MAX_INPUT_LENGTH];//密文数组unsignedcharpadding[BLOCK_SIZE];//填充数组unsignedintlength=strlen((char*)input);//数据长度(以字节为单位)unsignedintpadding_length=(length+BLOCK_SIZE-1)%BLOCK_SIZE;//需要填充的字节数unsignedintround=0;//加密轮数计数器unsignedintj=0;//数据指针,用于循环读取数据和填充数据intkey_offset=((1<<(32-KEY_LENGTH))-1)<<(32-(ITERATIONS*BLOCK_SIZE));//密钥索引值,用于生成密钥数组和填充数组的初始值unsignedintk=0;//DES算法中每个轮次的密钥索引值,用于生成每个轮次的密钥数组和填充数组的值unsignedintkplus1=(k+1)%((1<<(32-BLOCK_SIZE))-1);//DES算法中每个轮次的密钥索引值加一后的值,用于下一个轮次的密钥生成charseed[32];//使用MD5作为初始种子值生成随机数序列chartmp[MAX_INPUT_LENGTH];//临时变量数组,用于数据交换和中间计算结果存储等操作time_tt;//时间戳变量,用于生成随机数序列的种子值srand((unsignedint)time(&t));//设置随机数种子值,确保每次运行生成的随机数序列不同init_permutation(iv);//初始置换操作,将输入数据转化为56位分组(需要重复填充时)或一个随机的分组(不需要重复填充时)memcpy(key,key_offset,sizeof(key));//将初始化的密钥数组复制到相应的位置上,以便于接下来的轮次生成不同的密钥值memcpy(padding,seed,sizeof(seed));//将种子值复制到填充数组中,以便于接下来的轮次生成不同的随机数序列值for(round=0;round<ITERATIONS;round++){//进行加密轮次操作,每轮包括。

DES加密解密算法C语言代码实现

DES加密解密算法C语言代码实现

DES加密解密算法C语⾔代码实现代码:1 #include<stdio.h>2 #include<string.h>3 #include<stdlib.h>4/*------------------------5定义枚举型全局变量6------------------------*/7 typedef enum8 {9false = 0,10true = 111 } bool;1213// ⼗六轮⼦密钥14static bool SubKey[16][48]={0};1516/*---------------------*/17/*-------------------------------------------------------------18各种置换表19-------------------------------------------------------------*/20// IP置换表21const char IP_Table[64]={2258,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4,2362,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8,2457,49,41,33,25,17, 9, 1,59,51,43,35,27,19,11, 3,2561,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 726 };27// IP-1置换表28const char IPR_Table[64]={2940, 8,48,16,56,24,64,32,39, 7,47,15,55,23,63,31,3038, 6,46,14,54,22,62,30,37, 5,45,13,53,21,61,29,3136, 4,44,12,52,20,60,28,35, 3,43,11,51,19,59,27,3234, 2,42,10,50,18,58,26,33, 1,41, 9,49,17,57,2533 };3435// E扩展表36static char E_Table[48]={3732, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,388, 9,10,11,12,13,12,13,14,15,16,17,3916,17,18,19,20,21,20,21,22,23,24,25,4024,25,26,27,28,29,28,29,30,31,32, 141 };42// PC1置换表43static char PC1_Table[56]={4457,49,41,33,25,17, 9, 1,58,50,42,34,26,18,4510, 2,59,51,43,35,27,19,11, 3,60,52,44,36,4663,55,47,39,31,23,15, 7,62,54,46,38,30,22,4714, 6,61,53,45,37,29,21,13, 5,28,20,12, 448 };4950// pc2表51static char PC2_Table[48]={5214,17,11,24, 1, 5, 3,28,15, 6,21,10,5323,19,12, 4,26, 8,16, 7,27,20,13, 2,5441,52,31,37,47,55,30,40,51,34,33,48,5544,49,39,56,34,53,46,42,50,36,29,3256 };57// 移位表58static char Move_Table[16]={591, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 160 };61// S盒62static char S_Box[8][4][16]={63//S16414, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,650,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,664, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,6715,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,68//S26915, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,703,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,710,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,7213, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,73//S37410, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,7513, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,7613, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,771,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,78//S4797,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,8013, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,8110, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,823,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,83//S5842,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,8514,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,864, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,8711, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,88//S68912, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,9010,15, 4, 2, 7,12, 0, 5, 6, 1,13,14, 0,11, 3, 8,919,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,924, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,93//S7944,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,9513, 0,11, 7, 4, 0, 1,10,14, 3, 5,12, 2,15, 8, 6,961, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,976,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,98//S89913, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,1001,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,1017,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,1022, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11103 };104//P置换表105static char P_Table[32]={10616, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,1072, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25108 };109/*-------------------------------------------------------------------*/110111/*-----------------------------⾃定义函数-----------------------------*/112void SetKey(char My_key[8]); //⽣成16轮的⼦密钥;113void ByteToBit(bool * Data_out,char * Data_in,int Num); //字节转换成位;114void Change_bit(bool * Data_out,int Num);//⼆进制的位置进⾏转换;115void BitToByte(char My_message[8],bool * Message_in,int Num); //位转换成字节;116void TableReplace(bool *Data_out,bool *Data_in,const char *Table,int Num); //各种表的置换算法;117void Bitcopy(bool * Data_out,bool * Data_in,int Num); //⼆进制数组的拷贝118void Loop_bit(bool * Data_out,int movstep,int len); //左移位;119void Run_Des(char My_message[8],char HexMssage[16]);//des的轮加密算法120void Xor(bool * Message_out, bool * Message_in,int Num); //执⾏异或121void S_change(bool * Data_out, bool * Data_in); // S盒变换;122void HexToBit(bool * Data_out,char * Data_in,int Num); // ⼗六进制转⼆进制123void BitToHex(char * Data_out,bool * Data_in,int Num); //⼆进制转换成⼗六进制;124void Run_desDes(char My_message[8],char HexMessage[16]);// DES轮解密算法;125126/*--------------------------*/127128/*--------------------------主函数----------------------------------*/129int main()130 {131int i=0,j;132char My_key[8]={0}; //记录加密密钥;133char You_key[8]={0}; //解密密钥134char My_message[8]={0}; //明⽂135char Message_hex[16]={0};//16进制的密⽂136 printf("请输⼊你要加密的内容(8 Byte):\n");137 gets(My_message);138 printf("请输⼊你的加密密钥:\n");139 gets(My_key);140 i=strlen(My_key);141while(i!=8)142 {143 printf("请输⼊加密密钥(8 Byte)\n");144 gets(My_key);145 i=0;146 i=strlen(My_key);147 }148 SetKey(My_key); //⽣成16轮的加密⼦密钥;149 Run_Des(My_message,Message_hex); //des的轮加密过程150 printf("经过加密的密⽂为:\n");151for(i=0;i<16;i++)152 {153 printf("%c ",Message_hex[i]);154 }155 printf("\n");156 printf("请输⼊你的解密密钥(8 Byte):\n");157 gets(You_key);158 i=strlen(You_key);159while(i!=8)160 {161 printf("请输⼊解密密钥(8 Byte)\n");162 gets(You_key);164 i=strlen(You_key);165 }166 SetKey(You_key); //⽣成16轮的解密⼦密钥;167 Run_desDes(My_message,Message_hex);//解密;168 printf("解密结果为:\n");169for(i=0;i<8;i++)170 {171 printf("%c ",My_message[i]);172 }173 printf("\n");174return0;175 }176177/*--------------------具体函数定义----------------------*/178void Bitcopy(bool * Data_out, bool * Data_in,int Num) //⼆进制数组拷贝179 {180int i=0;181for(i=0;i<Num;i++)182 {183 Data_out[i]=Data_in[i];184 }185186 }187void Change_bit(bool * Data_out,int Num) //⼆进制的位置进⾏转换;188 {189int i,j;190static bool Temp[8]={0};191for(i=0;i<Num/8;i++)192 {193 Bitcopy(Temp,Data_out,Num/8);194for(j=0;j<Num/8;j++)195 {196 Data_out[j]=Temp[Num/8-1-j];197 }198 Data_out+=Num/8;199 }200 }201void ByteToBit( bool * Data_out,char * Data_in,int Num) //字节转位202 {203int i,j;204for(i=0;i<Num;i++)205 {206 Data_out[i]=(Data_in[i/8]>>(i%8))&0x01;207 }208//Change_bit(Data_out,Num);209 }210void BitToHex(char * Data_out, bool * Data_in,int Num) //⼆进制转⼗六进制211 {212int i;213for(i=0;i<Num/4;i++)214 {215 Data_out[i]=0;216 }217for(i=0;i<Num/4;i++)218 {219 Data_out[i]=Data_in[4*i]+Data_in[4*i+1]*2+Data_in[4*i+2]*4+Data_in[4*i+3]*8; 220if(Data_out[i]%16>9)221 {222 Data_out[i]=Data_out[i]%16+'7';223 }224else225 Data_out[i]=Data_out[i]%16+'0';226 }227 }228void HexToBit(bool * Data_out,char * Data_in,int Num) //⼗六进制转⼆进制229 {230int i;231for(i=0;i<Num;i++)232 {233if(Data_in[i/4]<='9')234 {235 Data_out[i]=((Data_in[i/4]-'0')>>(i%4))&0x01;236 }237else238 {239 Data_out[i]=((Data_in[i/4]-'7')>>(i%4))&0x01;240 }241 }242 }243void BitToByte(char My_message[8],bool * Message_in,int Num) //位转换成字节244 {245int i=0;246for(i=0;i<(Num/8);i++)248 My_message[i]=0;249 }250for(i=0;i<Num;i++)251 {252 My_message[i/8]|=Message_in[i]<<(i%8);253 }254 }255void TableReplace( bool *Data_out, bool * Data_in,const char *Table ,int Num) // 置换算法256 {257int i=0;258static bool Temp[256]={0};259for(i=0;i<Num;i++)260 {261 Temp[i]=Data_in[Table[i]-1];262 }263 Bitcopy(Data_out,Temp,Num);264 }265void Loop_bit(bool * Data_out,int movstep,int len)266 {267static bool Temp[256]={0};268 Bitcopy(Temp,Data_out,movstep);269 Bitcopy(Data_out,Data_out+movstep,len-movstep);270 Bitcopy(Data_out+len-movstep,Temp,movstep);271/*Temp=Data_out;272 Temp[movstep]='\0';273 Data_out=Data_out+movstep;274 Data_out+(len-movstep)=Temp;*/275 }276void Xor(bool * Message_out,bool * Message_in,int Num)//执⾏异或277 {278int i;279for(i=0;i<Num;i++)280 {281 Message_out[i]=Message_out[i]^Message_in[i];282 }283 }284void SetKey(char My_key[8])285 {286int i,j;287static bool Key_bit[64]={0}; //Key的⼆进制缓存;288static bool *Key_bit_L,*Key_bit_R;289 Key_bit_L=&Key_bit[0]; //key的左边28位;290 Key_bit_R=&Key_bit[28]; //key的右边28位;291 ByteToBit(Key_bit,My_key,64);292/* Change_bit(Key_bit,64) ;//⼆进制的位置进⾏转换;293 for(i=0;i<64;i++)294 {295 printf("%d ",Key_bit[i]);296 }297 printf("\n");298 printf("\n");*/299 TableReplace(Key_bit,Key_bit,PC1_Table,56);//pc-1 置换300for(i=0;i<16;i++)301 {302 Loop_bit(Key_bit_L,Move_Table[i],28);303 Loop_bit(Key_bit_R,Move_Table[i],28);304 TableReplace(SubKey[i],Key_bit,PC2_Table,48);//pc-2置换305 }306 }307void S_change(bool * Data_out, bool * Data_in) //S盒变换308 {309int i;310int r=0,c=0;//S盒的⾏和列;311for(i=0;i<8;i++,Data_in=Data_in+6,Data_out=Data_out+4)312 {313 r=Data_in[0]*2+Data_in[5]*1;314 c=Data_in[1]*8+Data_in[2]*4+Data_in[3]*2+Data_in[4]*1;315 ByteToBit(Data_out,&S_Box[i][r][c],4);316 }317 }318void F_change(bool Data_out[32],bool Data_in[48]) // f函数;319 {320int i;321static bool Message_E[48]={0}; //存放E置换的结果;322 TableReplace(Message_E,Data_out,E_Table,48);//E表置换323 Xor(Message_E,Data_in,48);324 S_change(Data_out,Message_E); // S盒变换325 TableReplace(Data_out,Data_out,P_Table,32); //P置换326 }327void Run_Des(char My_message[8],char HexMssage[16])//des轮加密算法;328 {329int i;330static bool Message_bit[64]={0};331static bool *Message_bit_L=&Message_bit[0],*Message_bit_R=&Message_bit[32]; 332static bool Temp[32]={0};333 ByteToBit(Message_bit,My_message,64);334/*Change_bit(Message_bit,64) ;//⼆进制的位置进⾏转换;335 for(i=0;i<64;i++)336 {337 printf("%d ",Message_bit[i]);338 }339 printf("\n");340 printf("\n");*/341 TableReplace(Message_bit,Message_bit,IP_Table,64);342for(i=0;i<16;i++)343 {344 Bitcopy(Temp,Message_bit_R,32);345 F_change(Message_bit_R,SubKey[i]);346 Xor(Message_bit_R,Message_bit_L,32);347 Bitcopy(Message_bit_L,Temp,32);348 }349 TableReplace(Message_bit,Message_bit,IPR_Table,64);350 BitToHex(HexMssage,Message_bit,64);//⼆进制转换成⼗六进制;351 }352void Run_desDes(char My_message[8],char HexMessage[16])// DES轮解密算法;353 {354int i=0;355static bool Message_bit[64]={0};356static bool * Message_bit_L=&Message_bit[0], * Message_bit_R=&Message_bit[32]; 357static bool Temp[32]={0};358 HexToBit(Message_bit,HexMessage,64);359 TableReplace(Message_bit,Message_bit,IP_Table,64);360for(i=15;i>=0;i--)361 {362 Bitcopy(Temp,Message_bit_L,32);363 F_change(Message_bit_L,SubKey[i]);364 Xor(Message_bit_L,Message_bit_R,32);365 Bitcopy(Message_bit_R,Temp,32);366 }367 TableReplace(Message_bit,Message_bit,IPR_Table,64);368 BitToByte(My_message,Message_bit,64);369 }。

用C语言实现DES加密和解密

用C语言实现DES加密和解密

用C语言实现DES加密与解密#include<iostream.h>int IP[64] = {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};int IP_1[64] = {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};int E[48] = {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};int P[32]={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 };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},PC2[48]={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,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};Char_to_Int(in,Msg_Int,8);//正确O_to_B(Msg_Int,Msg_Bin,8);//正确cout<<endl;Convert(Msg_Bin,Msg_Bin_PC1,PC1,56); Divide(Msg_Bin_PC1,C[0],D[0],56);for(i=0;i<16;i++){if(i==0){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==1){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==8){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==15){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else{RLC2(C[i],C[i+1],28);RLC2(D[i],D[i+1],28);Combine(C[i+1],D[i+1],Key_Bin,56); Convert(Key_Bin,out[i],PC2,48);}//生成子密钥正确void S_box(int in[],int out[]){int i;int SBox[8][64] ={{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 },{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 },{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 },{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 },{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, },{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 },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},{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}};int s1,s3,s2[8];for(i=0;i<8;i++){s1=in[i*6]*2+in[i*6+5]*1;s3=in[i*6+1]*8+in[i*6+2]*4+in[i*6+3]*2+in[i*6+4]*1; s2[i]=SBox[i][s1*16+s3];}int j;for(i=0;i<8;i++)for(j=i*4+3;j>=i*4;j--){out[j]=s2[i]%2; s2[i]=s2[i]/2;void Encode(unsigned char in[],unsigned char Final_H[],int K[16][48]) {int i;intORINT[8],ORBYTE[64],FinalBYTE[64],LR[64],R48[48],S_in[48],S_out[32],F_RL[32],Fi nalINT[8],L[17][32],R[17][32];char ORH[16];void DeCode(unsigned char in[],unsigned char Final_H[],int K[16][48]) {int i;intORINT[8],ORBYTE[64],LR[64],R48[48],S_in[48],S_out[32],F_RL[32],FinalBYTE[64],Fi nalINT[8],L[17][32],R[17][32];char ORH[16];Char_to_Int(in,ORINT,8);cout<<"!!!!!"<<endl;O_to_B(ORINT,ORBYTE,8);Convert(ORBYTE,LR,IP,64);Divide(LR,L[0],R[0],64);for(i=1;i<17;i++){COPY(R[i-1],L[i],32);Convert(R[i-1],R48,E,48);XOR(R48,K[16-i],S_in,48);S_box(S_in,S_out);Convert(S_out,F_RL,P,32);XOR(L[i-1],F_RL,R[i],32);}Combine(R[16],L[16],LR,64);Convert(LR,FinalBYTE,IP_1,64);B_to_H(FinalBYTE,Final_H);}void main(){int i;unsigned char Msg[8],UnCodeMsg[16],CodeMsg[16],Key[8],Msg_H[16],CodeChar[8];int SKey[16][48];int OrByte[64],OrInt[8],FinalByte[64],FinalInt[8];cout<<"请输入明文:"<<endl;for(i=0;i<8;i++)cin>>Msg[i];cout<<"请输入初始密钥:"<<endl;for(i=0;i<8;i++)cin>>Key[i];GetKey(Key,SKey);Encode(Msg,CodeMsg,SKey);cout<<"十六进制的密文:"<<endl;for(i=0;i<16;i++)cout<<CodeMsg[i];cout<<endl;cout<<"字符型密文:"<<endl;H_to_B(CodeMsg,FinalByte);B_to_O(FinalByte,FinalInt,8);Int_to_Char(FinalInt,CodeChar,8); for(i=0;i<8;i++)cout<<CodeChar[i];cout<<endl;cout<<"请输入十六进制的密文:"<<endl;for(i=0;i<16;i++)cin>>CodeMsg[i];B_to_O(FinalByte,FinalInt,8);Int_to_Char(FinalInt,CodeChar,8); cout<<"请输入解密密钥:"<<endl;for(i=0;i<8;i++)cin>>Key[i];GetKey(Key,SKey);DeCode(CodeChar,Msg_H,SKey);//for(i=0;i<16;i++)//cout<<Msg_H[i];cout<<endl;H_to_B(Msg_H,OrByte);B_to_O(OrByte,OrInt,8);Int_to_Char(OrInt,Msg,8);cout<<"明文是:"<<endl;for(i=0;i<8;i++)cout<<Msg[i];cout<<endl;}欢迎您的下载,资料仅供参考!。

DES加密C语言实现源代码

DES加密C语言实现源代码

void *memcpy( void *dest, const void *src, unsigned char count ){// ASSERT((dest != NULL)&&(src != NULL));unsigned char *temp_dest = (unsigned char *)dest;unsigned char *temp_src = (unsigned char *)src;while(count--) // 不对是否存在重叠区域进行判断{*temp_dest++ = *temp_src++;}return dest;}unsigned char *memset(unsigned char *dst,unsigned char value,unsigned char count) {unsigned char *start = dst;while (count--)*dst++ = value;return(start);}// 初始置换表IPunsigned char 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};//逆初始置换表IP^-1unsigned char 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};//扩充置换表Eunsigned char 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};//置换函数Punsigned char 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盒unsigned char 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}}};//置换选择1unsigned char 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};//置换选择2unsigned char 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};// 对左移次数的规定unsigned char MOVE_TIMES[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1}; //字节转换成二进制unsigned char ByteToBit(unsigned char ch, unsigned char bit[8]) {unsigned char cnt;for(cnt = 0;cnt < 8; cnt++){*(bit+cnt) = (ch>>cnt)&1;}return 0;}// 二进制转换成字节unsigned char BitToByte(unsigned char bit[8],unsigned char *ch){unsigned char cnt;for(cnt = 0;cnt < 8; cnt++){*ch |= *(bit + cnt)<<cnt;}return 0;}// 将长度为8的字符串转为二进制位串unsigned char Char8ToBit64(unsigned char ch[8],unsigned char bit[64]){unsigned char cnt;for(cnt = 0; cnt < 8; cnt++){ByteToBit(*(ch+cnt),bit+(cnt<<3));}return 0;}// 将二进制位串转为长度为8的字符串unsigned char Bit64ToChar8(unsigned char bit[64],unsigned char ch[8]){unsigned char cnt;memset(ch,0,8);for(cnt = 0; cnt < 8; cnt++){BitToByte(bit+(cnt<<3),ch+cnt);}return 0;}// 密钥置换1unsigned char DES_PC1_Transform(unsigned char key[64], unsigned char tempbts[56]) {unsigned char cnt;for(cnt = 0; cnt < 56; cnt++){tempbts[cnt] = key[PC_1[cnt]];}return 0;}//密钥置换2unsigned char DES_PC2_Transform(unsigned char key[56], unsigned char tempbts[48]) {unsigned char cnt;for(cnt = 0; cnt < 48; cnt++){tempbts[cnt] = key[PC_2[cnt]];}return 0;}//循环左移unsigned char DES_ROL(unsigned char data[56], unsigned char time){unsigned char temp[56];//保存将要循环移动到右边的位memcpy(temp,data,time);memcpy(temp+time,data+28,time);//前28位移动memcpy(data,data+time,28-time);memcpy(data+28-time,temp,time);//后28位移动memcpy(data+28,data+28+time,28-time);memcpy(data+56-time,temp+time,time);return 0;}//IP置换unsigned char DES_IP_Transform(unsigned char data[64]){unsigned char cnt;unsigned char temp[64];for(cnt = 0; cnt < 64; cnt++){temp[cnt] = data[IP_Table[cnt]];}memcpy(data,temp,64);return 0;}// IP逆置换unsigned char DES_IP_1_Transform(unsigned char data[64]) {unsigned char cnt;unsigned char temp[64];for(cnt = 0; cnt < 64; cnt++){temp[cnt] = data[IP_1_Table[cnt]];}memcpy(data,temp,64);return 0;}//扩展置换unsigned char DES_E_Transform(unsigned char data[48]) {unsigned char cnt;unsigned char temp[48];for(cnt = 0; cnt < 48; cnt++){temp[cnt] = data[E_Table[cnt]];}memcpy(data,temp,48);return 0;}//P置换unsigned char DES_P_Transform(unsigned char data[32]) {unsigned char cnt;unsigned char temp[32];for(cnt = 0; cnt < 32; cnt++){temp[cnt] = data[P_Table[cnt]];}memcpy(data,temp,32);return 0;}// 异或unsigned char DES_XOR(unsigned char R[48], unsigned char L[48] ,unsigned char count) {unsigned char cnt;for(cnt = 0; cnt < count; cnt++){R[cnt] ^= L[cnt];}return 0;}// S盒置换unsigned char DES_SBOX(unsigned char data[48]){unsigned char cnt;unsigned char line,row,output;unsigned char cur1,cur2;for(cnt = 0; cnt < 8; cnt++){cur1 = cnt*6;cur2 = cnt<<2;// 计算在S盒中的行与列line = (data[cur1]<<1) + data[cur1+5];row = (data[cur1+1]<<3) + (data[cur1+2]<<2)+ (data[cur1+3]<<1) + data[cur1+4];output = S[cnt][line][row];// 化为2进制data[cur2] = (output&0X08)>>3;data[cur2+1] = (output&0X04)>>2;data[cur2+2] = (output&0X02)>>1;data[cur2+3] = output&0x01;}return 0;}//交换unsigned char DES_Swap(unsigned char left[32], unsigned char right[32]){unsigned char temp[32];memcpy(temp,left,32);memcpy(left,right,32);memcpy(right,temp,32);return 0;}//生成子密钥unsigned char DES_MakeSubKeys(unsigned char key[64],unsigned char subKeys[16][48]) {unsigned char temp[56];unsigned char cnt;DES_PC1_Transform(key,temp);// PC1置换for(cnt = 0; cnt < 16; cnt++) //16轮跌代,产生16个子密钥{DES_ROL(temp,MOVE_TIMES[cnt]);// 循环左移DES_PC2_Transform(temp,subKeys[cnt]);//PC2置换,产生子密钥}return 0;}//加密单个分组unsigned char DES_EncryptBlock(unsigned char plainBlock[8], unsigned char subKeys[16][48], unsigned char cipherBlock[8]){unsigned char plainBits[64];unsigned char copyRight[48];unsigned char cnt;Char8ToBit64(plainBlock,plainBits);//初始置换(IP置换)DES_IP_Transform(plainBits);// 16轮迭代for(cnt = 0; cnt < 16; cnt++){memcpy(copyRight,plainBits+32,32);DES_E_Transform(copyRight); // 将右半部分进行扩展置换,从32位扩展到48位DES_XOR(copyRight,subKeys[cnt],48); // 将右半部分与子密钥进行异或操作DES_SBOX(copyRight); // 异或结果进入S盒,输出32位结果 DES_P_Transform(copyRight); // P置换DES_XOR(plainBits,copyRight,32); //将明文左半部分与右半部分进行异或 if(cnt != 15){DES_Swap(plainBits,plainBits+32);//最终完成左右部的交换}}DES_IP_1_Transform(plainBits); //逆初始置换(IP^1置换)Bit64ToChar8(plainBits,cipherBlock);return 0;}// 解密单个分组unsigned char DES_DecryptBlock(unsigned char cipherBlock[8], unsigned char subKeys[16][48],unsigned char plainBlock[8]){unsigned char cipherBits[64];unsigned char copyRight[48];short cnt;Char8ToBit64(cipherBlock,cipherBits);//初始置换(IP置换)DES_IP_Transform(cipherBits);// 16轮迭代for(cnt = 15; cnt >= 0; cnt--){memcpy(copyRight,cipherBits+32,32);//将右半部分进行扩展置换,从32位扩展到48位DES_E_Transform(copyRight);// 将右半部分与子密钥进行异或操作DES_XOR(copyRight,subKeys[cnt],48);//异或结果进入S盒,输出32位结果DES_SBOX(copyRight);// P置换DES_P_Transform(copyRight);//将明文左半部分与右半部分进行异或DES_XOR(cipherBits,copyRight,32);if(cnt != 0){// 最终完成左右部的交换DES_Swap(cipherBits,cipherBits+32);}}// 逆初始置换(IP^1置换)DES_IP_1_Transform(cipherBits);Bit64ToChar8(cipherBits,plainBlock);return 0;}//加密文件unsigned char DES_Encrypt(unsigned char *keyStr,unsigned char *plainFile,unsigned char *cipherFile){unsigned char keyBlock[8],bKey[64],subKeys[16][48];memcpy(keyBlock,keyStr,8); //设置密钥Char8ToBit64(keyBlock,bKey); //将密钥转换为二进制流 DES_MakeSubKeys(bKey,subKeys); //生成子密钥DES_EncryptBlock(plainFile,subKeys,cipherFile);return 1;}//解密文件unsigned char DES_Decrypt(unsigned char *keyStr,unsigned char *cipherFile,unsigned char *plainFile){// unsigned char plainBlock[8],cipherBlock[8],unsigned char bKey[64],keyBlock[8],subKeys[16][48];memcpy(keyBlock,keyStr,8); //设置密钥Char8ToBit64(keyBlock,bKey); //将密钥转换为二进制流 DES_MakeSubKeys(bKey,subKeys); //生成子密钥DES_DecryptBlock(cipherFile,subKeys,plainFile);return 1;}/****************************************************************************** ****************** 作者:win2kddk* 说明:3重DES是使用16字节密钥将8字节明文数据块进行3次DES加密和解密。

DES算法的C语言代码及实现

DES算法的C语言代码及实现

DES算法的C语言代码及实现首先新建头文件des_encode.H内容如下:void EncodeMain(); //EncodeMain functionvoid DecodeMain(); //Sorry ,it has not usedvoid Decode(int *str,int *keychar); //decode :input 8 chars,8 keycharsvoid Encode(int *str,int *keychar); //encode: input 8 chars,8 keycharsvoid keyBuild(int *keychar); //create key arrayvoid StrtoBin(int *midkey,int *keychar); //change into binaryvoid keyCreate(int *midkey2,int movebit,int i); //call by keyBuildvoid EncodeData(int *lData,int *rData,int *srt); //encodedata functionvoid F(int *rData,int *key); //F functionvoid Expand(int *rData,int *rDataP); //Expand functionvoid ExchangeS(int *rDataP,int *rData); //S-diagram changevoid ExchangeP(int *rData); //P changevoid FillBin(int *rData,int n,int s); // data to binary;call by S-Diagram change functionvoid DecodeData(int *str,int *lData,int *rData); //DecodeData from binaryint IP1[]={58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, //initial change62, 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,};int IP2[]={40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, //opp initial change38, 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};int s[][4][16]={{ //S-diagram array{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}},{{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}},{{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},{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12} },{{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} },{{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} },{{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} },{{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} },{{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} }};int Ex[48]={ 32,1,2,3,4,5, //Expand array 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};int P[32]={16,7,20,21, //P-change29,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};int PC1[56]={57,49,41,33,25,17,9, //PC-1 in keyBuild 1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,63,55,47,39,31,33,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4};int PC2[48]={14,17,11,24,1,5, //PC-2 in keyBuild3,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,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};再创建des.cpp内容如下:#include<stdio.h>#include<string.h>#include"des_encode.h"int key[16][48];char str[8];void main() //main function{EncodeMain();}void EncodeMain() //EncodeMain function {int i;char keychar[8];int key2[8];int strkey[8];printf("请输入8个要加密的字符:\n");scanf("%c",&str[i]);getchar();for(i=0;i<8;i++)strkey[i]=str[i];printf("\n输入明文的十六进制为:\n"); for(i=0;i<8;i++)printf("%10x",strkey[i]);printf("\n请输入密钥(8个字符):\n"); for(i=0;i<8;i++)scanf("%c",&keychar[i]);for(i=0;i<8;i++)key2[i]=keychar[i];getchar();// printf("%c",keychar[i]);Encode(strkey,key2);printf("\n加密后十六进制密文是:\n"); for(i=0;i<8;i++)printf("%10x",strkey[i]);printf("\n\n清输入解密密码\n");for(i=0;i<8;i++)scanf("%c",&keychar[i]);key2[i]=keychar[i];Decode(strkey,key2);for(i=0;i<8;i++)printf("%10x",strkey[i]);for(i=0;i<8;i++)str[i]=strkey[i];printf("\n明文为:\t");for(i=0;i<8;i++)printf("%c",str[i]);printf("\n\n");}void keyBuild(int *keychar){ //create key array int i,j;int movebit[]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};int midkey2[56];int midkey[64];StrtoBin(midkey,keychar);for(i=0;i<56;i++)midkey2[i]=midkey[PC1[i]-1];for(i=0;i<16;i++)keyCreate(midkey2,movebit[i],i);}void StrtoBin(int *midkey,int *keychar){ //change into binary int trans[8],i,j,k,n;n=0;for(i=0;i<8;i++){j=0;while(keychar[i]!=0){trans[j]=keychar[i]%2;keychar[i]=keychar[i]/2;j++;}for(k=j;k<8;k++)trans[k]=0;for(k=0;k<8;k++)midkey[n++]=trans[7-k];}}void keyCreate(int *midkey2,int movebit,int n){int i,temp[4];temp[0]=midkey2[0];temp[1]=midkey2[1];temp[2]=midkey2[28];temp[3]=midkey2[29];if(movebit==2){for(i=0;i<26;i++){midkey2[i]=midkey2[i+2];midkey2[i+28]=midkey2[i+30];}midkey2[26]=temp[0];midkey2[27]=temp[1];midkey2[54]=temp[2];midkey2[55]=temp[3]; }else{ for(i=0;i<27;i++){midkey2[i]=midkey2[i+1];midkey2[i+28]=midkey2[i+29];}midkey2[27]=temp[0];midkey2[55]=temp[2];}for(i=0;i<48;i++)key[n][i]=midkey2[PC2[i]-1];}void EncodeData(int *lData,int *rData,int *str){ //encodedata function int i,j,temp[8],lint,rint;//int h;int data[64];lint=0,rint=0;for(i=0;i<4;i++){j=0;while(str[i]!=0){temp[j]=str[i]%2;str[i]=str[i]/2;j++;}while(j<8)temp[j++]=0;for(j=0;j<8;j++)lData[lint++]=temp[7-j];j=0;while(str[i+4]!=0){temp[j]=str[i+4]%2;str[i+4]=str[i+4]/2;j++;}while(j<8)temp[j++]=0;for(j=0;j<8;j++)rData[rint++]=temp[7-j]; }for(i=0;i<32;i++){data[i]=lData[i];data[i+32]=rData[i];}for(i=0;i<32;i++){lData[i]=data[IP1[i]-1];//printf("P1:%5d:%5d,%5d\n",IP1[i],lData[i],data[IP1[i]-1]) ;rData[i]=data[IP1[i+32]-1];}}void F(int *rData,int *key){ //F functionint i,rDataP[48];Expand(rData,rDataP);for(i=0;i<48;i++){rDataP[i]=rDataP[i]^key[i];// printf("%10d",rDataP[i]);if((i+1)%6==0)printf("\n");}ExchangeS(rDataP,rData);ExchangeP(rData);}void Expand(int *rData,int *rDataP){ //Expand functionint i;for(i=0;i<48;i++)rDataP[i]=rData[Ex[i]-1];}void ExchangeS(int *rDataP,int *rData){ //S-diagram changeint i,n,linex,liney;linex=liney=0;for(i=0;i<48;i+=6){n=i/6; //printf("%10d\n",(rDataP[i]<<1));linex=(rDataP[i]<<1)+rDataP[i+5];liney=(rDataP[i+1]<<3)+(rDataP[i+2]<<2)+(rDataP[i+3]<<1)+rDataP[i+4];FillBin(rData,n,s[n][linex][liney]);}}void ExchangeP(int *rData){ //P changeint i,temp[32];for(i=0;i<32;i++)temp[i]=rData[i];for(i=0;i<32;i++)rData[i]=temp[P[i]-1];}void FillBin(int *rData,int n,int s){ // data to binary;call by S-Diagram change functionint temp[4],i;for(i=0;i<4;i++){temp[i]=s%2;s=s/2;for(i=0;i<4;i++)rData[n*4+i]=temp[3-i];}void DecodeData(int *str,int *lData,int *rData){ //DecodeData from binary int i;int a,b;int data[64];a=0,b=0;for(i=0;i<32;i++){data[i]=lData[i];data[i+32]=rData[i];}for(i=0;i<32;i++){lData[i]=data[IP2[i]-1];rData[i]=data[IP2[i+32]-1];}for(i=0;i<32;i++){a=(lData[i]&0x1)+(a<<1);b=(rData[i]&0x1)+(b<<1);if((i+1)%8==0){str[i/8]=a;a=0;//printf("%d",i/8);str[i/8+4]=b;b=0;//printf("%d",i/8+4);}}void Encode(int *str,int *keychar){ //encode: input 8 chars,8 keychars int lData[32],rData[32],temp[32],rDataP[48];int i,j;keyBuild(keychar);EncodeData(lData,rData,str);for(i=0;i<16;i++){for(j=0;j<32;j++)temp[j]=rData[j];F(rData,key[i]);for(j=0;j<32;j++){rData[j]=rData[j]^lData[j];}for(j=0;j<32;j++)lData[j]=temp[j];}DecodeData(str,rData,lData);}void Decode(int *str,int *keychar){ //decode :input 8 chars,8 keychars int lData[32],rData[32],temp[32],rDataP[48];int i,j;keyBuild(keychar);EncodeData(lData,rData,str); //这个位置for(i=0;i<16;i++){for(j=0;j<32;j++)temp[j]=rData[j];F(rData,key[15-i]);for(j=0;j<32;j++){rData[j]=rData[j]^lData[j];}for(j=0;j<32;j++){lData[j]=temp[j];}}DecodeData(str,rData,lData);}。

DES加密算法C 实现

DES加密算法C  实现

函数执行次序和调用关系关系如下:
3
6、实验结果
1、根据提示,输入任意 8 字节的原文,并将其转换为 64 为二进制明文:
2、将 64 为二进制明文进行初始置换:
3、将原 64 位明文分成左得子密钥。 按上述流程完成 16 轮迭代运算后进行终结置换, 得到 64 位密文 (整 个过程设计数据过多,没有一一输出,只给出最终截图内容)
2, 4, 6, 8, 1, 3, 5, 7
//逆置换矩阵 int IP_ANTEX[64]= { 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, 41, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; //扩展矩阵 int EXTEND[48]= {
6
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, 1, 2 }; //S 盒 int S[8][4][16]= { { {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} }, { {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} }, { {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} }, { { 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} }, { {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} }, {

C语言实现DES加密解密算法

C语言实现DES加密解密算法

C语言实现DES加密解密算法
最近几十年里,DES(Data Encryption Standard)算法的发展起到
了极其重要的作用。

Des算法是一种基于分组密码的算法。

算法将64位
的明文数据块按位分组成8个字节,每一组以8位为单位转换成一个64
位的密文数据块,采用16轮的分组加密,每次密码变化,保证加密强度。

本文详细介绍了DES算法的C语言实现,并分别介绍了加解密算法的实现
步骤以及DES加解密测试过程。

一、DES算法C语言实现
1.函数原型
DES算法的实现包括加密和解密函数,函数原型如下:
unsigned char* DesEncrypt(unsigned char *src, unsigned char
*key); // DES加密函数
unsigned char* DesDecrypt(unsigned char *src, unsigned char
*key); // DES解密函数
输入参数src是指明文源数据,key是加解密密钥,输出参数为一个
指向加解密结果的字符串指针。

2.加解密算法
(1)DES加密算法
DES加密算法步骤如下:
(i)初始置换:将64位明文块做一次IP置换得到L0R0。

(ii)迭代轮换:对L0R0经过16次迭代轮换后,最终结果为
L16R16
(iii)逆置换:L16R16进行逆置换得到64位密文。

(2)DES解密算法
DES解密算法步骤和DES加密算法步骤是一样的,只是将置换步骤改为逆置换,将轮换步骤改为逆轮换即可。

三、DES加解密测试
1.程序测试
在C语言编写完DES加解密算法之后。

DES加密算法代码

DES加密算法代码

实验二 DES加密算法用C语言编写DES算法代码,代码如下:#include <stdio.h>#include <string.h>//明文IP置换坐标int DataIP[] = {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};char preData[8]; //用户输入的明文char preKey[8]; //用户输入的密钥int Data64[64]; //8个明文通过十进制转二进制得到的64bit明文int Key64[64]; //8个密钥通过十进制转二进制得到的64bit密钥int DataTemp[64]; //Data64[]通过IP置换得到的64bit明文,用于得到L0和R0int L0[32],R0[32];int flagDorK=0; //标志位,如果是1的时候表示明文,是0的时候表示密文//此段全局变量服务于段雪琦所写所用函数//PC_1置换坐标表int PC_1[56] = {57,49,41,33,25,17,9,1,58,50,42,34,26,18,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};//PC_2置换坐标表int PC_2[48] = {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,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};//迭代左移位数数组int lsi[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};//逆初始置换表IP^-1int 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 KeyOutBox[48]={0}; //输出的子密钥数组int lsinumber=0; //用于记录是第i次加密,lsinumber等于i //sherry第二个函数所用//f操作中将R0扩展为48位的置换坐标表int fExpand48[48] = {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,31};//P变换置换坐标表int fPChange[32]={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};//S盒置换坐标表int sBox[8][4][16] = {{{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}},{{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, 4,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}},{{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}},{{ 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}},{{ 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}, {10, 6, 9, 0,12,11, 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}},{{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}},{{ 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}},{{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}}};/*---------------------生成R0 L0-----------------------* **将输入的分别为8个的明文和密钥转化为64bit的明文和密钥,****分别放入Data64[]和Key64[]; ** **将生成的64bit的明文通过IP置换得到L0[]和R0[] ** **-----------------------------------------------------*/ void R0andL0(char tempData[8]){int i,j,temp,y;int k=0;int tempBox[64];int mtemp=0;for(i=0; i<8; i++){for(j=7;j>=0;j--){temp=tempData[i]&(1<<j);if(0 == temp){tempBox[k]=0;k++;}else{tempBox[k]=1;k++;}}}//当flagDorK等于1时,为明文转化,生成L0和R0if(flagDorK==1){printf("转化为64bit明文\n");for(y=0;y<64;y++){Data64[y]=tempBox[y];tempBox[y]=0;printf("%d",Data64[y]);if(y== 7||y==15||y==23||y==39||y==47||y==55||y==63){printf(" ");}if(y==31){printf("\n");}}printf("\n");for(y=0;y<64;y++){DataTemp[y]=Data64[DataIP[y]-1];if(y<32){L0[y]=DataTemp[y];}else{R0[y-32]=DataTemp[y];}}printf("\n明文Data过IP置换后得到:\n"); printf("L0: ");for(i=0;i<32;i++){printf("%d",L0[i]);if(i== 7||i==15||i==23||i==31){printf(" ");}}printf("\nR0: ");for(i=0;i<32;i++){printf("%d",R0[i]);if(i== 7||i==15||i==23||i==31){printf(" ");}}printf("\n\n");}//当flagDorK等于0时,为密钥转化,生成密钥Key64 if(flagDorK==0){printf("转化为64bit的密钥\n");for(y=0;y<64;y++){Key64[y]=tempBox[y];tempBox[y]=0;printf("%d",Key64[y]);if(y== 7||y==15||y==23||y==39||y==47||y==55||y==63){printf(" ");}if(y==31){printf("\n");}}printf("\n\n");}}/*---------------输入函数-------------****------------------------------------*/void Input(){int i;printf("请输入明文:\n");for(i=0;i<8;i++){scanf("%c",&preData[i]);}flagDorK=1;R0andL0(preData);getchar();printf("请输入密钥:\n");for(i=0;i<8;i++){scanf("%c",&preKey[i]);}flagDorK=0;R0andL0(preKey);}//IP逆置换int DES_IP_1_Transform(int data[64]){int cnt;int temp[64];for(cnt = 0; cnt < 64; cnt++){temp[cnt] = data[IP_1_Table[cnt]];}memcpy(data,temp,64);for(cnt = 0; cnt < 64; cnt++){printf("%d",data[cnt]);}printf("\n");return 0;}//二进制转换成字节int BitToByte(int bit[8],int *ch){int cnt;for(cnt = 0;cnt < 8; cnt++){*ch |= *(bit + cnt)<<cnt;}for(cnt = 0;cnt < 8; cnt++){printf("%d",ch[cnt]);}return 0;}/*------------------生成子密钥函数------------------****传进来的64bit密钥通过PC_1置换坐标表变成56bit子密钥****此函数被调用16次即得到16个子密钥 ****--------------------------------------------------*/void subKey(int K[64]){int K0[56]={0};int KeyOut[56]={0};///*检测所用*/ intK0[56]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,2 2,23,24,25,26,27,28,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,1 8,19,20,21,22,23,24,25,26,27};int tempC[28]={0};int tempD[28]={0};int temp[56]={0};int i=0,j=0,lsisum=0;//lsisum为算出要移多少位for(i=0;i<=lsinumber-1;i++){lsisum+=lsi[i];}//得到第一个56位for(j=0;j<56;j++){K0[j]=K[PC_1[j]-1];}//移位操作 lsisum为移多少位,不论Ki中i为多少都可以用这个来做//tempC相当于教程中C0移位后的。

DES加密算法的C语言实现

DES加密算法的C语言实现

DES加密算法的C语言实现DES(Data Encryption Standard)是一种对称密钥加密算法,它的核心思想是将明文分成64位的数据块,并通过一系列的轮次操作对数据块进行加密,最终得到密文。

下面是一种用C语言实现DES加密算法的示例代码:```c#include <stdio.h>unsigned char initial_permutation(unsigned char block)unsigned char result = 0;result ,= (block & 0x80) >> 7;result ,= (block & 0x40) >> 5;result ,= (block & 0x20) >> 3;result ,= (block & 0x10) >> 1;result ,= (block & 0x08) << 1;result ,= (block & 0x04) << 3;result ,= (block & 0x02) << 5;result ,= (block & 0x01) << 7;return result;unsigned char final_permutation(unsigned char block)unsigned char result = 0;result ,= (block & 0x80) >> 7;result ,= (block & 0x40) >> 5;result ,= (block & 0x20) >> 3;result ,= (block & 0x10) >> 1;result ,= (block & 0x08) << 1;result ,= (block & 0x04) << 3;result ,= (block & 0x02) << 5;result ,= (block & 0x01) << 7;return result;void des_encrypt(unsigned char* plaintext, unsigned char* key, unsigned char* ciphertext)unsigned char block;unsigned char round_key;unsigned char i;// Initial Permutationblock = initial_permutation(*plaintext);// Round Permutationfor (i = 0; i < 16; i++)round_key = key[i];block ^= round_key;block = substitution(block);block = permutation(block);}// Final Permutation*ciphertext = final_permutation(block);int maiunsigned char plaintext = 0x55; // 明文unsigned char key[16] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}; // 密钥unsigned char ciphertext;des_encrypt(&plaintext, key, &ciphertext);printf("明文: 0x%02X\n", plaintext);printf("密钥: ");for (unsigned char i = 0; i < 16; i++)printf("%02X ", key[i]);}printf("\n");printf("密文: 0x%02X\n", ciphertext);return 0;```上述代码实现了DES算法的加密功能。

DES算法(基于某C语言,加密解密代码)

DES算法(基于某C语言,加密解密代码)

/* Note:Your choice is C IDE */#include"stdio.h"#include"string.h"#define uchar unsignedchar/*************************************************************************** *******************************明文转换声明局部*******************************///IP1置换表int IP_1[64]={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};//IP2逆置换表int IP_2[64]={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};//E扩展置换表int E_case[48]={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};//S盒压缩int S1[4][16]={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};int S2[4][16]={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};int S3[4][16]={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};int S4[4][16]={ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4,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};int S5[4][16]={ 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};int S6[4][16]={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,4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13};int S7[4][16]={ 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};int S8[4][16]={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};//P盒置换int Permute[32]={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};/*************************************************************************** ***************************字节与二进制相互变换局部******************************************************************************************************* *///字节转换成二进制int ByteToBit(char ch,char bit[8]){uchar x;for(x=0;x<8;x++){*(bit+x)=((ch<<x)&0x80)>>7;}return 0;}//字符串转换成二进制位串int Char8ToBit64(char ch[8],char bit[64]) {uchar x;for(x=0;x<8;x++){ByteToBit(*(ch+x),bit+(x<<3)); }return 0;}//二进制转换成字节int BitToByte(char bit[8],char*ch){uchar x;for(x=0;x<8;x++){*ch|=*(bit+x)<<(7-x);}return 0;}//将二进制串转换成字符串int Bit64ToChar8(char bit[64],char ch[8]){uchar x;memset(ch,0,8); //把ch[8]全部清零。

DES加密算法的C语言实现

DES加密算法的C语言实现
//密钥置换 unsigned char Key[]= {
57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 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 };
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 };
FILE *input = fopen( argv[1], "r"); if(ferror( input ))
return 0 ; //创建文件 用于输出密文 FILE *encyption = fopen(argv[2],"w"); if(ferror( encyption ))
return 0 ; //创建文件 用于输出解密后的明文 FILE *decyption = fopen(argv[3],"w"); if(ferror( decyption ))
};
//S-盒置换 unsigned char S_Box[8][64] = {
/* S1 */
Generated by Foxit PDF Creator © Foxit Software For evaluation only.
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

信息与网络安全实验报告计算机科学与技术学院网络工程1202班郭尚秀1208020204/********DES密码的加密过程*******************1.将字母转化为二进制数(明文&密文)分成两组完成*2.对明文m进行初始IP置换完成*3.对密钥k进行密钥置换完成*4.对密钥k进行压缩置换完成*5.对R0进行扩展变换32->48 完成*6.结果和k进行异或运算完成*7.将结果分成8组,通过8个s盒完成*8.对s盒的输出序列进行P置换完成*9.对p置换的结果与L0进行异或运算完成********************************************/#include<stdio.h>int ip[] = { //IP置换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};int jiou[] = {7,15,23,31,39,47,55,63 //进行密钥添加奇偶校验位使用};int ki[] = { //密钥置换57,49,41,33,25,17, 9, 1,58,50,42,34,26,18,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};int kyasuo[]= { //对密钥进行压缩置换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,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};int mkuozhan[]= {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};int s[8][4][16] = { //8个s盒{{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}},{{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, 4,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}},{{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}},{{ 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}},{{ 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},{10, 6, 9, 0,12,11, 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}},{{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}},{{ 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}},{{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}}};int p[32] = { //最后的p置换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};void main(){char mingwen1[100];int mingwen2[100];int tempmingwen[100];int L0[32],R0[32];int R48[48];char miyue1[100];int miyue2[100];int tempmiyue[100];int C0[28],D0[28];int yihuo[48]; //存放R0与K1异或的结果int sheD; //存放经过s盒之后的十进制结果int sheB[8][4]; //存放经过s盒之后的二进制结果int she[32]; //对s盒中的数据进行合并,存放经过s盒的结果int hang,lie; //存放s盒的行列序号int p2[32]; //存放经过p置换后的结果int c,d; //存放C0[0]和D0[0]int i,j; //i,j作为循环使用int icount1,icount2,k;int temp;//__________________________明文输入__________________________________________________________________________________ ________________k=0;printf("请输入明文(以'#'号结尾):\n");for(i=0; i<100; i++){scanf("%c",&mingwen1[i]);if('#' == mingwen1[i]){icount1 = i;goto loop1;}}loop1:for(i=0;i<=icount1;i++){for(j=7;j>=0;j--){temp=mingwen1[i]&(1<<j);if(0 == temp){mingwen2[k]=0;k++;}else{mingwen2[k]=1;k++;}}}//_________________________密钥输入__________________________________________________________________________________ _________________k=0;fflush(stdin); //清除缓冲区内的数据,如果不加上,则miwen[0]会多出一个LF(换行符)!!!printf("请输入密钥(以'#'号结束):\n");for(i=0; i<100; i++){scanf("%c",&miyue1[i]);if('#' == miyue1[i]){icount2 = i;goto loop2;}}loop2:for(i=0;i<=icount2;i++){for(j=7;j>=0;j--){temp=miyue1[i]&(1<<j);if(0 == temp){miyue2[k]=0;k++;}else{miyue2[k]=1;k++;}}}//**********************************************************printf("\n\nm = ");for(i=0;i<8*icount1;i++){printf("%d",mingwen2[i]);if(i== 7||i==15||i==23||i==39||i==47||i==55||i==63){printf(" ");}if(i==31){printf("\n");printf(" ");}}printf("\nk = ");for(i=0;i<8*icount2;i++){printf("%d",miyue2[i]);if(i== 7||i==15||i==23||i==39||i==47||i==55||i==63){printf(" ");}if(i==31){printf("\n");printf(" ");}}printf("\n");//**********************************************************///____________________至此二进制的明文存放在mingwen2[]中_____________________________________________________________________________//________________________进行明文的初始置换IP________________________________________________________________________________ _________________fflush(stdin); //清除缓冲区内的数据for(i=0;i<8*icount1;i++){tempmingwen[i] = mingwen2[ip[i]-1];}//________________________置换IP后的结果存放在tempmingwen[i]中__________________________________________________________________________//________________________把明文结果存放到L0和R0中__________________________________________________________________________________ ____fflush(stdin); //清除缓冲区内的数据for(i=0;i<64;i++){if(i<32) {L0[i]=tempmingwen[i];}else {R0[i-32]=tempmingwen[i];}}//**************************************printf("\nm经过IP置换后得到:\n");printf("L0: ");for(i=0;i<32;i++){printf("%d",L0[i]);if(i== 7||i==15||i==23||i==31){printf(" ");}}printf("\nR0: ");for(i=0;i<32;i++){printf("%d",R0[i]);if(i== 7||i==15||i==23||i==31){printf(" ");}}printf("\n");//**************************************///_______________________对密文进行添加奇偶校验位__________________________________________________________________________________ _____fflush(stdin); //清除缓冲区内的数据for(i=0;i<8;i++){j=56+i;while(j>=jiou[i]){miyue2[j+1]=miyue2[j];j--;}if(i==0 || i==2 || i==4 || i==6) {miyue2[jiou[i]]=0;}else {miyue2[jiou[i]]=1;}}/**********************************************************for(i=0;i<64;i++){printf("%d",miyue2[i]);if(i== 7||i==15||i==23||i==31||i==39||i==47||i==55||i==63){printf(" ");}}**********************************************************///_________________________对密文进行密钥置换__________________________________________________________________________________ _________________fflush(stdin); //清除缓冲区内的数据for(i=0;i<56;i++){tempmiyue[i]=miyue2[ki[i]-1];}/*********************************printf("\n\n");for(i=0;i<56;i++){printf("%d",tempmiyue[i]);if(i== 7||i==15||i==23||i==31||i==39||i==47||i==55||i==63){printf(" ");}}*********************************/fflush(stdin); //清除缓冲区内的数据for(i=0;i<56;i++){if(i<28) {C0[i]=tempmiyue[i];}else {D0[i-28]=tempmiyue[i];}}//**************************************printf("\n密钥k经过置换后得到:");printf("\nC0: ");for(i=0;i<28;i++)printf("%d",C0[i]);if(i== 7||i==15||i==23){printf(" ");}}printf("\nR0: ");for(i=0;i<28;i++){printf("%d",D0[i]);if(i== 7||i==15||i==23){printf(" ");}}printf("\n");//**************************************///_________________________分别对C0和D0进行循环左移操作__________________________________________________________________________________ __c = C0[0];d = D0[0];for(i=0;i<27;i++){C0[i] = C0[i+1];}C0[28] = c;for(i=0;i<27;i++){D0[i] = D0[i+1];}D0[28] = d;//__________________________对C0和D0进行合并存入miyue2[]中______________________________________________________________________________ fflush(stdin); //清除缓冲区内的数据for(i=0;i<56;i++){if(i<28) {miyue2[i]=C0[i];}else {miyue2[i]=D0[i-28];}}//_________________________________________________________________________________ _____-fflush(stdin); //清除缓冲区内的数据for(i=0;i<48;i++){tempmiyue[i] = miyue2[kyasuo[i]-1];}//**************************************printf("\n循环左移一位后经过密钥置换得到48位子密钥:\n");for(i=0;i<48;i++){printf("%d",tempmiyue[i]);if(i== 7||i==15||i==31||i==39||i==47){printf(" ");}if(i==23){printf("\n");}//**************************************/fflush(stdin); //清除缓冲区内的数据for(i=0;i<48;i++){R48[i]=R0[mkuozhan[i]-1];}//***************************************printf("\n\nR0经过扩展变换得到的48位序列为:\n");for(i=0;i<48;i++){printf("%d",R48[i]);if(i== 7||i==15||i==31||i==39||i==47){printf(" ");}if(i==23){printf("\n");}}//**************************************///______________________R0与K1异或______________________________________________________________fflush(stdin); //清除缓冲区内的数据for(i=0;i<48;i++){yihuo[i] = R48[i]^tempmiyue[i];}//______________________结果存放到yihuo[]数组中_________________________________________________//***************************************printf("\n\n结果再和k1进行异或运算,得到的结果为:\n");for(i=0;i<48;i++){printf("%d",yihuo[i]);if(i== 7||i==15||i==31||i==39||i==47){printf(" ");}if(i==23){printf("\n");}}//**************************************///_______________________通过8个s盒的到32位的序列_______________________________________________fflush(stdin); //清除缓冲区内的数据for(i=0;i<8;i++){k=0; //清除k的值hang=yihuo[(1+(i*6))-1]*2 + yihuo[(6+(i*6))-1];lie =yihuo[(2+(i*6))-1]*8 + yihuo[(3+(i*6))-1]*4 + yihuo[(4+(i*6))-1]*2 + yihuo[(5+(i*6))-1];sheD=s[i][hang][lie];for(j=3;j>=0;j--){temp=sheD&(1<<j);if(0 == temp){sheB[i][k]=0;k++;}else{sheB[i][k]=1;k++;}}}//______________________将二维数组sheB[][]中的内容转存到一维数组she中,方便以后的计算___________fflush(stdin); //清除缓冲区内的数据k=0;for(i=0;i<8;i++){for(j=0;j<4;j++){she[k]=sheB[i][j];k++;}}//*************************************printf("\n\n通过8个s盒得到32位的序列为:\n");for(i=0;i<32;i++){printf("%d",she[i]);if(i== 7||i==15||i==23||i==31){printf(" ");}}printf("\n");//*************************************///______________________对s盒的输出序列进行p置换__________________________________________________________fflush(stdin); //清除缓冲区内的数据for(i=0;i<32;i++){p2[i]=she[p[i]-1];}//*************************************fflush(stdin); //清除缓冲区内的数据printf("\n对s盒的输出序列进行p置换,得到\n");for(i=0;i<32;i++){printf("%d",p2[i]);if(i== 7||i==15||i==23||i==31){printf(" ");}}//*************************************///______________________p置换之后和L0进行异或运算_______________________________________________________________________________fflush(stdin); //清除缓冲区内的数据for(i=0;i<32;i++){p2[i]=L0[i]^p2[i];}//______________________L0和R0交换__________________________________________________________________________________ ______________________for(i=0;i<32;i++){L0[i]=R0[i];R0[i]=p2[i];}//*************************************printf("\n\n经过以上操作,得到进过第一轮加密的结果序列为:\n");printf("L0: ");for(i=0;i<32;i++){printf("%d",L0[i]);if(i== 7||i==15||i==23||i==31){printf(" ");}}printf("\n");printf("R0: ");for(i=0;i<32;i++){printf("%d",R0[i]);if(i== 7||i==15||i==23||i==31){printf(" ");}}printf("\n");}。

相关文档
最新文档