Enigma/enigma8.

From Teknologisk videncenter
< Enigma
Revision as of 13:59, 17 June 2012 by Heth (talk | contribs)
Jump to: navigation, search
/**************************************************************************
  #     #
  ##   ##  ######  #####    ####     ##    #    #   #####  ######   ####
  # # # #  #       #    #  #    #   #  #   ##   #     #    #       #    #
  #  #  #  #####   #    #  #       #    #  # #  #     #    #####   #
  #     #  #       #####   #       ######  #  # #     #    #       #
  #     #  #       #   #   #    #  #    #  #   ##     #    #       #    #
  #     #  ######  #    #   ####   #    #  #    #     #    ######   ####

***************************************************************************
Author..: Henrik Thomsen heth@mercantec.dk
Company.: House of Technology at Mercantec ( http://www.mercantec.dk )
date....: 2010 Nov. 28
Version.: 0.00000000001 (Still experimental)
***************************************************************************
Abstract: Enigma8 is a symmetrical encryption/decryption engine developed
          and used for fun. DONT USE IT FOR ANY ACTUAL REAL-LIVE purpose
Purpose.: To be used for fun to challenge our students making multithreaded
          solutions to break the codes.
***************************************************************************
Cavets..: wheels and rotor reflectors made with program makewheel which:
                - Real lousy solution generating random numbers with rand()
                -Real lousy solution solving missing hits in sub rr. missing
                 randomness.

          This program was written in C after years of no C-coding, so
          the generel structure is quite messy. (Sorry ;o)
          It's purpose however is to challenge students so perhaps they
          will improve it.
***************************************************************************
Modification log:
***************************************************************************
License:  Free open software but WITHOUT ANY WARRANTY.
Terms..:  see http://www.gnu.org/licenses
**************************************************************************/
#include <errno.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <stdio.h>
#include "wheels.h"
#define WHEELSUSED 3

char *progname;
int order[WHEELSUSED] = {2,0,1}; /* Wheels installed and in which order */
int position[WHEELSUSED] = {10,216,65}; /* Inital position of rotors */

void usage( void ) {
        printf("Usage:\n%s from-file to-file\n",progname);
        printf("\n%s is a symmetrical encryption/decryption Enigma engine copy\n",progname);
        exit(1);
}
/* sub tick
  abstract: Wheel 1 is ticked forward after each byte.
             Alle wheels have a notch. When they reach their notch the whell
             will tick the next wheel. See wheel_notch in wheels.h
             When wheel 1 reaches its notch it will tick wheel 2 one
             tick forward. When wheel 2 reaches its notch it will tick
             wheel 3 one tick forward etc...
   input     None it uses global variables wheel_notch, order and
             postition.
   returns   none */
void tick( void ) {
        int i,j,flag;
        for (i=0; i < WHEELSUSED; i++ ) {
                if ( i == 0 ) {  /* First wheel always ticks */
                        position[0]++;
                        if ( position[0] >  255 ) { /* Wheel turn over */
                                position[0]=0;
                        }
                }
                if ( wheel_notch[order[i]] == position[i] ) { /* At notch */
                        if ( i < ( WHEELSUSED-1) ) { /* Last wheel dont tick next */
                                flag=1;
                                /* All preceding wheel at notch, then tick next*/
                                for (j=0; j <= i; j++ ) {
                                        if ( wheel_notch[order[j]] != position[j] ) {
                                                flag = 0;
                                        }
                                }
                                if ( flag == 1) {
                                        position[i+1]++;
                                        if ( position[i+1] > 255 ) {
                                                position[i+1] = 0; /* Wheel turn over */
                                        }
                                }
                        }
                }
        }
}

/* sub rotor
   abstract: Send data through a rotor
   input wh  = wheel number
         pos = position (how many ticks is the wheel rotated)
         n   = data (integer) sent through the wheel
   returns (integer) new value of n after been throgh the wheel*/
int rotor(int wh,int pos,int n) {
        /* Beregn den relative position når hjulet er roteret pso ticks */
        int rel;
        if ( n-pos < 0) {
                n+=256;
        }
        rel = wheel[wh][n-pos]+pos;
        if (rel > 255 ) {
                rel = rel % 256;
        }
        return(rel);
}

/* sub revrotor
   abstract: Send data through a reverse rotor (After Route Reflector)
   input wh  = wheel number
         pos = position (how many ticks is the wheel rotated)
         n   = data (integer) sent through the wheel
   returns (integer) new value of n after been throgh the reverse wheel*/
int revrotor(int wh,int pos,int n) {
        /* Beregn den relative position når hjulet er roteret pso ticks */
        int rel;
        if ( n-pos < 0) {
                n+=256;
        }
        rel = rwheel[wh][n-pos]+pos;
        if (rel > 255 ) {
                rel = rel % 256;
        }
        return(rel);
}

/* sub reflector
   abstract: Send data through the route reflector
    input n  = data (integer) sent through the route reflector
    output   = data (integer) after n has been through the route reflector*/
int reflector(int n) {
        return(rr[n]);
}
int main( int argc, char *argv[] ) {
        int c,i,j;
        int w1,r,rw1;
        FILE *fpin;
        FILE *fpout;
        progname = argv[0];

        if ( argc != 3 ) { /* Wheel name appended */
                usage();
        }

        fpin = fopen(argv[1], "r");
        if(fpin == NULL) {
                err("Can't open file for reading",errno);
                fprintf(stderr,"failed to open file %s\n",argv[1]);
                exit(1);
        }
        fpout = fopen(argv[2], "w");
        if(fpout== NULL) {
                err("Can't open file for writing",errno);
                fprintf(stderr,"failed to open file %s\n",argv[1]);
                exit(1);
        }


        while ((c = fgetc(fpin)) != EOF) {
                /* Trough rotors */
                for (i=0; i < 3; i++) {
                        c = rotor(order[i],position[i],c);
                }
                /* Reflector */
                c = reflector(c);
                /* Trough reverse rotors */
                for (i=0; i < 3; i++) {
                        c = revrotor(order[2-i],position[2-i],c);
                }
                fputc(c,fpout);
                tick();
                /*printf("position: %3i %3i %3i\n",position[0],position[1],position[2]);*/
        }
        if ( fclose(fpin) != 0 ) {
                fprintf(stderr,"Cannot close input file\n");
                exit(5);
        }
        if ( fclose(fpout) != 0 ) {
                fprintf(stderr,"Cannot close output file\n");
                exit(5);
        }
        exit(0); /* Succes */
}