Difference between revisions of "Enigma/enigma8."

From Teknologisk videncenter
Jump to: navigation, search
m
m
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
<div class="Source" style="height:400px; overflow:auto">
 
<Source lang=c>
 
<Source lang=c>
 
/**************************************************************************
 
/**************************************************************************
Line 22: Line 23:
 
Cavets..: wheels and rotor reflectors made with program makewheel which:
 
Cavets..: wheels and rotor reflectors made with program makewheel which:
 
                 - Real lousy solution generating random numbers with rand()
 
                 - Real lousy solution generating random numbers with rand()
                 -Real lousy solution solving missing hits in sub rr. missing
+
                 - Real lousy solution solving missing hits in sub rr. missing
                randomness.
+
                  randomness.
  
 
           This program was written in C after years of no C-coding, so
 
           This program was written in C after years of no C-coding, so
Line 31: Line 32:
 
***************************************************************************
 
***************************************************************************
 
Modification log:
 
Modification log:
 +
  09. nov 2024 - Eome bugs removed
 
***************************************************************************
 
***************************************************************************
 
License:  Free open software but WITHOUT ANY WARRANTY.
 
License:  Free open software but WITHOUT ANY WARRANTY.
Line 37: Line 39:
 
#include <errno.h>
 
#include <errno.h>
 
#include <stdlib.h>
 
#include <stdlib.h>
#include <sys/timeb.h>
 
 
#include <stdio.h>
 
#include <stdio.h>
 
#include "wheels.h"
 
#include "wheels.h"
Line 47: Line 48:
  
 
void usage( void ) {
 
void usage( void ) {
        printf("Usage:\n%s from-file to-file\n",progname);
+
    printf("Usage:\n%s from-file to-file\n",progname);
        printf("\n%s is a symmetrical encryption/decryption Enigma engine copy\n",progname);
+
    printf("\n%s is an 8-bit symmetrical encryption/decryption Enigma engine copy\n",progname);
        exit(1);
+
    exit(1);
 
}
 
}
 
/* sub tick
 
/* sub tick
Line 62: Line 63:
 
   returns  none */
 
   returns  none */
 
