Enigma--Your mission is a failure!
A. First Edition
This is a failed attempt! I posted only to let me remember my shame!
Enigma is the encryption machine that seemed to be impossible to decode before the invention of computer. I
write a simple program to simulate it but it only brings me with shame and regret.
I want to write it simple and fast because I have no time.
There are few points to mention.
E.Further improvement
F.File listing
1. rotor.h
2. rotor.cpp
3. enigma.h
4. enigma.cpp
5. main.cpp (main)
file name: rotor.h
#ifndef ROTOR_H
#define ROTOR_H
const int RotorSize=26;
const int RotorNumber=9;//the last one is stecker
class Rotor
{
private:
int in[RotorSize];
int out[RotorSize];
int shift;
int advance;
public:
bool rotate(bool adv);
int left2right(int number);
int right2left(int number);
void set(int settingNumber, int starter);//ground setting
void reset(){advance=0;}
};
#endif
file name: rotor.cpp
#include "Rotor.h"
//I got these settings from "Russell Schwager" who saves me the job of
//"random mapping"
//the last one is stecker
int setting[RotorNumber][RotorSize]=
{
{
4, 10, 12, 5, 11, 6, 3, 16,
21, 25, 13, 19, 14, 22, 24,
7, 23, 20, 18, 15, 0, 8, 1,
17, 2, 9
},
{
0, 9, 3, 10, 18, 8, 17,
20, 23, 1, 11, 7, 22, 19,
12, 2, 16, 6, 25, 13, 15,
24, 5, 21, 14, 4
},
{
1, 3, 5, 7, 9, 11, 2,
15, 17, 19, 23, 21, 25,
13, 24, 4, 8, 22, 6, 0,
10, 12, 20, 18, 16, 14
},
{
4, 18, 14, 21, 15, 25, 9, 0,
24, 16, 20, 8, 17, 7, 23, 11,
13, 5, 19, 6, 10, 3, 2, 12, 22, 1
},
{
21, 25, 1, 17, 6, 8, 19, 24, 20,
15, 18, 3, 13, 7, 11, 23, 0, 22,
12, 9, 16, 14, 5, 4, 2, 10
},
{
9, 15, 6, 21, 14, 20, 12, 5,
24, 16, 1, 4, 13, 7, 25, 17,
3, 10, 0, 18, 23, 11, 8, 2,
19, 22
},
{
13, 25, 9, 7, 6, 17, 2, 23,
12, 24, 18, 22, 1, 14, 20, 5,
0, 8, 21, 11, 15, 4, 10, 16,
3, 19
},
//this is for reflector and reflector doesn't rotate
{
5, 10, 16, 7, 19, 11, 23,
14, 2, 1, 9, 18, 15, 3,
25, 17, 0, 12, 4, 22, 13, 8,
20, 24, 6, 21
},
//this is stecker, note that it is only "swap" between two characters
{
9, 20, 12, 3, 18,
5, 16, 25, 23, 0,
24, 11, 2, 22, 14,
1, 6, 17, 4, 19,
15, 21, 13, 8, 10, 7
}
};
int Rotor::left2right(int number)
{
int index=(number+advance+shift)%RotorSize;
if (index<0)
{
index+=RotorSize;
}
return out[index];
}
int Rotor::right2left(int number)
{
int index=(number+advance+shift)%RotorSize;
if (index<0)
{
index+=RotorSize;
}
return in[index];
}
bool Rotor::rotate(bool adv)
{
if (adv)
{
advance++;
}
else
{
advance--;
}
return advance%RotorSize==0;
}
void Rotor::set(int settingNumber, int starter)
{
for (int i=0; i<RotorSize; i++)
{
in[i]=setting[settingNumber][i];
out[in[i]]=i;
}
shift=starter;
advance=0;
}
file name: enigma.h
#include "Rotor.h"
const int RotorCount=3;
class Enigma
{
private:
Rotor rotors[RotorCount];
Rotor reflector;
Rotor stecker;
int advances[RotorCount];
int shifts[RotorCount];
void initialize();
void rotate(bool adv);
int operation(int input);
public:
void run(char* text);
Enigma();
};
file name: enigma.cpp
#include <iostream>
#include "Enigma.h"
using namespace std;
const int SteckerIndex=8;
const int ReflectorIndex=7;
int groundSetting[2][RotorCount]=
{
{0,1,2},//rotor index
{0,0,0} //rotor starting position
};
Enigma::Enigma()
{
initialize();
}
void Enigma::initialize()
{
for (int i=0; i<RotorCount; i++)
{
rotors[i].set(groundSetting[0][i], groundSetting[1][i]);
//just record the shifts, and it seems useless now.
shifts[i]=groundSetting[1][i];
advances[i]=0;
}
reflector.set(ReflectorIndex, 0);
stecker.set(SteckerIndex, 0);
}
void Enigma::rotate(bool adv)
{
int i=0;
while (i<RotorCount)
{
//this is where "enigma" bookkeeping the "advances"
if (rotors[i].rotate(adv))
{
//it means this rotor rotates a cycle and
//it is turn for next rotor to rotate
i++;
}
else
{
//there is no more rotating
break;
}
}
}
int Enigma::operation(int input)
{
//here goes the steckers
//input=stecker.right2left(input);
for (int i=0; i<RotorCount; i++)
{
input=rotors[i].right2left(input);
rotate(true);
}
//input=reflector.right2left(input);
//input=reflector.left2right(input);
for (i=RotorCount-1; i>=0; i--)
{
input=rotors[i].left2right(input);
//input=rotors[i].right2left(input);
rotate(true);
}
//input=stecker.left2right(input);
//rotating
/*
while (i<RotorCount)
{
//this is where "enigma" bookkeeping the "advances"
advances[i]++;
rotors[i].rotate();
if (advances[i]%RotorSize==0)//like notch
{
//it means this rotor rotates a cycle and
//it is turn for next rotor to rotate
i++;
}
else
{
//there is no more rotating
break;
}
}
*/
return input;
}
void Enigma::run(char* text)
{
for (int i=0; i<RotorCount; i++)
{
rotors[i].reset();
//advances[i]=0;
}
char *src=text;
int input;
while (*src!='\0')
{
if (isalpha(*src))
{
input=toupper(*src)-'A';
input=operation(input);
*src=input+'A';
}
src++;
}
}
file name: main.cpp
#include <iostream>
#include "Enigma.h"
#include "Rotor.h"
using namespace std;
int main()
{
char buffer[256];
Enigma E;
strcpy(buffer, "abcd");
E.run(buffer);
cout<<buffer<<endl;
cout<<"now decoding\n";
E.run(buffer);
cout<<buffer<<endl;
return 0;
}
The result is like following:
insert into customer(customer_licence, customer_name, customer_address, customer_phone)values
('L1O2183H6', 'JLIRQ IBURX', '5294,KOJJJIQIQRX', '716-2482-852');
insert into service_order(
order_id, customer_licence, order_issue_date) values
(302,'L1O2183H6','05-MAY-04');
insert into car (car_plate_number, customer_licence, modal )values('J9UY3L', 'L1O2183H6', 'Suzuki');
insert into schedule(
order_id, car_plate_number, employee_sin ,
service_date, service_time, service_type ) values (
302, 'J9UY3L',567891234, '23-MAY-04', 7, 0);
insert into schedule(
order_id, car_plate_number, employee_sin ,
service_date, service_time, service_type ) values (
302, 'J9UY3L',234567891, '04-JUN-04', 0, 2);
insert into car (car_plate_number, customer_licence, modal )values('15D957', 'L1O2183H6', 'Ford');
insert into schedule(
order_id, car_plate_number, employee_sin ,
service_date, service_time, service_type ) values (
302, '15D957',567891234, '01-JUN-04', 7, 0);
insert into schedule(
order_id, car_plate_number, employee_sin ,
service_date, service_time, service_type ) values (
302, '15D957',567891234, '17-MAY-04', 3, 2);
insert into payment(payment_id, payment_method, order_id,
payment_amount) values(
100,'credi',
302,0.5*(select sum(service_price) from
service_type t, schedule s where
s.service_type=t.service_type and
s.order_id=302));
insert into payment(payment_id, payment_method, order_id,
payment_amount) values(
101,'debit',
302,0.5*(select sum(service_price) from
service_type t, schedule s where
s.service_type=t.service_type and
s.order_id=302));
insert into customer(customer_licence, customer_name, customer_address, customer_phone)values
('T5122K33K', 'MBQAX NPLQR', '6336,AOOGTDQXBQY', '056-0883-240');
insert into service_order(
order_id, customer_licence, order_issue_date) values
(303,'T5122K33K','16-MAY-04');
insert into car (car_plate_number, customer_licence, modal )values('K1P97W', 'T5122K33K', 'Suzuki');
insert into schedule(
order_id, car_plate_number, employee_sin ,
service_date, service_time, service_type ) values (
303, 'K1P97W',567891234, '08-JUN-04', 3, 0);
insert into schedule(
order_id, car_plate_number, employee_sin ,
service_date, service_time, service_type ) values (
303, 'K1P97W',678912345, '22-MAY-04', 5, 0);
insert into car (car_plate_number, customer_licence, modal )values('9ZO3BY', 'T5122K33K', 'Toyota');
insert into schedule(
order_id, car_plate_number, employee_sin ,
service_date, service_time, service_type ) values (
303, '9ZO3BY',345678912, '02-JUN-04', 4, 0);
insert into schedule(
order_id, car_plate_number, employee_sin ,
service_date, service_time, service_type ) values (
303, '9ZO3BY',234567891, '16-MAY-04', 1, 2);
insert into payment(payment_id, payment_method, order_id,
payment_amount) values(
102,'credi',
303,0.5*(select sum(service_price) from
service_type t, schedule s where
s.service_type=t.service_type and
s.order_id=303));
insert into payment(payment_id, payment_method, order_id,
payment_amount) values(
103,'credi',
303,0.5*(select sum(service_price) from
service_type t, schedule s where
s.service_type=t.service_type and
s.order_id=303));
...
...