/*------------------------------------------------------------------- * File name: mytestdes.cpp * * Author: Xiaomin Bao * Last updated: Feb. 22, 2001 *-------------------------------------------------------------------*/ /* This code implements the encryption of DES, it reads in a key and a plaintext (both in hexdecimal form) from files, then prints out the ciphertext (binary form). */ #include #include #include #include using namespace std; typedef vector binVector; //------------------------------------------------------------------- binVector KeyIP(binVector); binVector C_D_Finder(binVector, int); void Print_List(binVector); binVector K_Finder(binVector,int); binVector IP(binVector); binVector List_Extender(binVector); binVector Lists_Plus(binVector, binVector); binVector Get_Half_L(binVector, int); binVector Func_f(binVector); binVector R_Finder(binVector, binVector, binVector, int); binVector Lists_Merger(binVector, binVector); binVector InvIP(binVector); // external function, the names of the file which contains it is // hexToBinary.cpp binVector hexToBinary(char *); // external function, name of the file: binaryToHex.cpp vector binaryToHex(binVector); //------------------------------------------------------------------- int main(){ char *keyInput = new char[16]; char *textInput = new char[16]; binVector Key, plaintext; vector ciphertext; // ciphertext in hexdecimal form binVector c[17], d[17], k[16], L[17], R[17]; ifstream keyIn, PlainTextIn; keyIn.open("keyHexForm.txt"); if(!keyIn){ cout << "Can not open the file." << endl; return 1; } PlainTextIn.open("plaintextHexForm.txt"); if(!PlainTextIn){ cout << "Can not open the plaintext file." << endl; return 1; } keyIn.read(keyInput, 16); // read in key Key = hexToBinary(keyInput); cout << "\n The key (binary form) is:\n"; Print_List(Key); PlainTextIn.read(textInput,16); // read in plaintext plaintext = hexToBinary(textInput); cout << " The plaintext (binary form) is:\n"; Print_List(plaintext); c[0]=Get_Half_L(KeyIP(Key),1); d[0]=Get_Half_L(KeyIP(Key),29); c[1]=C_D_Finder(c[0],1); d[1]=C_D_Finder(d[0],1); c[2]=C_D_Finder(c[1],1); d[2]=C_D_Finder(d[1],1); for(int i = 3; i <= 8; i++){ c[i] = C_D_Finder(c[i-1],2); d[i] = C_D_Finder(d[i-1],2); } c[9]=C_D_Finder(c[8],1); d[9]=C_D_Finder(d[8],1); for(int i =10; i <= 15; i++){ c[i] = C_D_Finder(c[i-1],2); d[i] = C_D_Finder(d[i-1],2); } c[16]=C_D_Finder(c[15],1); d[16]=C_D_Finder(d[15],1); for(int i = 0; i < 16; i++) k[i] = K_Finder(Lists_Merger(c[i+1],d[i+1]),i+1); L[0]=Get_Half_L(IP(plaintext),1); R[0]=Get_Half_L(IP(plaintext),33); for(int i = 1; i <= 16; i++){ L[i] = R[i-1]; R[i] = R_Finder(L[i-1],R[i-1],k[i-1],i); } cout << " The cryptotext is:\n"; Print_List(InvIP(Lists_Merger(R[16],L[16]))); ciphertext = binaryToHex(InvIP(Lists_Merger(R[16],L[16]))); for(int i=0; i < ciphertext.size(); i++) ciphertext[i]; cout << endl; keyIn.close(); PlainTextIn.close(); return 0; }//main //--------------KeyIP------------------------------------------------ binVector KeyIP(binVector I){ // permute key binVector tmp; for(int i=1; i<=3; i++){ for(int j=0; j<8; j++) tmp.push_back(I[55+i-8*j]); } for(int i=1;i<=4;i++) tmp.push_back(I[59-8*(i-1)]); for(int i=1; i<=3; i++){ for(int j=0; j<8; j++) tmp.push_back(I[63-i-8*j]); } for(int i=1;i<=4;i++) tmp.push_back(I[27-8*(i-1)]); return tmp; }//KeyIP //--------------------C_D_Finder------------------------------------- binVector C_D_Finder(binVector L, int shift){// find ci and di binVector tmp; for(int i=shift; i