Des encryption and decryption algorithm
//affine transform plus decrypt. cpp: The entry point that defines the console application. //#include <stdio.h>#include <conio.h>Static Charkey[Ten], key_a[5], code[8], key_b[5], temp[Ten], key_aa[8], key_bb[8], l[4], r[4], l_a[4], r_a[4], v[8] = {0,1,0,1,0,1,0,1}, b[8];CharCode, ccode[ -];intp_a[Ten] = {3,5,2,7,4,Ten,1,9,8,6};intp_b[8] = {6,3,7,4,8,5,Ten,9};ints_a[4][4] = {1,0,2,3,3,2,1,0,0,2,1,3,3,1,3,2};ints_b[4][4] = {0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};intip_a[8] = {2,6,3,1,4,8,5,7}, ip_b[8] = {4,1,3,5,7,2,8,6};intf_e[8] = {4,1,2,3,2,3,4,1};voidC_code (Char);voidCoder (Chara[Ten]);voidDecoderChara[Ten]);voidSub_key_product (Chara[Ten]);voidBoxChara[4],intb[4][4]);CharXOR (Char,Char);CharReCode (Chara[8]);voidC_code_b (Char);voidGet_key ();voidCoder (Chara[ -],Charv[8]);voidDecoder (Chara[ -],Charv[8]);/**************************************************************************/ intMain () {//int i; while(GetChar ()! =' E ') {//clrscr (); printf("Please Enter the Text of your want to code:\n");scanf('%s ', Ccode); Coder (Ccode, V);printf('%s ', Ccode);printf("\ n"); Decoder (Ccode, V);printf('%s ', Ccode); }}voidCoder (Chara[ -],Charv[8])/*make the text into coder*/{intI, J; Get_key (); C_code (a[0]); for(i =0; i<8; i++) Code[i] = XOR (Code[i], v[i]); Code = ReCode (code); Coder (key); a[0] = Code; for(j =1; j< -; J + +) {C_code_b (A[j-1]); C_code (A[j]); for(i =0; i<8; i++) Code[i] = XOR (Code[i], b[i]); Code = ReCode (code); Coder (key); A[J] = Code; }}voidGet_key ()/*give The key to the computer*/{intI Getch ();printf("Please enter the key:");scanf('%s ', key); for(i =0; i<Ten; i++) Key[i]-= -;}voidC_code_b (CharM/*make the number into binary * *{Chari =1, n = -;if(m<0) {b[0] =1; m = m + -; }Elseb[0] =0; while(i<8) {B[i] = m/n; m = M-n*b[i]; n = n/2; i++; }}voidC_code (Charm) {Chari =1, n = -;if(m<0) {code[0] =1; m = m + -; }Elsecode[0] =0; while(i<8) {Code[i] = m/n; m = M-n*code[i]; n = n/2; i++; }}CharXOR (CharACharb) {if(A = = b)return 0;Else return 1;}CharReCode (Chara[8]){intI for(i =0; i<7; i++) A[i +1] =2* A[i] + a[i +1];returna[7];}voidBoxChara[4],intb[4][4]){intT t = b[a[0] *2+ a[3]][a[1] *2+ a[2]]; temp[6] = t/2; temp[7] = t%2;}voidSub_key_product (Chara[Ten])/*product the key word*/{intI for(i =0; i<Ten; i++) Temp[i] = A[p_a[i]-1];/ * M1 * / for(i =0; i<5; i++) {Key_a[i] = temp[(i +1) %5]; Key_b[i] = temp[(i +1) %5+5]; } i =0; while(i<5) {Temp[i] = Key_a[i]; i++; } while(i<Ten) {Temp[i] = key_b[i-5]; i++; }/ * m2 * / for(i =0; i<8; i++) Key_aa[i] = Temp[p_b[i]-1];/ * K1 * / for(i =0; i<5; i++) {Key_a[i] = temp[(i +2) %5]; Key_b[i] = temp[(i +2) %5+5]; }/ * m2 shift * /i =0; while(i<5) {Temp[i] = Key_a[i]; i++; } while(i<Ten) {Temp[i] = key_b[i-5]; i++; }/ * m3 * / for(i =0; i<8; i++) Key_bb[i] = Temp[p_b[i]-1];/ * K2 * /}voidIP () {intI for(i =0; i<8; i++) Temp[i] = Code[ip_a[i]-1]; for(i =0; i<8; i++) Code[i] = Temp[i];}voidDe_ip () {intI for(i =0; i<8; i++) Temp[i] = Code[ip_b[i]-1]; for(i =0; i<8; i++) Code[i] = Temp[i];}voidfkCharc_key[8]){intI for(i =0; i<4; i++) {L[i] = Code[i]; R[i] = code[i +4]; } for(i =0; i<8; i++) Temp[i] = XOR (R[f_e[i]-1], c_key[i]);/ * with key XOR */ for(i =0; i<4; i++) {L_a[i] = Temp[i]; R_a[i] = temp[i +4]; } box (l_a, s_a);/ * S0 * /temp[4] = temp[6]; temp[5] = temp[7]; Box (r_a, S_b);/ * S1 * /temp[0] = temp[5]; temp[1] = temp[7]; temp[2] = temp[6]; temp[3] = temp[4];/ * P4 * / for(i =0; i<4; i++) L[i] = XOR (Temp[i], l[i]); i =0; while(i<4) {Code[i] = L[i]; i++; } while(i<8) {Code[i] = r[i-4]; i++; }/ * Merge * /}voidSW () {inti =0; while(i<4) {Code[i] = R[i]; i++; } while(i<8) {Code[i] = l[i-4]; i++; }/ * SW * /}voidCoder (Chara[Ten]) {C_code (code); IP (); Sub_key_product (a); FK (KEY_AA); SW (); FK (KEY_BB); De_ip (); Code = ReCode (code);}voidDecoderChara[Ten]) {C_code (code); IP (); Sub_key_product (a); FK (KEY_BB); SW (); FK (KEY_AA); De_ip (); Code = ReCode (code);}voidDecoder (Chara[ -],Charv[8])/*make the coder into text*/{intI, J; Get_key (); Code = a[0]; Decoder (key); C_code (code); for(i =0; i<8; i++) Code[i] = XOR (Code[i], v[i]); C_code_b (a[0]); a[0] = ReCode (code); for(j =1; j< -; J + +) {Code = A[j]; Decoder (key); C_code (code); for(i =0; i<8; i++) Code[i] = XOR (Code[i], b[i]); C_code_b (A[j]); A[J] = ReCode (code); }}
Des encryption and decryption algorithm