SDES加密算法源代码(C++语言)
- 格式:docx
- 大小:17.71 KB
- 文档页数:7
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#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` 中使用。
#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 };void Char_to_Int(unsigned char in[],int out[],int len){for(int i=0;i<len;i++)out[i]=int(in[i]);}void Int_to_Char(int in[],unsigned char out[],int len){for(int i=0;i<len;i++)out[i]=char(in[i]);}void B_to_H(int in[],unsigned char out[]){for(int i=0;i<16;i++){int temp=in[i*4]*8+in[i*4+1]*4+in[i*4+2]*2+in[i*4+3];switch(temp){case 10 :out[i]='A';break;case 11 :out[i]='B';break;case 12 :out[i]='C';break;case 13 :out[i]='D';break;case 14 :out[i]='E';break;case 15 :out[i]='F';break;default :out[i]=unsigned char(temp+48);break;}}}void H_to_B(unsigned char in[],int out[]){for(int i=0;i<16;i++)switch(in[i]){case 'A':out[i*4]=1;out[i*4+1]=0;out[i*4+2]=1;out[i*4+3]=0;break;case 'B':out[i*4]=1;out[i*4+1]=0;out[i*4+2]=1;out[i*4+3]=1;break;case 'C':out[i*4]=1;out[i*4+1]=1;out[i*4+2]=0;out[i*4+3]=0;break;case 'D':out[i*4]=1;out[i*4+1]=1;out[i*4+2]=0;out[i*4+3]=1;break;case 'E':out[i*4]=1;out[i*4+1]=1;out[i*4+2]=1;out[i*4+3]=0;break;case 'F':out[i*4]=1;out[i*4+1]=1;out[i*4+2]=1;out[i*4+3]=1;break;case '0':out[i*4]=0;out[i*4+1]=0;out[i*4+2]=0;out[i*4+3]=0;break;case '1':out[i*4]=0;out[i*4+1]=0;out[i*4+2]=0;out[i*4+3]=1;break;case '2':out[i*4]=0;out[i*4+1]=0;out[i*4+2]=1;out[i*4+3]=0;break;case '3':out[i*4]=0;out[i*4+1]=0;out[i*4+2]=1;out[i*4+3]=1;break;case '4':out[i*4]=0;out[i*4+1]=1;out[i*4+2]=0;out[i*4+3]=0;break;case '5':out[i*4]=0;out[i*4+1]=1;out[i*4+2]=0;out[i*4+3]=1;break;case '6':out[i*4]=0;out[i*4+1]=1;out[i*4+2]=1;out[i*4+3]=0;break;case '7':out[i*4]=0;out[i*4+1]=1;out[i*4+2]=1;out[i*4+3]=1;break;case '8':out[i*4]=1;out[i*4+1]=0;out[i*4+2]=0;out[i*4+3]=0;break;case '9':out[i*4]=1;out[i*4+1]=0;out[i*4+2]=0;out[i*4+3]=1;break;}}void O_to_B(int in[],int out[],int len){int i,j;for(i=0;i<len;i++)for(j=i*len+7;j>=i*len;j--){out[j]=in[i]%2; in[i]=in[i]/2;}}void B_to_O(int in[],int out[],int len){int j;for(int i=0;i<len;i++){j=8*i;out[i]=in[j]*128+in[j+1]*64+in[j+2]*32+in[j+3]*16+in[j+4]*8+in[j+5]*4+in[j+6]*2+in[j+7]*1;}}void Convert(int in[],int out[],int table[],int len){for(int i=0;i<len;i++)out[i]=in[table[i]-1];}void Divide(int in[],int out1[],int out2[],int len){for(int i=0;i<len;i++)if(i<len/2)out1[i]=in[i];elseout2[i-len/2]=in[i];}//正确void Combine(int in1[],int in2[],int out[],int len){for(int i=0;i<len;i++)if(i<len/2)out[i]=in1[i];elseout[i]=in2[i-len/2];}//正确void RLC1(int in[],int out[],int len){int temp=in[0];for(int i=0;i<len-1;i++)out[i]=in[i+1];out[len-1]=temp;}//正确void RLC2(int in[],int out[],int len){int temp1=in[0],temp2=in[1];for(int i=0;i<len-2;i++)out[i]=in[i+2];out[len-2]=temp1;out[len-1]=temp2;}//正确void COPY(int in[],int out[],int len){for(int i=0;i<len;i++)out[i]=in[i];}//ZHENGQUEvoid XOR(int in1[],int in2[],int out[],int len){for(int i=0;i<len;i++)out[i]=(in1[i]+in2[i])%2;}void GetKey(unsigned char in[],int out[16][48]){int i;int Msg_Int[8],Msg_Bin[64],Msg_Bin_PC1[56],Key_Bin[56],C[17][28],D[17][28];char Msg_H[16];int PC1[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[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],FinalINT[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++){Convert(R[i-1],R48,E,48);XOR(R48,K[i-1],S_in,48);S_box(S_in,S_out);Convert(S_out,F_RL,P,32);XOR(F_RL,L[i-1],R[i],32);COPY(R[i-1],L[i],32);}Combine(R[16],L[16],LR,64);Convert(LR,FinalBYTE,IP_1,64);cout<<"二进制的密文:"<<endl;for(i=0;i<64;i++){cout<<FinalBYTE[i];}cout<<endl<<endl;B_to_H(FinalBYTE,Final_H);}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],FinalINT[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(数据加密标准)是一种常用的对称加密算法,它采用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++){//进行加密轮次操作,每轮包括。
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加密和解密。
AES加密C语言实现代码以下是一个简单的C语言实现AES加密算法的代码:```c#include <stdio.h>#include <stdlib.h>#include <stdint.h>//定义AES加密的轮数#define NR 10//定义AES加密的扩展密钥长度#define Nk 4//定义AES加密的行数和列数#define Nb 4//定义AES加密的状态矩阵typedef uint8_t state_t[4][4];//定义AES加密的S盒变换表static const uint8_t sbox[256] =//S盒变换表};//定义AES加密的轮常量表static const uint8_t Rcon[11] =//轮常量表};//定义AES加密的密钥扩展变换函数void KeyExpansion(const uint8_t* key, uint8_t* expandedKey) uint32_t* ek = (uint32_t*)expandedKey;uint32_t temp;//密钥拷贝到扩展密钥中for (int i = 0; i < Nk; i++)ek[i] = (key[4 * i] << 24) , (key[4 * i + 1] << 16) ,(key[4 * i + 2] << 8) , (key[4 * i + 3]);}//扩展密钥生成for (int i = Nk; i < Nb * (NR + 1); i++)temp = ek[i - 1];if (i % Nk == 0)//对上一个密钥的字节进行循环左移1位temp = (temp >> 8) , ((temp & 0xFF) << 24);//对每个字节进行S盒变换temp = (sbox[temp >> 24] << 24) , (sbox[(temp >> 16) & 0xFF] << 16) , (sbox[(temp >> 8) & 0xFF] << 8) , sbox[temp & 0xFF];// 取轮常量Rcontemp = temp ^ (Rcon[i / Nk - 1] << 24);} else if (Nk > 6 && i % Nk == 4)//对每个字节进行S盒变换temp = (sbox[temp >> 24] << 24) , (sbox[(temp >> 16) & 0xFF] << 16) , (sbox[(temp >> 8) & 0xFF] << 8) , sbox[temp & 0xFF];}//生成下一个密钥ek[i] = ek[i - Nk] ^ temp;}//定义AES加密的字节替换函数void SubBytes(state_t* state)for (int i = 0; i < 4; i++)for (int j = 0; j < 4; j++)(*state)[i][j] = sbox[(*state)[i][j]];}}//定义AES加密的行移位函数void ShiftRows(state_t* state) uint8_t temp;//第2行循环左移1位temp = (*state)[1][0];(*state)[1][0] = (*state)[1][1]; (*state)[1][1] = (*state)[1][2]; (*state)[1][2] = (*state)[1][3]; (*state)[1][3] = temp;//第3行循环左移2位temp = (*state)[2][0];(*state)[2][0] = (*state)[2][2]; (*state)[2][2] = temp;temp = (*state)[2][1];(*state)[2][1] = (*state)[2][3]; (*state)[2][3] = temp;//第4行循环左移3位temp = (*state)[3][0];(*state)[3][0] = (*state)[3][3];(*state)[3][3] = (*state)[3][2];(*state)[3][2] = (*state)[3][1];(*state)[3][1] = temp;//定义AES加密的列混淆函数void MixColumns(state_t* state)uint8_t temp, tmp, tm;for (int i = 0; i < 4; i++)tmp = (*state)[i][0];tm = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;temp = (*state)[i][0] ^ (*state)[i][1];(*state)[i][0] ^= temp ^ tm;temp = (*state)[i][1] ^ (*state)[i][2];(*state)[i][1] ^= temp ^ tm;temp = (*state)[i][2] ^ (*state)[i][3];(*state)[i][2] ^= temp ^ tm;temp = (*state)[i][3] ^ tmp;(*state)[i][3] ^= temp ^ tm;}//定义AES加密的轮密钥加函数void AddRoundKey(state_t* state, const uint8_t* roundKey) for (int i = 0; i < 4; i++)for (int j = 0; j < 4; j++)(*state)[j][i] ^= roundKey[i * 4 + j];}}//定义AES加密函数void AES_Encrypt(const uint8_t* plainText, const uint8_t* key, uint8_t* cipherText)state_t* state = (state_t*)cipherText;uint8_t expandedKey[4 * Nb * (NR + 1)];//密钥扩展KeyExpansion(key, expandedKey);//初始化状态矩阵for (int i = 0; i < 4; i++)for (int j = 0; j < 4; j++)(*state)[j][i] = plainText[i * 4 + j];}}//第1轮密钥加AddRoundKey(state, key);//迭代执行第2至第10轮加密for (int round = 1; round < NR; round++) SubBytes(state);ShiftRows(state);MixColumns(state);AddRoundKey(state, expandedKey + round * 16); }//执行第11轮加密SubBytes(state);ShiftRows(state);AddRoundKey(state, expandedKey + NR * 16);int maiuint8_t plainText[16] =//明文数据};uint8_t key[16] =//密钥数据};uint8_t cipherText[16];AES_Encrypt(plainText, key, cipherText);。
des算法;c语言源程序#include<iostream>using namespace std;typedef unsigned char uchar;const 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};const 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, 45, 33, 48,44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32};const 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 };const 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 };const char E_Table[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};const 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, 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,// S612, 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,// S74, 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,// 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 };const 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 };const char LOOP_Table[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};//把BYTE转化为Bit流static void ByteToBit(bool *Out, const unsigned char *In, int bits) {for(int i=0; i<bits; ++i)Out[i] = (In[i>>3]>>(7 - i&7)) & 1;}//把Bit转化为Byte流static void BitToByte(unsigned char *Out, const bool *In, int bits) {memset(Out, 0, bits>>3);for(int i=0; i<bits; ++i)Out[i>>3] |= In[i]<<(7 - i&7);}//把两个Bit流按表进行位转化static void Transform(bool *Out, bool *In, const char *Table, int len) {bool Tmp[256];for(int i=0; i<len; ++i)Tmp[i] = In[ Table[i]-1 ];memcpy(Out, Tmp, len);}//循环左移static void RotateL(bool *In, int len, int loop){bool Tmp[256];memcpy(Tmp, In, loop);memcpy(In, In+loop, len-loop);memcpy(In+len-loop, Tmp, loop);}//两个比特流模2相加static void Xor(bool *InA, const bool *InB, int len){for(int i=0; i<len; ++i)InA[i] ^= InB[i];}//用8个S盒产生32位结果static 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]; //组织SID 下标for(int l=0; l<4; ++l) //把相应4bit赋值Out[l] = (S_Box[i][j][k]>>(3 - l)) & 1;}}//实现数据加密到输出Pstatic void F_func(bool In[32], const bool Ki[48]){bool MR[48];Transform(MR, In, E_Table, 48);Xor(MR, Ki, 48);S_func(In, MR);Transform(In, In, P_Table, 32);}//生成16个48位的子密钥void SetSubKey(bool pSubKey[16][48], const unsigned char Keys[8]) {bool bK[64],bKCD[56], *bKC=&bK[0], *bKD=&bK[28];ByteToBit(bK, Keys, 64);Transform(bKC, bK, PC1_Table, 56);for(int i=0; i<16; ++i){RotateL(bKC, 28, LOOP_Table[i]);RotateL(bKD, 28, LOOP_Table[i]);Transform(pSubKey[i], bKCD, PC2_Table, 48);}}void EncryptOfDES(unsigned char Out[8], const unsigned char In[8], bool pSubKey[16][48]){bool bM[64], bTmp[32], *bL=&bM[0], *bR=&bM[32];ByteToBit(bM, In, 64);Transform(bM, bM, IP_Table, 64);for(int i=0; i<16; i++){memcpy(bTmp,bR, 32); //Ri[i-1] 保存F_func(bR,pSubKey[i]); //Ri[i-1]经过转化和SBox输出为P Xor(bR, bL, 32); //Ri[i] = P XOR Li[i-1]memcpy(bL,bTmp,32); //Li[i] = Ri[i-1]}RotateL(bM,64,32); //Ri与Li换位重组MTransform(bM, bM, IPR_Table, 64); //最后结果进行转化BitToByte(Out, bM, 64);}void JiemiOfDES(unsigned char Out[8], const unsigned char In[8], bool pSubKey[16][48]){bool bM[64], bTmp[32], *bL=&bM[0], *bR=&bM[32];ByteToBit(bM, In, 64);Transform(bM, bM, IP_Table, 64);for(int i=15; i>=0; i--){memcpy(bTmp,bR, 32); //Ri[i-1] 保存F_func(bR, pSubKey[i]); //Ri[i-1]经过转化和SBox输出为PXor(bR, bL, 32); //Ri[i] = P XOR Li[i-1]memcpy(bL,bTmp,32); //Li[i] = Ri[i-1]}RotateL(bM,64,32); //Ri与Li换位重组MTransform(bM, bM, IPR_Table, 64); //最后结果进行转化BitToByte(Out, bM, 64);}int main(){uchar cPlainText[8];bool bPlainText[64];uchar cCipherText[8];uchar ccCipherText[64];bool bCipherText[64];uchar cKeys[8];bool pSubKey[16][48];char flag='a';cout<<"请选择:"<<endl<<"a)加密"<<endl<<"b)解密"<<endl<<"q)退出"<<endl;cin>>flag;while(flag!='q'){if(flag=='a'){cout<<"请输入8字节明文:";cin>>cPlainText;cout<<"请输入8字节密钥:";cin>>cKeys;//cout<<"明文:"<<cPlainText<<endl;//cout<<"密钥:"<<cKeys<<endl;SetSubKey(pSubKey,cKeys);EncryptOfDES(cCipherText,cPlainText,pSubKey); ByteToBit(bCipherText,cCipherText,64);cout<<"密文(Bit) :";for(int i=0;i<64;i++){if(bCipherText[i])cout<<'1';elsecout<<'0';}cout<<endl;/*cout<<"密文(Byte):";for(int i=0;i<8;i++){cout<<cCipherText[i];}cout<<endl;*/}else if(flag=='b'){cout<<"请输入64Bit密文:"<<endl;cin>>ccCipherText;for(int i=0;i<64;i++){if(ccCipherText[i]=='0')bCipherText[i]=0;else if(ccCipherText[i]=='1')bCipherText[i]=1;else{cout<<"输入错误"<<endl;break;}}BitToByte(cCipherText,bCipherText,64);cout<<"请输入8字节密钥:";cin>>cKeys;SetSubKey(pSubKey,cKeys);JiemiOfDES(cPlainText,cCipherText,pSubKey); ByteToBit(bPlainText,cPlainText,64);cout<<"明文:";for(int i=0;i<8;i++){char a=(char)cPlainText[i];cout<<a;}cout<<endl;}elsecout<<"输入错误,请重新输入!"<<endl;cout<<"请选择:"<<endl<<"a)加密"<<endl<<"b)解密"<<endl<<"q)退出"<<endl;cin>>flag;};return 0;}。
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);}。
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_S 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_S);//最终完成左右部的交换}}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_S);}}// 逆初始置换(IP^1置换)DES_IP_1_Transform(cipherBits);Bit64ToChar8(cipherBits,plainBlock);return 0;}//加密文件unsigned char DES_Encrypt(unsigned char *keyStr,unsigned char *plain char *cipherFile){unsigned char keyBlock[8],bKey[64],subKeys[16][48];memcpy(keyBlock,keyStr,8); //设置密钥Char8ToBit64(keyBlock,bKey); //将密钥转换为二进制流DES_MakeSubKeys(bKey,subKeys); //生成子密钥DES_EncryptBlock(plain);return 1;}//解密文件unsigned char DES_Decrypt(unsigned char *keyStr,unsigned char *cipher 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(cipher);return 1;}/****************************************************************************** ****************** 作者:win2kddk* 说明:3重DES是使用16字节密钥将8字节明文数据块进行3次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加密算法的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语⾔代码实现代码: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 }。
#include<iostream>using namespace std;int*out;//用于函数中返回结果的指针,如果写在函数中会被销毁,得不到返回的指针int Key[10];//10位密钥int subKey1[8],subKey2[8];//两个8位的子密钥int plaintext[8];//8位明文int S0[4][4] = { {1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2} };//S0盒子int S1[4][4] = { {0,1,2,3},{ 2,0,1,3 },{3,0,1,0},{ 2,1,0,3} };//S1盒子//生成子密钥的函数void createSubKey() {int buff[10];buff[0] = Key[2];buff[1] = Key[4];buff[2] = Key[1];buff[3] = Key[6];buff[4] = Key[3];buff[5] = Key[9];buff[6] = Key[0];buff[7] = Key[8];buff[8] = Key[7];buff[9] = Key[5];for (int i = 0; i < 10; i++) {Key[i] = buff[i];}/*左边5位LS-1置换*/int temp = buff[0];for (int i = 1; i < 5; i++) {buff[i - 1] = buff[i];}buff[4] = temp;/*右边5位LS-1置换*/temp = buff[5];for (int i = 6; i < 10; i++) {buff[i - 1] = buff[i];}buff[9] = temp;/*将结果写入subKey1*/subKey1[0] = buff[5];subKey1[1] = buff[2];subKey1[2] = buff[6];subKey1[3] = buff[3];subKey1[4] = buff[7];subKey1[5] = buff[4];subKey1[6] = buff[9];subKey1[7] = buff[8];/*左边5位LS-2置换*/temp = buff[0];int temp2 = buff[1];for (int i = 2; i < 5; i++) {buff[i - 2] = buff[i];}buff[3] = temp;buff[4] = temp2;/*右边5位LS-2置换*/temp = buff[5];temp2 = buff[6];for (int i = 7; i < 10; i++) { buff[i - 2] = buff[i];}buff[8] = temp;buff[9] = temp2;/*将结果写入subKey2*/subKey2[0] = buff[5];subKey2[1] = buff[2];subKey2[2] = buff[6];subKey2[3] = buff[3];subKey2[4] = buff[7];subKey2[5] = buff[4];subKey2[6] = buff[9];subKey2[7] = buff[8];}//IP置换int* IP(int* p) {out=new int[8];out[0] = p[1];out[1] = p[5];out[2] = p[2];out[3] = p[0];out[4] = p[3];out[5] = p[7];out[6] = p[4];out[7] = p[6];return out;}//FP置换(IP的逆置换)int* FP(int* p) {out=new int[8];out[0] = p[3];out[1] = p[0];out[2] = p[2];out[3] = p[4];out[4] = p[6];out[5] = p[1];out[6] = p[7];out[7] = p[5];return out;}//异或操作int XOR(int a, int b) {if (a != b) return 1;else return 0;}//F操作int* F(int*R, int*sk) {int buff[8];//E/P操作buff[0] = R[3];buff[1] = R[0];buff[2] = R[1];buff[3] = R[2];buff[4] = R[1];buff[5] = R[2];buff[6] = R[3];buff[7] = R[0];//与子密钥异或for (int i = 0; i < 8; i++)buff[i] = XOR(buff[i],sk[i]);//S盒子操作int row,col;if (buff[0] == 0){if (buff[3] == 0) row = 0;else row = 1;}else {if (buff[3] == 0) row = 2;else row = 3;}if (buff[1] == 0) col = 0;else col = 2;if (buff[2] != 0) col = col + 1;/*S0盒子输出*/int out1 = S0[row][col];if (buff[4] == 0) {if (buff[7] == 0) row = 0;else row = 1;}else {if (buff[7] == 0) row = 2;else row = 3;}if (buff[5] == 0) col = 0;else col = 2;if (buff[6] != 0) col = col + 1;/*S1盒子输出*/int out2 = S1[row][col];out=new int[4];if (out1 == 0 || out1 == 1) out[0] = 0;else out[0] = 1;if (out1 == 1 || out1 == 3) out[1] = 1;else out[1] = 0;if (out2 == 0 || out2 == 1) out[2] = 0;else out[2] = 1;if (out2 == 1 || out2 == 3) out[3] = 1;else out[3] = 0;//置换P4buff[0] = out[1];buff[1] = out[3];buff[2] = out[2];buff[3] = out[0];for (int i = 0; i < 4; i++) out[i] = buff[i];return out;}//fk操作int*Fk(int*L, int*R) {int*r1 = F(R, subKey1);for (int i = 0; i < 4; i++) r1[i] = XOR(L[i],r1[i]);//L异或F(R,SK)int*r2 = F(r1, subKey2);for(int i = 0; i < 4; i++) r2[i] = XOR(R[i], r2[i]);out = new int[8];for (int i = 0; i < 4; i++) out[i] = r2[i];for (int i = 4; i < 8; i++) out[i] = r1[i-4];return out;}//解密的fk操作(调换了子密钥的顺序)int*Fk2(int*L, int*R) {int*r1 = F(R, subKey2);for (int i = 0; i < 4; i++) r1[i] = XOR(L[i], r1[i]);//L异或F(R,SK)int*r2 = F(r1, subKey1);for (int i = 0; i < 4; i++) r2[i] = XOR(R[i], r2[i]);out = new int[8];for (int i = 0; i < 4; i++) out[i] = r2[i];for (int i = 4; i < 8; i++) out[i] = r1[i - 4];return out;}int main() {cout <<"请输入10位密钥,每位为1或0,用空格分隔(例:1 0 1 0 0 0 0 0 1 0):\n";for (int i = 0; i < 10; i++) {cin >> Key[i];}createSubKey();cout <<"子密钥1:\t";for (int i = 0; i < 8; i++) {cout << subKey1[i];}cout <<"\t";cout <<"子密钥2:\t";for (int i = 0; i < 8; i++) {cout << subKey2[i];}cout <<"\n请输入8位明文,每位为1或0,用空格分隔(例:1 0 1 1 1 1 0 1):\n";for (int i = 0; i < 8; i++) {cin >> plaintext[i];}int*s;s=IP(plaintext);int L[4];for (int i = 0; i < 4; i++) L[i] = s[i];int R[4];for (int i = 4; i < 8; i++) R[i - 4] = s[i];s=Fk(L, R);s = FP(s);/*int a[4] = { 1,0,0,1 };int b[8] = { 1,0,0,1,1,0,0,1 };s = F(a,b);*/cout <<"加密后的密文如下:\n";for (int i = 0; i < 8; i++) {cout << s[i];}cout <<"\n";cout <<"解密后的明文如下:\n";s = IP(s);for (int i = 0; i < 4; i++) L[i] = s[i];for (int i = 4; i < 8; i++) R[i - 4] = s[i];s = Fk2(L, R);s = FP(s);for (int i = 0; i < 8; i++) {cout << s[i];}cout <<"\n";}。
#include<stdio.h>#include<stdlib.h>#include<string.h>void show1(> //主界面{printf("\n\n\n\t\t*************** DES加密解密系统 ******************\n\n">。
printf("\t\t--------------------------------------------------\n">。
//printf("\t\t--------------------------------------------------\n">。
printf("\t\t**************************************************\n">。
printf("\t\t**\t\t\t\t\t\t**\n">。
printf("\t\t**\t\t\t\t\t\t**\n">。
printf("\t\t**\t\t\t1.加密\t\t\t**\n">。
printf("\t\t**\t\t\t\t\t\t**\n">。
printf("\t\t**\t\t\t2.解密\t\t\t**\n">。
printf("\t\t**\t\t\t\t\t\t**\n">。
printf("\t\t**\t\t\t3.退出\t\t\t**\n">。
printf("\t\t**\t\t\t\t\t\t**\n">。
printf("\t\t**\t\t\t\t\t\t**\n">。
用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;}欢迎您的下载,资料仅供参考!。
void SDES(char *x,char *m)//DES解密{int y[8]={2,6,3,1,4,8,5,7};//IP置换码int z[8]={4,1,3,5,7,2,8,6};//IP~1置换码//解密使用的临时数组char L0[4],R0[4],L1[4],R1[4],L2[4];char R2[4],elem[8],num[8],c[4],d[4];char k1[8];char k2[8];//子密钥for(int i=0;i<8;i++)//IP~1置换{elem[i]=x[y[i]-1];}for(int j=0;j<4;j++){L2[j]=elem[j];R2[j]=R1[j]=elem[j+4];}key(k1,k2);//获取子密钥K1,K2F(R1,k2,c);//E/P扩展及置换for(int k=0;k<4;k++){if(L2[k]==c[k])L1[k]=R0[k]='0';elseL1[k]=R0[k]='1';}F(R0,k1,d);//F函数运算for(int p=0;p<4;p++)//异或运算,相同为0,不相同为1 {if(R1[p]==d[p])L0[p]='0';elseL0[p]='1';}for(int q=0;q<4;q++)//合并数组{num[q]=L0[q];num[q+4]=R0[q];}for(int qp=0;qp<8;qp++)//IP置换{m[qp]=num[z[qp]-1];}}void jinzhi(int x[],int n)//该函数的作用是将8位的二进制转换为十进制,并显示其对应的字符{int kp=0;char q=' ';for(int j=n-1;j>=0;j--){int tem=1;int sum=0;for(int i=0;i<n-1-j;i++)//幂运算{tem=tem*2;}sum+=tem*x[j];kp+=sum;}q=kp;//将ASCLL码转换成字符cout<<q;}void base(char *x,int n)//S-DES加密的主程序{int *y;int *z;int num[8];y=new int[n];z=new int[n];char temp[8];char elem[8];for(int i=0;i<n;i++)//将字符转换成ASCLL码,并存储在一个整型数组中{y[i]=x[i];}for(int k=0;k<n;k++){int c,b=y[k];for(int j=7;j>=0;j--)//将一个十进制数转换成一个8位的二进制数{c=b%2;b=b/2;z[j]=c;}for(int p=0;p<8;p++)//异或运算,相同为0,不相同为1{if(z[p]==0)temp[p]='0';elsetemp[p]='1';}DES(temp,elem);//加密运算for(int q=0;q<8;q++)//将字符数组转换成整型数组{if(elem[q]=='0')num[q]=0;elsenum[q]=1;}jinzhi(num,8);//进制转换}}void Sbase(char *x,int n)//S-DES解密的主程序{int *y;int *z;int num[8];y=new int[n];z=new int[n];char temp[8];char elem[8];for(int i=0;i<n;i++)//将字符转换成ASCLL码,并存储在一个整型数组中{y[i]=x[i];}for(int k=0;k<n;k++){int c,b=y[k];for(int j=7;j>=0;j--)//将一个十进制数转换成一个8位的二进制数{c=b%2;b=b/2;z[j]=c;}for(int p=0;p<8;p++)//将整型数组转换成字符数组{if(z[p]==0)temp[p]='0';elsetemp[p]='1';}SDES(temp,elem);for(int q=0;q<8;q++)//将字符数组转换成整型数组{if(elem[q]=='0')num[q]=0;elsenum[q]=1;}jinzhi(num,8);}}。