2015-08-27 01:48:33 +02:00
|
|
|
package de.vanitasvitae.enigmandroid.enigma;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main component of the Enigma machine
|
|
|
|
* This is the mostly abstract base of any enigma machine.
|
|
|
|
* Copyright (C) 2015 Paul Schaub
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
* @author vanitasvitae
|
|
|
|
*/
|
|
|
|
public abstract class Enigma
|
|
|
|
{
|
|
|
|
protected static String machineType;
|
|
|
|
protected boolean doAnomaly = false; //Has the time come to handle an anomaly?
|
|
|
|
protected boolean prefAnomaly; //Do you WANT to simulate the anomaly?
|
2015-09-10 02:56:48 +02:00
|
|
|
|
|
|
|
public Enigma()
|
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
}
|
2015-08-27 01:48:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the enigma to an initial state
|
|
|
|
*/
|
|
|
|
public abstract void initialize();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypt / Decrypt a given String w.
|
|
|
|
* w must be prepared using prepare(w) beforehand.
|
|
|
|
* Doing so changes the state of the rotors but not the state of the plugboard and the
|
|
|
|
* ringSettings
|
|
|
|
*
|
2015-09-10 02:56:48 +02:00
|
|
|
* @param w Text to decrypt/encryptString
|
2015-08-27 01:48:33 +02:00
|
|
|
* @return encrypted/decrypted string
|
|
|
|
*/
|
2015-09-10 02:56:48 +02:00
|
|
|
public String encryptString(String w)
|
2015-08-27 01:48:33 +02:00
|
|
|
{
|
|
|
|
//output string
|
|
|
|
String output = "";
|
|
|
|
//for each char x in k
|
|
|
|
for (char x : w.toCharArray())
|
|
|
|
{
|
|
|
|
output = output + this.encryptChar(x);
|
|
|
|
}
|
|
|
|
//return en-/decrypted string
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the enigma into the next mechanical state.
|
|
|
|
* This rotates the first rotor and eventually also the second/third.
|
|
|
|
* Also this method handles the anomaly in case it should happen.
|
|
|
|
*/
|
|
|
|
public abstract void nextState();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Substitute char k by sending the signal through the enigma.
|
|
|
|
* The signal passes the plugboard, the rotors and returns back after going through the
|
|
|
|
* reflector wheel.
|
|
|
|
*
|
|
|
|
* @param k input char
|
|
|
|
* @return substituted output char
|
|
|
|
*/
|
|
|
|
public abstract char encryptChar(char k);
|
|
|
|
|
|
|
|
/**
|
2015-09-10 02:56:48 +02:00
|
|
|
* Set the state of the enigma
|
|
|
|
* @param state new state
|
2015-08-27 01:48:33 +02:00
|
|
|
*/
|
2015-09-10 02:56:48 +02:00
|
|
|
public abstract void setState(EnigmaStateBundle state);
|
2015-08-27 01:48:33 +02:00
|
|
|
|
|
|
|
/**
|
2015-09-10 02:56:48 +02:00
|
|
|
* Return an object representing the current state of the enigma
|
|
|
|
* @return state
|
2015-08-27 01:48:33 +02:00
|
|
|
*/
|
2015-09-10 02:56:48 +02:00
|
|
|
public abstract EnigmaStateBundle getState();
|
2015-08-27 01:48:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set prefAnomaly variable
|
|
|
|
* @param b boolean
|
|
|
|
*/
|
|
|
|
public void setPrefAnomaly(boolean b)
|
|
|
|
{
|
|
|
|
this.prefAnomaly = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the type indicator of the enigma machine
|
|
|
|
* @return type
|
|
|
|
*/
|
|
|
|
public String getMachineType()
|
|
|
|
{
|
|
|
|
return machineType;
|
|
|
|
}
|
2015-09-10 02:56:48 +02:00
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|