Answered! Write a Feistal structure in C++ Program should read the number of rounds and key from command line….

Write a Feistal structure in C++

Program should read the number of rounds and key from command line.
Convert all input to upper case and remove non-text characters and spaces.
If the length of plaintext is not an even number, pad the text with binary zeros.
Use a 32-bit block size
Use the CBC mode for messages larger than the cipher block size.

Basic algorithm is:
The input to each round is split into two blocks of bits.
The round function for this round is applied to the right half.
The result from the round function is XORed with the left half.
The left and right half of the input is swapped and then the right half is replaced with the result of the previous step.

Expert Answer

 

Code:

//Include libraries

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include “stdint.h”

#define lRds 8

#define lAept “-e”

#define lAdpt “-d”

#define lMecb “ecb”

#define lMcbc “cbc”

//Define enum

enum { lActn = 1, MODE };

//Define a function

uint32_t f(uint32_t lbck, uint32_t lky);

//Define a function

uint64_t lept(uint32_t lLft, uint32_t lRgt, uint32_t rounds, uint32_t lKys[]);

//Define a function

uint64_t ldpt(uint32_t lLft, uint32_t lRgt, uint32_t rounds, uint32_t lKys[]);

//Define a function

void leptecb(FILE *lInfile, FILE *lOutfile, uint32_t rounds, uint32_t lKys[]);

//Define a function

void ldptecb(FILE *lInfile, FILE *lOutfile, uint32_t rounds, uint32_t lKys[]);

//Define a function

void leptcbc(FILE *lInfile, FILE *lOutfile, uint32_t rounds, uint32_t lKys[]);

//Define a function

void ldptcbc(FILE *lInfile, FILE *lOutfile, uint32_t rounds, uint32_t lKys[]);

//Define a function

void lUsg(void);

//Define a main method

int main(int argc, char *argv[])

{

uint32_t lKys[lRds] = { 0xDEADBEEF, 0xBAADF00D, 0xFEEDFACE, 0xCAFEBABE, 0xDEADBABE, 0xD15EA5E, 0xDECEA5ED, 0xBAADAC1D };

FILE *lInfile, *lOutfile;

if(argc < 5)

{

fprintf(stderr,”Not enough argumentsn”);

return EXIT_FAILURE;

}

fprintf(stderr,”Trying to open file ‘%s’ as input filen”,argv[3]);

lInfile = fopen(argv[3],”r”);

if(!lInfile)

{

perror(“fopen”);

return EXIT_FAILURE;

}

fprintf(stderr,”Trying to open file ‘%s’ as output filen”,argv[4]);

lOutfile = fopen(argv[4],”w”);

if(!lOutfile)

{

perror(“fopen”);

return EXIT_FAILURE;

}

if(!strcmp(argv[lActn],lAept))

{

if(!strcmp(argv[MODE],lMecb))

{

leptecb(lInfile,lOutfile,lRds,lKys);

}

else if(!strcmp(argv[MODE],lMcbc))

{

leptcbc(lInfile,lOutfile,lRds,lKys);

}

else

{

lUsg();

}

}

else if(!strcmp(argv[lActn],lAdpt))

{

if(!strcmp(argv[MODE],lMecb))

{

fprintf(stderr,”ldptecb()n”);

ldptecb(lInfile,lOutfile,lRds,lKys);

}

else if(!strcmp(argv[MODE],lMcbc))

{

ldptcbc(lInfile,lOutfile,lRds,lKys);

}

else

{

lUsg();

}

}

else

{

lUsg();

}

fclose(lInfile);

fclose(lOutfile);

system(“pause”);

return 0;

}

//Define a method

void lUsg(void)

{

fprintf(stderr,”NOPE.n”);

}

//Define a method

uint32_t f(uint32_t lbck, uint32_t lky)

{

return lbck ^ lky;

}

//Define a method

uint64_t lept(uint32_t lLft, uint32_t lRgt, uint32_t rounds, uint32_t lKys[])

