/************************************************************
 khodrotor.c  

 

 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).


 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



 The plaintext of the easy version of the substitution cipher
 was used as a test.

*************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#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 */ 
    