void tick( void ) {
 
void tick( void ) {
        int i,j,flag;
+
    int i,j,flag;
        for (i=0; i < WHEELSUSED; i++ ) {
+
    for (i=0; i < WHEELSUSED; i++ ) {
                if ( i == 0 ) {  /* First wheel always ticks */
+
        if ( i == 0 ) {  /* First wheel always ticks */
                        position[0]++;
+
            position[0]++;
                        if ( position[0] >  255 ) { /* Wheel turn over */
+
            if ( position[0] >  255 ) { /* Wheel turn over */
                                position[0]=0;
+
                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 ( wheel_notch[order[i]] == position[i] ) { /* At notch */
+
                 if ( flag == 1) {
                        if ( i < ( WHEELSUSED-1) ) { /* Last wheel dont tick next */
+
                    position[i+1]++;
                                flag=1;
+
                    if ( position[i+1] > 255 ) {
                                /* All preceding wheel at notch, then tick next*/
+
                        position[i+1] = 0; /* Wheel turn over */
                                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 */
 
                                        }
 
                                }
 
                        }
 
 
                 }
 
                 }
 +
            }
 
         }
 
         }
 +
    }
 
}
 
}
  
Line 97: Line 98:
 
   returns (integer) new value of n after been throgh the wheel*/
 
   returns (integer) new value of n after been throgh the wheel*/
 
int rotor(int wh,int pos,int n) {
 
int rotor(int wh,int pos,int n) {
        /* Beregn den relative position når hjulet er roteret pso ticks */
+
    /* Beregn den relative position når hjulet er roteret pso ticks */
        int rel;
+
    int rel;
        if ( n-pos < 0) {
+
    if ( n-pos < 0) {
                n+=256;
+
        n+=256;
        }
+
    }
        rel = wheel[wh][n-pos]+pos;
+
    rel = wheel[wh][n-pos]+pos;
        if (rel > 255 ) {
+
    if (rel > 255 ) {
                rel = rel % 256;
+
        rel = rel % 256;
        }
+
    }
        return(rel);
+
    return(rel);
 
}
 
}
  
Line 116: Line 117:
 
   returns (integer) new value of n after been throgh the reverse wheel*/
 
   returns (integer) new value of n after been throgh the reverse wheel*/
 
int revrotor(int wh,int pos,int n) {
 
int revrotor(int wh,int pos,int n) {
        /* Beregn den relative position når hjulet er roteret pso ticks */
+
    /* Beregn den relative position når hjulet er roteret pso ticks */
        int rel;
+
    int rel;
        if ( n-pos < 0) {
+
    if ( n-pos < 0) {
                n+=256;
+
        n+=256;
        }
+
    }
        rel = rwheel[wh][n-pos]+pos;
+
    rel = rwheel[wh][n-pos]+pos;
        if (rel > 255 ) {
+
    if (rel > 255 ) {
                rel = rel % 256;
+
        rel = rel % 256;
        }
+
    }
        return(rel);
+
    return(rel);
 
}
 
}
  
Line 133: Line 134:
 
     output  = data (integer) after n has been through the route reflector*/
 
     output  = data (integer) after n has been through the route reflector*/
 
int reflector(int n) {
 
int reflector(int n) {
        return(rr[n]);
+
    return(rr[n]);
 
}
 
}
 
int main( int argc, char *argv[] ) {
 
int main( int argc, char *argv[] ) {
        int c,i,j;
+
    int c,i,j;
        int w1,r,rw1;
+
    int w1,r,rw1;
        FILE *fpin;
+
    FILE *fpin;
        FILE *fpout;
+
    FILE *fpout;
        progname = argv[0];
+
    progname = argv[0];
  
        if ( argc != 3 ) { /* Wheel name appended */
+
    if ( argc != 3 ) { /* Wheel name appended */
                usage();
+
        usage();
        }
+
    }
  
        fpin = fopen(argv[1], "r");
+
    fpin = fopen(argv[1], "r");
        if(fpin == NULL) {
+
    if(fpin == NULL) {
                err("Can't open file for reading",errno);
+
        //err("Can't open file for reading",errno);
                fprintf(stderr,"failed to open file %s\n",argv[1]);
+
        fprintf(stderr,"failed to open file %s\n",argv[1]);
                exit(1);
+
        exit(1);
        }
+
    }
        fpout = fopen(argv[2], "w");
+
    fpout = fopen(argv[2], "w");
        if(fpout== NULL) {
+
    if(fpout== NULL) {
                err("Can't open file for writing",errno);
+
        //err("Can't open file for writing",errno);
                fprintf(stderr,"failed to open file %s\n",argv[1]);
+
        fprintf(stderr,"failed to open file %s\n",argv[1]);
                exit(1);
+
        exit(1);
        }
+
    }
  
  
        while ((c = fgetc(fpin)) != EOF) {
+
    while ((c = fgetc(fpin)) != EOF) {
                /* Trough rotors */
+
        /* Trough rotors */
                for (i=0; i < 3; i++) {
+
        for (i=0; i < 3; i++) {
                        c = rotor(order[i],position[i],c);
+
            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 ) {
+
         /* Reflector */
                fprintf(stderr,"Cannot close output file\n");
+
        c = reflector(c);
                exit(5);
+
        /* Trough reverse rotors */
 +
        for (i=0; i < 3; i++) {
 +
            c = revrotor(order[2-i],position[2-i],c);
 
         }
 
         }
         exit(0); /* Succes */
+
         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 */
 
}
 
}
 
</source>
 
</source>
[[Category:C]][[Category:Programming]][[Category:CoE]]
+
</div>
 +
[[Category:C]][[Category:CoE]]

Latest revision as of 10:51, 9 November 2024

/**************************************************************************
  #     #
  ##   ##  ######  #####    ####     ##    #    #   #####  ######   ####
  # # # #  #       #    #  #    #   #  #   ##   #     #    #       #    #
  #  #  #  #####   #    #  #       #    #  # #  #     #    #####   #
  #     #  #       #####   #       ######  #  # #     #    #       #
  #     #  #       #   #   #    #  #    #  #   ##     #    #       #    #
  #     #  ######  #    #   ####   #    #  #    #     #    ######   ####

***************************************************************************
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:
  09. nov 2024 - Eome bugs removed
***************************************************************************
License:  Free open software but WITHOUT ANY WARRANTY.
Terms..:  see http://www.gnu.org/licenses
**************************************************************************/
#include <errno.h>
#include <stdlib.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 an 8-bit 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 */
}