/************************************************************ rotor.c Project 2 Dimitry Khodarkovsky March 6 1997 To compile: cc rotor.c -o rotor To execute: rotor Note: Math library doesn't have to be linked. Description: This program prompts the user for input and output filenames and for the encryption/decryption key (b1,b2). An Affine substitution was used by each rotor. y = a2 * (a1 * x + b1) + b2 (mod26) is the equation used x - input text y - output text b1 - orientation of the first rotor b2 - orientation of the second rotor a1,a2 - constants {1,5,25} obtained from a1 * a2 = 25 Derivation: assume: e(K) = W e(10) = 22 d(W) = K d(22) = 10 2 linear equations are obtained: a2 * (a1 * 10 + b1) + b2 = 22 (mod26) eq1 a2 * (a1 * 22 + b1) + b2 = 10 (mod26) eq2 solve 2 linear equations eq1 - eq2 and solve for a1,a2 [a2 * (a1 * 10 + b1) + b2] - [a2 * (a1 * 22 + b1) + b2] = 22 - 10 a2 * ( 10a1 + b1 -22a1 - b1) + b2 - b2 = 12 a2 * (-12a1) = 12 a2 * a1 = -1 mod26 a2 * a1 = 25 additive inverse of -1mod26 = 25 The plaintext of the easy version of the substitution cipher was used as a test. *************************************************************/ #include #include #include #define a1 5 #define a2 5 #define TRUE 1 FILE *ifp,*ofp; main() { int counter; int b1,b2,x,y; char temp[5]; /* holds the key */ char temp1[40]; /* used for unix system calls */ char infile[15]; /* input file name */ char outfile[15]; /* output file name */ while(TRUE){ printf("\nThis program simulates a simple version of the rotor"); printf(" machine.\n"); printf("It only uses 2 rotors.\n"); printf("Please enter the input filename\n:>"); gets(infile); if( (ifp = fopen(infile,"r")) == NULL) { fprintf(stderr, "File %s could not be opened for reading\n",infile); exit(1); } printf("\nPlease enter the output filename\n:>"); gets(outfile); if( (ofp = fopen(outfile,"w")) == NULL) { fprintf(stderr, "File %s could not be opened for writing\n",outfile); exit(1); } printf("\nThis is the input text:\n"); strcpy(temp1,"cat "); strcat(temp1,infile); /* displays the contents */ system(temp1); temp1[0] = '\0'; /* of the input file */ printf("The key is not case sensitive but it has to be in the range:"); printf(" A - Z\nPlease enter the key (A-Z,A-Z) one letter at a time\n"); printf("Please enter the first part of the key:\nK1:>"); gets(temp); b1 = temp[0]; if( (isalpha(b1) == 0) ) { fprintf(stderr,"Invalid key\n"); exit(1); } printf("Please enter the second part of the key:\nK2:>"); gets(temp); b2 = temp[0]; if( (isalpha(b2) == 0) ) { fprintf(stderr,"Invalid key\n"); exit(1); } if(islower(b1)) b1 = toupper(b1); if(islower(b2)) b2 = toupper(b2); b1 -= 'A'; b2 -= 'A'; /* map the key components from ASCII */ counter = 0; /* to range 0 - 25 */ while( (x = fgetc(ifp)) != EOF) { if(isalpha(x)) { counter++; if(islower(x)) x = toupper(x); /* map input characters from ASCII to range 0 - 25 */ x = x - 'A'; /* map output characters from range 0 - 25 to ASCII */ y = (a2 * (a1 * x + b1) + b2) % 26 + 'A'; fputc(y,ofp); b1 = (b1 + 1) % 26; if( (counter % 26) == 0) b2 = (b2 + 1) % 26; }else{ fputc(x,ofp); /* write out special non-alphabet characters */ } } fputc('\n',ofp); fclose(ofp); fclose(ifp); printf("\nThis is the output text:\n"); strcpy(temp1,"cat "); strcat(temp1,outfile); /* display the contents */ system(temp1); /* of the output file */ printf("Would you like to run again?\n"); printf("Enter 1 for yes, anything else will be interpreted as no\n:>"); gets(temp); if(temp[0] != '1') break; } /* end while */ return 0; } /* end of main */