{

uint32_t li, lLft1, lRgt1;

for(li = 0;li < rounds;li++)

{

lLft1 = f(lLft,lKys[li]) ^ lRgt;

lRgt1 = lLft;

if(li == (rounds-1))

{

lLft = lRgt1;

lRgt = lLft1;

}

else

{

lLft = lLft1;

lRgt = lRgt1;

}

}

return (uint64_t)lLft<<32 | lRgt;

}

//Define a method

uint64_t ldpt(uint32_t lLft, uint32_t lRgt, uint32_t rounds, uint32_t lKys[])

{

uint32_t li, lLft1, lRgt1;

for(li = 0;li < rounds;li++)

{

lLft1 = f(lLft,lKys[rounds-li-1]) ^ lRgt;

lRgt1 = lLft;

if(li == (rounds-1))

{

lLft = lRgt1;

lRgt = lLft1;

}

else

{

lLft = lLft1;

lRgt = lRgt1;

}

}

return (uint64_t)lLft<<32 | lRgt;

}

//Define a method

void leptecb(FILE *lInfile, FILE *lOutfile, uint32_t rounds, uint32_t lKys[])

{

uint32_t lLft, lRgt;

size_t lrt;

uint64_t lSbk;

while(!feof(lInfile))

{

memset(&lSbk,0,sizeof(lSbk));

lrt = fread(&lSbk,1,sizeof(lSbk),lInfile);

if(!lrt) break;

lLft = (lSbk>>32) & 0xFFFFFFFF;

lRgt = lSbk & 0xFFFFFFFF;

lSbk = lept(lLft,lRgt,lRds,lKys);

lrt = fwrite(&lSbk,1,sizeof(lSbk),lOutfile);

}

}

//Define a method

void ldptecb(FILE *lInfile, FILE *lOutfile, uint32_t rounds, uint32_t lKys[])

{

uint32_t lLft, lRgt;

size_t lrt;

uint64_t lSbk;

while(!feof(lInfile))

{

memset(&lSbk,0,sizeof(lSbk));

lrt = fread(&lSbk,1,sizeof(lSbk),lInfile);

if(!lrt) break;

lLft = (lSbk>>32) & 0xFFFFFFFF;

lRgt = lSbk & 0xFFFFFFFF;

lSbk = ldpt(lLft,lRgt,lRds,lKys);

lrt = fwrite(&lSbk,1,sizeof(lSbk),lOutfile);

}

}

//Define a method

void leptcbc(FILE *lInfile, FILE *lOutfile, uint32_t rounds, uint32_t lKys[])

{

uint32_t lLft, lRgt;

size_t lrt;

uint64_t lSbk, lSbkprev = 0xFEEDFACE;

while(!feof(lInfile))

{

memset(&lSbk,0,sizeof(lSbk));

lrt = fread(&lSbk,1,sizeof(lSbk),lInfile);

if(!lrt) break;

/* CBC */

lSbk ^= lSbkprev;

lLft = (lSbk>>32) & 0xFFFFFFFF;

lRgt = lSbk & 0xFFFFFFFF;

lSbk = lept(lLft,lRgt,lRds,lKys);

lSbkprev = lSbk;

fwrite(&lSbk,1,sizeof(lSbk),lOutfile);

}

}

//Define a method

void ldptcbc(FILE *lInfile, FILE *lOutfile, uint32_t rounds, uint32_t lKys[])

{

int first = 1;

uint32_t lLft, lRgt;

size_t lrt;

uint64_t lSbk, lSbkprev, saved;

while(!feof(lInfile))

{

memset(&lSbk,0,sizeof(lSbk));

lrt = fread(&lSbk,1,sizeof(lSbk),lInfile);

if(!lrt) break;

saved = lSbk;

lLft = (lSbk>>32) & 0xFFFFFFFF;

lRgt = lSbk & 0xFFFFFFFF;

lSbk = ldpt(lLft,lRgt,lRds,lKys);

if(first)

{

lSbk ^= 0xFEEDFACE;

first = 0;

}

else

{

lSbk ^= lSbkprev;

}

/* CBC */

lSbkprev = saved;

fwrite(&lSbk,1,sizeof(lSbk),lOutfile);

}

}

Still stressed from student homework?
Get quality assistance from academic writers!