Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I attached code for the Feistel Cipher with one round of XORing and swapping. Both encryption and decryption codes are attached. Please add one more

I attached code for the Feistel Cipher with one round of XORing and swapping. Both encryption and decryption codes are attached.

Please add one more round of XORing and swapping by generating another 12 bit subkey from the original 32 bit key. Please make sure that the original 32 bit key is not an array in your program but instead keep it in a notepad file and read into a 32 size array.

//Feistel Cipher with only one round of XORing and swapping

#include #include #include #include #include using namespace std;

int *getBinary(int);

int main(){ int length, num, *chararray1, chararray2[8]; string sentence; char *ptr; int *ptr1, *ptr2, *ptr3, counter=8, counter2=0, counter3=0, counter4=0, counter5=0, randnum; int key[32]={1,0,0,1,0,1,0,1,0,0,0,1,1,0,0,1,0,1,1,1,0,0,0,1,1,0,1,0,1,1,0,1}; //should be kept in a secret notepad file int key1[12]; int leftside[12], rightside[12]; srand(time(0));

ifstream input("secret2.txt"); if(!input){ cout<<"File not found !"; return 1; }

ofstream output("key1.txt"); ofstream output2("cipherbits.txt");

getline(input, sentence); length = sentence.size();

input.clear(); input.seekg(0, ios::beg);

ptr = new char[length]; ptr1= new int[8*length]; ptr2= new int[8*length]; ptr3= new int[8*length];

while(getline(input, sentence)){ while(sentence[counter2]!='\0'){ ptr[counter2]=sentence[counter2]; //populate array ptr with characters counter2++; } }

chararray1= new int[8];

for(int i=0;i

//copy chararray1 to chararray2 for(int j=0;j

//flip the values in chararray1 for(int k=0;k

} // end outer loop

// *************** ENCRYPTION ********************* //GENERATE subkey of size 12

for(int i=0;i<12;i++){ randnum=rand()%32; key1[i]=key[randnum]; }

int code=1; //even / odd //copy 12 bits into array leftside, XOR with key1, place next 12 bits into rightside, then swap the two

for(int i=0;i

for(int j=0;j<12;j++){ leftside[j]=leftside[j]^key1[j]; }

counter4=0; code++; } } else if(code%2==0){ rightside[counter4]=ptr1[i]; counter4++;

if(counter4%12==0){ for(int j=0;j<12;j++){ ptr2[counter5]=rightside[j]; counter5++; }

for(int j=0;j<12;j++){ ptr2[counter5]=leftside[j]; counter5++; } counter4=0; code++; } }

} // loop end -->

counter5=0; //read ptr2 array for(int i=0;i

for(int i=0;i

//write key1 to file

for(int i=0;i<12;i++){ output<

//deallocate heap memory delete [] ptr; delete [] ptr1; delete [] ptr2; delete [] ptr3; delete [] chararray1; ptr=0; ptr1=0; ptr2=0; chararray1=0;

return 0; }

int *getBinary(int num){ int rem, counter=0; int *ptr6;

ptr6 = new int[8]; //allocate an array of size 8

//finding the binary equivalent of num;

while(num>0){ rem=num%2; num=num/2; ptr6[counter]=rem; counter++; }

if(counter==6){ ptr6[6]=0; ptr6[7]=0; } else if(counter==7){ ptr6[7]=0; }

return ptr6; }

Decryption

//Decryption of Feistel Cipher

#include #include #include #include #include using namespace std;

char getChar(int []); int main(){ ifstream input("cipherbits.txt"); if(!input){ cout<<"Bit File not found !"; return 1; }

ifstream input2("key1.txt"); if(!input){ cout<<"Key File not found !"; return 1; } int length, num, bitarray[8]; string sentence; char *ptr; int *ptr1, *ptr2, counter=8, counter2=0, counter3=0, counter4=0, counter5=0, counter6=0, counter7=0;

int key1[12]; int leftside[12], rightside[12];

//Read bits into a sentence to determine the size of bits getline(input, sentence); length = sentence.size();

length=length/2; // because there is a " " after each bit, which makes length twice as long as number of bits

input.clear(); input.seekg(0, ios::beg);

ptr = new char[length/8]; ptr1= new int[length]; ptr2= new int[length];

//populate array ptr1 with bits. Note: Without gaps in between bits this will not be possible as they will be one massive number while(input>>ptr1[counter2]){ counter2++; }

//Read keys for XORing from key file while(input2>>key1[counter3]){ counter3++; }

// *************** DECRYPTION *********************

int code=1; //even / odd to switch between arrays leftside and rightside

//copy 12 bits into array leftside, place next 12 into array rightside, XOR with key1, then swap the two for(int i=0;i

//swap the two arrays when writing to array ptr2 to reverse the swapping done in encryption process

for(int j=0;j<12;j++){ ptr2[counter5]=rightside[j]; counter5++; }

for(int j=0;j<12;j++){ ptr2[counter5]=leftside[j]; counter5++; } counter4=0; code++; // code becomes odd } } } // loop end -->

for(int i=0;i

for(int i=0;i

//deallocate heap memory delete [] ptr; delete [] ptr1; delete [] ptr2;

ptr=0; ptr1=0; ptr2=0;

return 0; }

//function getChar that takes 8 bits at a time and returns the ascii equivalent back

char getChar(int ar[]){ char letter; string let; int total=0; for(int i=0;i<8;i++){ total+=ar[i]*pow(2,7-i); } letter=(char)total; return letter; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Carry out an interview and review its success.

Answered: 1 week ago