From 992c9a924d7c504d495a1a8f8750b83112e4c594 Mon Sep 17 00:00:00 2001 From: VanitasVitae Date: Sun, 4 Oct 2015 22:51:35 +0200 Subject: [PATCH] Major bug fixes and improvements --- CHANGELOG.txt | 5 +- app/src/main/AndroidManifest.xml | 1 + .../integration/android/IntentIntegrator.java | 4 +- .../enigmandroid/MainActivity.java | 73 +- .../enigmandroid/enigma/Enigma.java | 141 ++- .../enigmandroid/enigma/Enigma_D.java | 103 +- .../enigmandroid/enigma/Enigma_G260.java | 31 +- .../enigmandroid/enigma/Enigma_G31.java | 115 +- .../enigmandroid/enigma/Enigma_G312.java | 32 +- .../enigmandroid/enigma/Enigma_I.java | 119 ++- .../enigmandroid/enigma/Enigma_K.java | 139 ++- .../enigma/Enigma_K_Swiss_Airforce.java | 47 +- .../enigma/Enigma_K_Swiss_Standard.java | 47 +- .../enigmandroid/enigma/Enigma_M3.java | 56 +- .../enigmandroid/enigma/Enigma_M4.java | 167 +-- .../enigmandroid/enigma/Enigma_R.java | 145 +-- .../enigmandroid/enigma/Enigma_T.java | 118 +-- .../enigmandroid/enigma/Plugboard.java | 56 +- .../enigma/inputPreparer/EditTextAdapter.java | 12 +- .../enigma/inputPreparer/InputPreparer.java | 3 +- .../enigma/rotors/EntryWheel.java | 135 +++ .../enigmandroid/enigma/rotors/Reflector.java | 241 +++-- .../enigmandroid/enigma/rotors/Rotor.java | 992 ++++++++---------- .../layout/LayoutContainer_D.java | 1 - .../layout/LayoutContainer_G260.java | 10 +- .../layout/LayoutContainer_G31.java | 21 +- .../layout/LayoutContainer_G312.java | 8 +- .../layout/LayoutContainer_I.java | 17 +- .../layout/LayoutContainer_K.java | 14 +- .../layout/LayoutContainer_K_Swiss.java | 14 +- .../LayoutContainer_K_Swiss_Airforce.java | 14 +- .../layout/LayoutContainer_M3.java | 17 +- .../layout/LayoutContainer_M4.java | 21 +- .../layout/LayoutContainer_R.java | 13 +- .../layout/LayoutContainer_T.java | 12 +- .../layout/PassphraseDialogBuilder.java | 8 +- .../layout/PluggableDialogBuilder.java | 2 +- .../layout/RingSettingsDialogBuilder.java | 6 +- ...in_g_k_t.xml => activity_main_g_k_r_t.xml} | 0 app/src/main/res/layout-land/dialog_plugs.xml | 79 +- app/src/main/res/layout/activity_main_d.xml | 1 - ...in_g_k_t.xml => activity_main_g_k_r_t.xml} | 0 app/src/main/res/layout/dialog_about.xml | 1 - app/src/main/res/layout/dialog_passphrase.xml | 19 +- app/src/main/res/layout/dialog_plugs.xml | 79 +- app/src/main/res/values-de/strings.xml | 6 - .../values-de/strings_activity_settings.xml | 6 - .../res/values/about_dialog_resources.xml | 4 +- app/src/main/res/values/strings.xml | 6 - .../res/values/strings_activity_settings.xml | 7 - app/src/main/res/xml/pref_page.xml | 11 - gradlew | 2 +- 52 files changed, 1708 insertions(+), 1473 deletions(-) create mode 100644 app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/EntryWheel.java rename app/src/main/res/layout-land/{activity_main_g_k_t.xml => activity_main_g_k_r_t.xml} (100%) rename app/src/main/res/layout/{activity_main_g_k_t.xml => activity_main_g_k_r_t.xml} (100%) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 6d95ca7..18eec38 100755 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -3,7 +3,10 @@ v0.1.9-not-yet-released< *Added option to share/receive configurations via QR-Code (ZXing Barcode Scanner) *Prevent user from setting incomplete reflector wiring *Add option to generate configuration from passphrase -*TODO: Encode content of QR-Code completely in Base26(A..Z) +*Reworked Enigma definition (available Rotors/Reflectors/Entrywheels +*Verified correct functionality of Enigma T +*TODO: Write tests to ensure correct functionality +*TODO: Migrate preferences to SettingsActivity *TODO: Add Enigma Z (Probably wont happen due to lack of information :/) *TODO: Add multi-Enigma (select any rotor/reflector etc. Probably wont happen too soon) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 568284b..284c739 100755 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ diff --git a/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java b/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java index d5628e8..231d4d9 100644 --- a/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java +++ b/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java @@ -434,7 +434,7 @@ public class IntentIntegrator { /** - * Defaults to type "TEXT_TYPE". + * Defaults to name "TEXT_TYPE". * * @param text the text string to encode as a barcode * @return the {@link AlertDialog} that was shown to the user prompting them to download the app @@ -450,7 +450,7 @@ public class IntentIntegrator { * scan the text off the screen of the device. * * @param text the text string to encode as a barcode - * @param type type of data to encode. See {@code com.google.zxing.client.android.Contents.Type} constants. + * @param type name of data to encode. See {@code com.google.zxing.client.android.Contents.Type} constants. * @return the {@link AlertDialog} that was shown to the user prompting them to download the app * if a prompt was needed, or null otherwise */ diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java b/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java index 762e9a7..3037e54 100755 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java @@ -5,6 +5,8 @@ import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; @@ -13,11 +15,15 @@ import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.TextView; import android.widget.Toast; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; +import java.math.BigInteger; +import java.security.SecureRandom; + import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer; @@ -50,16 +56,18 @@ public class MainActivity extends Activity "https://github.com/vanitasvitae/EnigmAndroid/blob/master/CHANGELOG.txt"; public static final String APP_ID = "EnigmAndroid"; - LayoutContainer layoutContainer; - protected String prefMachineType; - protected boolean prefAnomaly; - protected String prefNumericLanguage; - protected String prefMessageFormatting; + private LayoutContainer layoutContainer; + private String prefMachineType; + private String prefNumericLanguage; + private String prefMessageFormatting; + + private SecureRandom secureRandom; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + secureRandom = new SecureRandom(); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources(). getStringArray(R.array.pref_alias_machine_type)[0]); @@ -115,7 +123,7 @@ public class MainActivity extends Activity case "G31": case "G312": case "G260": - this.setContentView(R.layout.activity_main_g_k_t); + this.setContentView(R.layout.activity_main_g_k_r_t); break; default: this.setContentView(R.layout.activity_main_i_m3); @@ -128,14 +136,13 @@ public class MainActivity extends Activity SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); this.setPrefMachineType(sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources(). getStringArray(R.array.pref_alias_machine_type)[0])); - this.setPrefAnomaly(sharedPreferences.getBoolean(SettingsActivity.PREF_ANOMALY, true)); this.setPrefNumericLanguage(sharedPreferences.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources(). getStringArray(R.array.pref_alias_numeric_spelling_language)[0])); this.setPrefMessageFormatting(sharedPreferences.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, getResources(). getStringArray(R.array.pref_alias_message_formatting)[0])); } - public void setPrefMachineType(String type) + private void setPrefMachineType(String type) { if(prefMachineType == null || !prefMachineType.equals(type)) { @@ -150,7 +157,7 @@ public class MainActivity extends Activity layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); layoutContainer.getInput().setText(savedInput); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - sharedPreferences.edit().putString(SettingsActivity.PREF_MACHINE_TYPE, type); + sharedPreferences.edit().putString(SettingsActivity.PREF_MACHINE_TYPE, type).apply(); } } @@ -163,20 +170,6 @@ public class MainActivity extends Activity return prefMachineType; } - public void setPrefAnomaly(boolean anomaly) - { - if(prefAnomaly !=anomaly) - { - prefAnomaly = anomaly; - if(layoutContainer != null && layoutContainer.getEnigma() != null) layoutContainer.getEnigma().setPrefAnomaly(anomaly); - } - } - - public boolean getPrefAnomaly() - { - return prefAnomaly; - } - public void setPrefNumericLanguage(String lang) { if(prefNumericLanguage == null || !prefNumericLanguage.equals(lang)) @@ -213,6 +206,11 @@ public class MainActivity extends Activity return prefMessageFormatting; } + public SecureRandom getSecureRandom() + { + return this.secureRandom; + } + public void onDialogFinished(EnigmaStateBundle state) { layoutContainer.getEnigma().setState(state); @@ -288,7 +286,7 @@ public class MainActivity extends Activity IntentIntegrator QRIntegrator = new IntentIntegrator(this); layoutContainer.syncStateFromLayoutToEnigma(); Log.d(APP_ID, "Sharing configuration to QR: " + layoutContainer.getEnigma().stateToString()); - QRIntegrator.shareText(layoutContainer.getEnigma().stateToString()); + QRIntegrator.shareText(APP_ID+"/"+layoutContainer.getEnigma().stateToString()); return true; } else if(id == R.id.action_enter_seed) @@ -300,9 +298,9 @@ public class MainActivity extends Activity } /** - * Set the chosen Configuration to the enigma, get the input string from the input textbox and + * Set the chosen Configuration to the enigma, get the input string from the input text box and * prepare it, set the input to the prepared text, encrypt the prepared input and set the - * encrypted string to the output textbox and update the spinners to their new positions. + * encrypted string to the output text box and update the spinners to their new positions. * @param v View */ public void doCrypto(View v) @@ -314,10 +312,18 @@ public class MainActivity extends Activity * Show a Dialog containing information about the app, license, usage, author and a link * to the changelog */ - public void showAboutDialog() + private void showAboutDialog() { final View aboutView = View.inflate(this, R.layout.dialog_about, null); + //Get and set Version code + PackageInfo pInfo = null; + try{ pInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);} + catch (PackageManager.NameNotFoundException e){ e.printStackTrace();} + String version = pInfo.versionName+ " ("+pInfo.versionCode+")"; + TextView versionText = (TextView) aboutView.findViewById(R.id.about_version_section); + versionText.setText(version); + //Build and show dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.title_about_dialog); builder.setView(aboutView) @@ -354,7 +360,6 @@ public class MainActivity extends Activity SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); this.setPrefMachineType(sharedPrefs.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources() .getStringArray(R.array.pref_alias_machine_type)[0])); - this.setPrefAnomaly(sharedPrefs.getBoolean(SettingsActivity.PREF_ANOMALY, true)); this.setPrefNumericLanguage(sharedPrefs.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources(). getStringArray(R.array.pref_alias_numeric_spelling_language)[0])); this.setPrefMessageFormatting(sharedPrefs.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, @@ -366,8 +371,10 @@ public class MainActivity extends Activity if (scanResult != null) { String content = scanResult.getContents(); if(content == null) Log.e(APP_ID, "Error! Received nothing from QR-Code!"); - else Log.d(APP_ID, "Received "+content+" from QR-Code!"); + else { + Log.d(APP_ID, "Received " + content + " from QR-Code!"); restoreStateFromCode(content); + } } } } @@ -385,10 +392,12 @@ public class MainActivity extends Activity else { mem = mem.substring((APP_ID+"/").length()); - setPrefMachineType(Enigma.chooseEnigmaFromSave(mem)); + BigInteger s = new BigInteger(mem, 16); + Log.d(APP_ID, s.toString()); + setPrefMachineType(Enigma.chooseEnigmaFromSave(s)); updateContentView(); layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType()); - layoutContainer.getEnigma().restoreState(mem); + layoutContainer.getEnigma().restoreState(Enigma.removeDigit(s,20)); layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); layoutContainer.syncStateFromEnigmaToLayout(); } @@ -412,7 +421,7 @@ public class MainActivity extends Activity * Open the web page with the URL url * @param url URL of the website */ - public void openWebPage(String url) { + private void openWebPage(String url) { Uri webPage = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, webPage); if (intent.resolveActivity(getPackageManager()) != null) { diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java index 82019ab..41e420f 100755 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java @@ -1,8 +1,14 @@ package de.vanitasvitae.enigmandroid.enigma; -import java.security.SecureRandom; +import java.math.BigInteger; +import java.util.ArrayList; import java.util.Random; +import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; + /** * Main component of the Enigma machine * This is the mostly abstract base of any enigma machine. @@ -26,24 +32,90 @@ import java.util.Random; public abstract class Enigma { protected static String machineType; - protected int machineTypeOffset = 0; + protected boolean doAnomaly = false; //Has the time come to handle an anomaly? - protected boolean prefAnomaly; //Do you WANT to simulate the anomaly? + + protected ArrayList availableEntryWheels; + protected ArrayList availableRotors; + protected ArrayList availableReflectors; + protected Random rand; - public Enigma(int off) - { - this.machineTypeOffset = off; - initialize(); - } public Enigma() { + establishAvailableParts(); initialize(); } - public int getMachineTypeOffset() + /** + * In this method, available EntryWheels, Rotors and Reflectors can be defined. + */ + protected abstract void establishAvailableParts(); + + /** + * Add a Rotor to the ArrayList of available rotors for this machine. + * Also set the index of the Rotor. + * @param r Rotor + */ + protected void addAvailableRotor(Rotor r) { - return machineTypeOffset; + if(availableRotors == null) availableRotors = new ArrayList<>(); + availableRotors.add(availableRotors.size(), r.setIndex(availableRotors.size())); + } + + protected void addAvailableEntryWheel(EntryWheel e) + { + if(availableEntryWheels == null) availableEntryWheels = new ArrayList<>(); + availableEntryWheels.add(availableEntryWheels.size(), e.setIndex(availableEntryWheels.size())); + } + + protected void addAvailableReflector(Reflector r) + { + if(availableReflectors == null) availableReflectors = new ArrayList<>(); + availableReflectors.add(availableReflectors.size(), r.setIndex(availableReflectors.size())); + } + + public ArrayList getAvailableEntryWheels() + { + return availableEntryWheels; + } + + public ArrayList getAvailableRotors() + { + return availableRotors; + } + + public ArrayList getAvailableReflectors() + { + return availableReflectors; + } + + public EntryWheel getEntryWheel(int index) + { + if(availableEntryWheels == null || availableEntryWheels.size() == 0) return null; + return availableEntryWheels.get(index % availableEntryWheels.size()).getInstance(); + } + + public Rotor getRotor(int index) + { + if(availableRotors == null || availableRotors.size() == 0) return null; + return availableRotors.get(index % availableRotors.size()).getInstance(); + } + + public Rotor getRotor(int index, int rotation, int ringSetting) + { + return getRotor(index).setRotation(rotation).setRingSetting(ringSetting); + } + + public Reflector getReflector(int index) + { + if(availableReflectors == null || availableReflectors.size() == 0) return null; + return availableReflectors.get(index % availableReflectors.size()).getInstance(); + } + + public Reflector getReflector(int index, int rotation, int ringSetting) + { + return getReflector(index).setRotation(rotation).setRingSetting(ringSetting); } /** @@ -85,7 +157,8 @@ public abstract class Enigma */ public void randomState() { - this.rand = new SecureRandom(); + this.rand = ((MainActivity) (MainActivity.ActivitySingleton.getInstance().getActivity())) + .getSecureRandom(); generateState(); } @@ -121,7 +194,7 @@ public abstract class Enigma /** * Set the rand into a certain state based on seed. * Then set the enigmas state. - * @param seed + * @param seed passphrase */ public void setStateFromSeed(String seed) { @@ -129,12 +202,13 @@ public abstract class Enigma generateState(); } - public abstract void restoreState(String mem); + public abstract void restoreState(BigInteger mem); public abstract String stateToString(); public static String numToMachineType(int n) { + n = (12+(n+12)%12)%12; //Problem? Trolololo switch (n) { case 0: return "I"; case 1: return "M3"; @@ -156,27 +230,14 @@ public abstract class Enigma return numToMachineType(seed.hashCode() % 12); } - public static String chooseEnigmaFromSave(String save) + public static String chooseEnigmaFromSave(BigInteger save) { - int index = save.indexOf(":"); - if(index != -1) save = save.substring(0, index); - long s = Long.valueOf(save); - return numToMachineType(getValue(s,12)); - } - - - /** - * set prefAnomaly variable - * @param b boolean - */ - public void setPrefAnomaly(boolean b) - { - this.prefAnomaly = b; + return numToMachineType(getValue(save,20)); } /** - * Return the type indicator of the enigma machine - * @return type + * Return the name indicator of the enigma machine + * @return name */ public String getMachineType() { @@ -189,9 +250,10 @@ public abstract class Enigma * @param d domain (max value) of the value * @return value */ - protected static int getValue(long s, int d) + public static int getValue(BigInteger s, int d) { - return (int) ((s%d)+d)%d; + BigInteger o = s.mod(BigInteger.valueOf(d)).add(BigInteger.valueOf(d)).mod(BigInteger.valueOf(d)); + return Integer.valueOf(o.toString()); } /** @@ -200,9 +262,11 @@ public abstract class Enigma * @param d domain (max value) * @return trimmed source */ - protected static long removeDigit(long s, int d) + public static BigInteger removeDigit(BigInteger s, int d) { - return (s-(s%d))/d; + s = s.subtract(s.mod(BigInteger.valueOf(d))); + s = s.divide(BigInteger.valueOf(d)); + return s; } /** @@ -212,11 +276,10 @@ public abstract class Enigma * @param v actual value * @return lengthened source */ - protected static long addDigit(long s, int v, int b) + public static BigInteger addDigit(BigInteger s, int v, int b) { - long x = s; - x*=b; - x+=(v%b); - return x; + s = s.multiply(BigInteger.valueOf(b)); + s = s.add(BigInteger.valueOf(v % b)); + return s; } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java index f553e41..88453ee 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java @@ -1,11 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import de.vanitasvitae.enigmandroid.MainActivity; +import java.math.BigInteger; + +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Concrete implementation of an enigma machine of type D + * Concrete implementation of an enigma machine of name D * This machine has a rewirable UKW, non changeable rotors. * Copyright (C) 2015 Paul Schaub @@ -25,35 +27,46 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; * @author vanitasvitae */ public class Enigma_D extends Enigma { - protected Rotor entryWheel; + + protected EntryWheel entryWheel; protected Rotor rotor1; protected Rotor rotor2; protected Rotor rotor3; + protected Reflector reflector; - protected Reflector.ReflectorEnigma_D_KD_G31 reflector; - - protected static int machineTypeOffset = 70; public Enigma_D() { super(); machineType = "D"; } + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + + addAvailableRotor(new Rotor.Rotor_K_D_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_K_D_II(0, 0)); + addAvailableRotor(new Rotor.Rotor_K_D_III(0, 0)); + + addAvailableReflector(new Reflector.ReflectorEnigma_D_KD_G31()); + } + @Override public void initialize() { - this.entryWheel = Rotor.createRotor(1, 0, 0); - this.rotor1 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.rotor2 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.rotor3 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.reflector = (Reflector.ReflectorEnigma_D_KD_G31) Reflector.createReflector(machineTypeOffset); + this.entryWheel = availableEntryWheels.get(0); + this.rotor1 = availableRotors.get(0); + this.rotor2 = availableRotors.get(1); + this.rotor3 = availableRotors.get(2); + this.reflector = availableReflectors.get(0); } @Override public void nextState() { rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) { rotor2.rotate(); this.doAnomaly = rotor2.doubleTurnAnomaly(); @@ -75,14 +88,12 @@ public class Enigma_D extends Enigma { int ring3 = rand.nextInt(26); int ringRef = rand.nextInt(26); - this.rotor1 = Rotor.createRotor(machineTypeOffset, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset+1, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset+2, rot3, ring3); + this.rotor1.setRotation(rot1).setRingSetting(ring1); + this.rotor2.setRotation(rot2).setRingSetting(ring2); + this.rotor3.setRotation(rot3).setRingSetting(ring3); - this.reflector = (Reflector.ReflectorEnigma_D_KD_G31) Reflector.createReflector(machineTypeOffset); - this.reflector.setRotation(rotRef); - this.reflector.setRingSetting(ringRef); - this.reflector.setConfiguration(Plugboard.seedToReflectorConfiguration(rand)); + this.reflector.setRotation(rotRef).setRingSetting(ringRef) + .setConfiguration(Plugboard.seedToReflectorConfiguration(rand)); } @Override @@ -116,14 +127,14 @@ public class Enigma_D extends Enigma { @Override public void setState(EnigmaStateBundle state) { - this.entryWheel = Rotor.createRotor(state.getTypeEntryWheel(), 0, 0); - this.rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = new Reflector.ReflectorEnigma_D_KD_G31(); - this.reflector.setConfiguration(state.getConfigurationReflector()); - this.reflector.setRotation(state.getRotationReflector()); - this.reflector.setRingSetting(state.getRingSettingReflector()); + this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); + this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + this.reflector = getReflector(state.getTypeReflector(), + state.getRotationReflector(), + state.getRingSettingReflector()) + .setConfiguration(state.getConfigurationReflector()); } @Override @@ -131,11 +142,11 @@ public class Enigma_D extends Enigma { { EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getNumber()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getNumber()); - state.setTypeRotor2(rotor2.getNumber()); - state.setTypeRotor3(rotor3.getNumber()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); state.setRotationRotor1(rotor1.getRotation()); state.setRotationRotor2(rotor2.getRotation()); @@ -145,7 +156,7 @@ public class Enigma_D extends Enigma { state.setRingSettingRotor2(rotor2.getRingSetting()); state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getNumber()); + state.setTypeReflector(reflector.getIndex()); state.setRotationReflector(reflector.getRotation()); state.setRingSettingReflector(reflector.getRingSetting()); state.setConfigurationReflector(reflector.getConfiguration()); @@ -154,12 +165,8 @@ public class Enigma_D extends Enigma { } @Override - public void restoreState(String mem) + public void restoreState(BigInteger s) { - String reflectorConf = mem.substring(mem.lastIndexOf(":r")+2); - long s = Long.valueOf(mem.substring(0, mem.indexOf(":r"))); - - s = removeDigit(s, 20); //Remove machine type int rot1 = getValue(s, 26); s = removeDigit(s, 26); int ring1 = getValue(s, 26); @@ -175,20 +182,19 @@ public class Enigma_D extends Enigma { int rotRef = getValue(s, 26); s = removeDigit(s, 26); int ringRef = getValue(s, 26); + s = removeDigit(s, 26); - this.rotor1 = Rotor.createRotor(machineTypeOffset, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset+1, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset+2, rot3, ring3); - this.reflector = (Reflector.ReflectorEnigma_D_KD_G31) Reflector.createReflector(machineTypeOffset); - this.reflector.setConfiguration(Plugboard.stringToConfiguration(reflectorConf)); - this.reflector.setRotation(rotRef); - this.reflector.setRingSetting(ringRef); + this.rotor1 = getRotor(0, rot1, ring1); + this.rotor2 = getRotor(1, rot2, ring2); + this.rotor3 = getRotor(2, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); + this.reflector.setConfiguration(s); } @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long s = reflector.getRingSetting(); + BigInteger s = Plugboard.configurationToBigInteger(reflector.getConfiguration()); + s = addDigit(s, reflector.getRingSetting(), 26); s = addDigit(s, reflector.getRotation(), 26); s = addDigit(s, rotor3.getRingSetting(), 26); s = addDigit(s, rotor3.getRotation(), 26); @@ -197,9 +203,6 @@ public class Enigma_D extends Enigma { s = addDigit(s, rotor1.getRingSetting(), 26); s = addDigit(s, rotor1.getRotation(), 26); s = addDigit(s, 6, 20); //Machine #6 - - save = save+s; - save = save + ":r" + Plugboard.configurationToString(getState().getConfigurationReflector()); - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java index 33a3e0f..c210dcb 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java @@ -1,9 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import de.vanitasvitae.enigmandroid.MainActivity; +import java.math.BigInteger; + +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of type G31 (Abwehr) + * Implementation of the Enigma machine of name G31 (Abwehr) * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -25,16 +29,24 @@ public class Enigma_G260 extends Enigma_G31 { public Enigma_G260() { - super(60); + super(); machineType = "G260"; } + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + addAvailableRotor(new Rotor.Rotor_G260_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_G260_II(0, 0)); + addAvailableRotor(new Rotor.Rotor_G260_III(0, 0)); + addAvailableReflector(new Reflector.Reflector_K_G260()); + } @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long s = reflector.getRingSetting(); + BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); s = addDigit(s, reflector.getRotation(), 26); s = addDigit(s, rotor3.getRingSetting(), 26); s = addDigit(s, rotor3.getRotation(), 26); @@ -43,13 +55,12 @@ public class Enigma_G260 extends Enigma_G31 s = addDigit(s, rotor1.getRingSetting(), 26); s = addDigit(s, rotor1.getRotation(), 26); - s = addDigit(s, rotor3.getNumber(), 10); - s = addDigit(s, rotor2.getNumber(), 10); - s = addDigit(s, rotor1.getNumber(), 10); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); s = addDigit(s, 5, 20); //Machine #5 - save = save+s; - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java index bf647a5..31a5326 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java @@ -1,13 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import android.util.Log; +import java.math.BigInteger; -import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of type G31 (Abwehr) + * Implementation of the Enigma machine of name G31 (Abwehr) * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -27,37 +27,41 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_G31 extends Enigma { - protected Rotor entryWheel; + protected EntryWheel entryWheel; protected Rotor rotor1; protected Rotor rotor2; protected Rotor rotor3; - protected Reflector reflector; - public Enigma_G31(int off) - { - super(off); - } - public Enigma_G31() { - super(40); + super(); machineType = "G31"; } + + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + addAvailableRotor(new Rotor.Rotor_G31_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_G31_II(0, 0)); + addAvailableRotor(new Rotor.Rotor_G31_III(0, 0)); + addAvailableReflector(new Reflector.ReflectorEnigma_D_KD_G31()); + } + @Override public void initialize() { - this.entryWheel = Rotor.createRotor(1, 0, 0); - this.rotor1 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.rotor2 = Rotor.createRotor(machineTypeOffset + 1, 0, 0); - this.rotor3 = Rotor.createRotor(machineTypeOffset + 2, 0, 0); - this.reflector = Reflector.createReflector(machineTypeOffset); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(0); + this.rotor2 = getRotor(1); + this.rotor3 = getRotor(2); + this.reflector = getReflector(0); } @Override public void nextState() { - Log.d("Anomaly",""+prefAnomaly+","+doAnomaly); rotor1.rotate(); if (rotor1.isAtTurnoverPosition()) { @@ -75,7 +79,7 @@ public class Enigma_G31 extends Enigma protected void generateState() { - int r1, r2=-1, r3=-1; + int r1, r2=-1, r3; r1 = rand.nextInt(3); while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); r3 = 3 - r1 - r2; @@ -89,12 +93,11 @@ public class Enigma_G31 extends Enigma int ring3 = rand.nextInt(26); int ringRef = rand.nextInt(26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + r1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + r2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + r3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset); - reflector.setRotation(rotRef); - reflector.setRingSetting(ringRef); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); } @Override @@ -127,24 +130,23 @@ public class Enigma_G31 extends Enigma @Override public void setState(EnigmaStateBundle state) { - this.entryWheel = Rotor.createRotor(state.getTypeEntryWheel(), 0, 0); - this.rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = Reflector.createReflector(state.getTypeReflector()); - this.reflector.setRotation(state.getRotationReflector()); - this.reflector.setRingSetting(state.getRingSettingReflector()); + this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); + this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + this.reflector = getReflector(state.getTypeReflector(), + state.getRotationReflector(), state.getRingSettingReflector()); } @Override public EnigmaStateBundle getState() { EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getNumber()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getNumber()); - state.setTypeRotor2(rotor2.getNumber()); - state.setTypeRotor3(rotor3.getNumber()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); state.setRotationRotor1(rotor1.getRotation()); state.setRotationRotor2(rotor2.getRotation()); @@ -154,7 +156,7 @@ public class Enigma_G31 extends Enigma state.setRingSettingRotor2(rotor2.getRingSetting()); state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getNumber()); + state.setTypeReflector(reflector.getIndex()); state.setRotationReflector(reflector.getRotation()); state.setRingSettingReflector(reflector.getRingSetting()); @@ -162,17 +164,14 @@ public class Enigma_G31 extends Enigma } @Override - public void restoreState(String mem) + public void restoreState(BigInteger s) { - long s = Long.valueOf(mem); - s = removeDigit(s, 20); //Remove machine type - - int r1 = getValue(s, 10); - s = removeDigit(s, 10); - int r2 = getValue(s, 10); - s = removeDigit(s, 10); - int r3 = getValue(s, 10); - s = removeDigit(s, 10); + int r1 = getValue(s, availableRotors.size()); + s = removeDigit(s, availableRotors.size()); + int r2 = getValue(s, availableRotors.size()); + s = removeDigit(s, availableRotors.size()); + int r3 = getValue(s, availableRotors.size()); + s = removeDigit(s, availableRotors.size()); int rot1 = getValue(s, 26); s = removeDigit(s, 26); @@ -191,19 +190,17 @@ public class Enigma_G31 extends Enigma int ringRef = getValue(s, 26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + r1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + r2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + r3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset); - this.reflector.setRotation(rotRef); - this.reflector.setRingSetting(ringRef); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); } @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long s = reflector.getRingSetting(); + BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); s = addDigit(s, reflector.getRotation(), 26); s = addDigit(s, rotor3.getRingSetting(), 26); s = addDigit(s, rotor3.getRotation(), 26); @@ -212,13 +209,11 @@ public class Enigma_G31 extends Enigma s = addDigit(s, rotor1.getRingSetting(), 26); s = addDigit(s, rotor1.getRotation(), 26); - s = addDigit(s, rotor3.getNumber(), 10); - s = addDigit(s, rotor2.getNumber(), 10); - s = addDigit(s, rotor1.getNumber(), 10); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); s = addDigit(s, 3, 20); //Machine #3 - - save = save+s; - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java index 5d3e2d3..36354d1 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java @@ -1,9 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import de.vanitasvitae.enigmandroid.MainActivity; +import java.math.BigInteger; + +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of type G31 (Abwehr) + * Implementation of the Enigma machine of name G31 (Abwehr) * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -25,15 +29,24 @@ public class Enigma_G312 extends Enigma_G31 { public Enigma_G312() { - super(50); + super(); machineType = "G312"; } + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + addAvailableRotor(new Rotor.Rotor_G312_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_G312_II(0, 0)); + addAvailableRotor(new Rotor.Rotor_G312_III(0, 0)); + addAvailableReflector(new Reflector.Reflector_G312()); + } + @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long s = reflector.getRingSetting(); + BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); s = addDigit(s, reflector.getRotation(), 26); s = addDigit(s, rotor3.getRingSetting(), 26); s = addDigit(s, rotor3.getRotation(), 26); @@ -42,13 +55,12 @@ public class Enigma_G312 extends Enigma_G31 s = addDigit(s, rotor1.getRingSetting(), 26); s = addDigit(s, rotor1.getRotation(), 26); - s = addDigit(s, rotor3.getNumber(), 10); - s = addDigit(s, rotor2.getNumber(), 10); - s = addDigit(s, rotor1.getNumber(), 10); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); s = addDigit(s, 4, 20); //Machine #4 - save = save+s; - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java index f652e40..ef3d068 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java @@ -1,11 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import de.vanitasvitae.enigmandroid.MainActivity; +import java.math.BigInteger; + +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Concrete implementation of an enigma machine of type I + * Concrete implementation of an enigma machine of name I * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -25,14 +27,13 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_I extends Enigma { + protected EntryWheel entryWheel; protected Rotor rotor1; protected Rotor rotor2; protected Rotor rotor3; - protected Reflector reflector; protected Plugboard plugboard; - protected static int machineTypeOffset = 10; public Enigma_I() { @@ -40,21 +41,35 @@ public class Enigma_I extends Enigma machineType = "I"; } + @Override + protected void establishAvailableParts() { + addAvailableEntryWheel(new EntryWheel.EntryWheel_ABCDEF()); + addAvailableRotor(new Rotor.Rotor_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_II(0,0)); + addAvailableRotor(new Rotor.Rotor_III(0,0)); + addAvailableRotor(new Rotor.Rotor_IV(0,0)); + addAvailableRotor(new Rotor.Rotor_V(0,0)); + addAvailableReflector(new Reflector.Reflector_A()); + addAvailableReflector(new Reflector.Reflector_B()); + addAvailableReflector(new Reflector.Reflector_C()); + } + @Override public void initialize() { this.plugboard= new Plugboard(); - this.rotor1 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.rotor2 = Rotor.createRotor(machineTypeOffset+1, 0, 0); - this.rotor3 = Rotor.createRotor(machineTypeOffset+2, 0, 0); - this.reflector = Reflector.createReflector(machineTypeOffset); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(0, 0, 0); + this.rotor2 = getRotor(1, 0, 0); + this.rotor3 = getRotor(2, 0, 0); + this.reflector = getReflector(0); } @Override public void nextState() { rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) { rotor2.rotate(); this.doAnomaly = rotor2.doubleTurnAnomaly(); @@ -67,10 +82,10 @@ public class Enigma_I extends Enigma @Override protected void generateState() { - int rotor1, rotor2=-1, rotor3=-1; - rotor1 = rand.nextInt(5); - while(rotor2 == -1 || rotor2 == rotor1) rotor2 = rand.nextInt(5); - while(rotor3 == -1 || rotor3 == rotor2 || rotor3 == rotor1) rotor3 = rand.nextInt(5); + int r1, r2=-1, r3=-1; + r1 = rand.nextInt(5); + while(r2 == -1 || r2 == r1) r2 = rand.nextInt(5); + while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(5); int ref = rand.nextInt(3); int rot1 = rand.nextInt(26); @@ -80,10 +95,10 @@ public class Enigma_I extends Enigma int ring2 = rand.nextInt(26); int ring3 = rand.nextInt(26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + rotor1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + rotor2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + rotor3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset + ref); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(ref); this.plugboard = new Plugboard(); plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); @@ -97,6 +112,7 @@ public class Enigma_I extends Enigma //Encryption //forward direction x = plugboard.encrypt(x); + x = entryWheel.encryptForward(x); x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); x = rotor1.encryptForward(x); x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); @@ -113,6 +129,7 @@ public class Enigma_I extends Enigma x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); x = rotor1.encryptBackward(x); x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); + x = entryWheel.encryptBackward(x); x = plugboard.encrypt(x); return (char) (x + 65); //Add Offset again, cast back to char and return } @@ -121,10 +138,11 @@ public class Enigma_I extends Enigma public void setState(EnigmaStateBundle state) { plugboard.setConfiguration(state.getConfigurationPlugboard()); - rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - reflector = Reflector.createReflector(state.getTypeReflector()); + entryWheel = getEntryWheel(state.getTypeEntryWheel()); + rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + reflector = getReflector(state.getTypeReflector()); } @Override @@ -134,11 +152,13 @@ public class Enigma_I extends Enigma state.setConfigurationPlugboard(plugboard.getConfiguration()); - state.setTypeRotor1(rotor1.getNumber()); - state.setTypeRotor2(rotor2.getNumber()); - state.setTypeRotor3(rotor3.getNumber()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeReflector(reflector.getNumber()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); + + state.setTypeReflector(reflector.getIndex()); state.setRotationRotor1(rotor1.getRotation()); state.setRotationRotor2(rotor2.getRotation()); @@ -152,20 +172,16 @@ public class Enigma_I extends Enigma } @Override - public void restoreState(String mem) + public void restoreState(BigInteger s) { - String plugboardConf = mem.substring(mem.lastIndexOf(":p") + 2); - long s = Long.valueOf(mem.substring(0, mem.indexOf(":p"))); - - s = removeDigit(s, 20); //Remove machine type - int r1 = getValue(s, 10); - s = removeDigit(s, 10); - int r2 = getValue(s, 10); - s = removeDigit(s, 10); - int r3 = getValue(s, 10); - s = removeDigit(s, 10); - int ref = getValue(s, 10); - s = removeDigit(s, 10); + int r1 = getValue(s, availableRotors.size()); + s = removeDigit(s, availableRotors.size()); + int r2 = getValue(s, availableRotors.size()); + s = removeDigit(s, availableRotors.size()); + int r3 = getValue(s, availableRotors.size()); + s = removeDigit(s, availableRotors.size()); + int ref = getValue(s, availableReflectors.size()); + s = removeDigit(s, availableReflectors.size()); int rot1 = getValue(s, 26); s = removeDigit(s, 26); int ring1 = getValue(s, 26); @@ -177,32 +193,33 @@ public class Enigma_I extends Enigma int rot3 = getValue(s, 26); s = removeDigit(s, 26); int ring3 = getValue(s, 26); + s = removeDigit(s, 26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + r1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + r2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + r3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset + ref); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(ref); this.plugboard = new Plugboard(); - plugboard.setConfiguration(Plugboard.stringToConfiguration(plugboardConf)); + plugboard.setConfiguration(s); } @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long s = rotor3.getRingSetting(); + BigInteger s = Plugboard.configurationToBigInteger(plugboard.getConfiguration()); + s = addDigit(s, rotor3.getRingSetting(), 26); s = addDigit(s, rotor3.getRotation(), 26); s = addDigit(s, rotor2.getRingSetting(), 26); s = addDigit(s, rotor2.getRotation(), 26); s = addDigit(s, rotor1.getRingSetting(), 26); s = addDigit(s, rotor1.getRotation(), 26); - s = addDigit(s, rotor3.getNumber(), 10); - s = addDigit(s, rotor2.getNumber(), 10); - s = addDigit(s, rotor1.getNumber(), 10); + s = addDigit(s, reflector.getIndex(), availableReflectors.size()); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); s = addDigit(s, 0, 20); //Machine #0 - save = save+s; - save = save + ":p" + Plugboard.configurationToString(getState().getConfigurationPlugboard()); - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java index ea40bc2..6ef91ff 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java @@ -1,17 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import android.app.Activity; -import android.util.Log; +import java.math.BigInteger; -import java.security.SecureRandom; -import java.util.Random; - -import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of type K + * Implementation of the Enigma machine of name K * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -31,38 +27,43 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_K extends Enigma { - protected Rotor entryWheel; + protected EntryWheel entryWheel; protected Rotor rotor1; protected Rotor rotor2; protected Rotor rotor3; - protected Reflector reflector; - public Enigma_K(int off) - { - super(off); - } - public Enigma_K() { - super(80); + super(); machineType = "K"; } + + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + addAvailableRotor(new Rotor.Rotor_K_D_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_K_D_II(0,0)); + addAvailableRotor(new Rotor.Rotor_K_D_III(0,0)); + addAvailableReflector(new Reflector.Reflector_K_G260()); + } + @Override public void initialize() { - this.entryWheel = Rotor.createRotor(1, 0, 0); - this.rotor1 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.rotor2 = Rotor.createRotor(machineTypeOffset+1, 0, 0); - this.rotor3 = Rotor.createRotor(machineTypeOffset+2, 0, 0); - this.reflector = Reflector.createReflector(machineTypeOffset); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(0, 0, 0); + this.rotor2 = getRotor(1, 0, 0); + this.rotor3 = getRotor(2, 0, 0); + this.reflector = getReflector(0); } @Override public void nextState() { rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) { rotor2.rotate(); this.doAnomaly = rotor2.doubleTurnAnomaly(); @@ -75,10 +76,10 @@ public class Enigma_K extends Enigma @Override protected void generateState() { - int rotor1, rotor2=-1, rotor3=-1; - rotor1 = rand.nextInt(3); - while(rotor2 == -1 || rotor2 == rotor1) rotor2 = rand.nextInt(3); - rotor3 = 3 - rotor1 - rotor2; + int r1, r2=-1, r3; + r1 = rand.nextInt(3); + while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); + r3 = 3 - r1 - r2; int rot1 = rand.nextInt(26); int rot2 = rand.nextInt(26); @@ -89,12 +90,11 @@ public class Enigma_K extends Enigma int ring3 = rand.nextInt(26); int ringRef = rand.nextInt(26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + rotor1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + rotor2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + rotor3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset); - reflector.setRotation(rotRef); - reflector.setRingSetting(ringRef); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); } @Override @@ -127,13 +127,11 @@ public class Enigma_K extends Enigma @Override public void setState(EnigmaStateBundle state) { - this.entryWheel = Rotor.createRotor(state.getTypeEntryWheel(), 0, 0); - this.rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = Reflector.createReflector(state.getTypeReflector()); - this.reflector.setRotation(state.getRotationReflector()); - this.reflector.setRingSetting(state.getRingSettingReflector()); + this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); + this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + this.reflector = getReflector(state.getTypeReflector(), state.getRotationReflector(), state.getRingSettingReflector()); } @Override @@ -141,11 +139,11 @@ public class Enigma_K extends Enigma { EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getNumber()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getNumber()); - state.setTypeRotor2(rotor2.getNumber()); - state.setTypeRotor3(rotor3.getNumber()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); state.setRotationRotor1(rotor1.getRotation()); state.setRotationRotor2(rotor2.getRotation()); @@ -155,7 +153,7 @@ public class Enigma_K extends Enigma state.setRingSettingRotor2(rotor2.getRingSetting()); state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getNumber()); + state.setTypeReflector(reflector.getIndex()); state.setRotationReflector(reflector.getRotation()); state.setRingSettingReflector(reflector.getRingSetting()); @@ -163,16 +161,14 @@ public class Enigma_K extends Enigma } @Override - public void restoreState(String mem) + public void restoreState(BigInteger s) { - long s = Long.valueOf(mem); - s = removeDigit(s,20); //Remove machine type - int r1 = getValue(s,10); - s = removeDigit(s,10); - int r2 = getValue(s,10); - s = removeDigit(s,10); - int r3 = getValue(s,10); - s = removeDigit(s,10); + int r1 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); + int r2 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); + int r3 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); int rot1 = getValue(s,26); s = removeDigit(s,26); @@ -190,32 +186,29 @@ public class Enigma_K extends Enigma s = removeDigit(s,26); int ringRef = getValue(s,26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + r1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + r2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + r3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset); - this.reflector.setRotation(rotRef); - this.reflector.setRingSetting(ringRef); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); } @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long t = reflector.getRingSetting(); - t = addDigit(t, reflector.getRotation(), 26); - t = addDigit(t, rotor3.getRingSetting(),26); - t = addDigit(t, rotor3.getRotation(), 26); - t = addDigit(t, rotor2.getRingSetting(),26); - t = addDigit(t, rotor2.getRotation(), 26); - t = addDigit(t, rotor1.getRingSetting(), 26); - t = addDigit(t, rotor1.getRotation(), 26); - t = addDigit(t, rotor3.getNumber(), 10); - t = addDigit(t, rotor2.getNumber(), 10); - t = addDigit(t, rotor1.getNumber(), 10); - t = addDigit(t, 7, 20); //Machine #7 + BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); + s = addDigit(s, reflector.getRotation(), 26); + s = addDigit(s, rotor3.getRingSetting(),26); + s = addDigit(s, rotor3.getRotation(), 26); + s = addDigit(s, rotor2.getRingSetting(),26); + s = addDigit(s, rotor2.getRotation(), 26); + s = addDigit(s, rotor1.getRingSetting(), 26); + s = addDigit(s, rotor1.getRotation(), 26); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); + s = addDigit(s, 7, 20); //Machine #7 - save = save+t; - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java index 55a2501..b9ba64f 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java @@ -1,14 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import java.security.SecureRandom; -import java.util.Random; +import java.math.BigInteger; -import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of type K (Switzerland, Airforce) + * Implementation of the Enigma machine of name K (Switzerland, Airforce) * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -30,28 +29,36 @@ public class Enigma_K_Swiss_Airforce extends Enigma_K { public Enigma_K_Swiss_Airforce() { - super(100); + super(); machineType = "KSA"; } + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + addAvailableRotor(new Rotor.Rotor_K_Swiss_Airforce_I(0,0)); + addAvailableRotor(new Rotor.Rotor_K_Swiss_Airforce_II(0,0)); + addAvailableRotor(new Rotor.Rotor_K_Swiss_Airforce_III(0,0)); + addAvailableReflector(new Reflector.Reflector_K_G260()); + } + @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long t = reflector.getRingSetting(); - t = addDigit(t, reflector.getRotation(), 26); - t = addDigit(t, rotor3.getRingSetting(),26); - t = addDigit(t, rotor3.getRotation(), 26); - t = addDigit(t, rotor2.getRingSetting(),26); - t = addDigit(t, rotor2.getRotation(), 26); - t = addDigit(t, rotor1.getRingSetting(), 26); - t = addDigit(t, rotor1.getRotation(), 26); - t = addDigit(t, rotor3.getNumber(), 10); - t = addDigit(t, rotor2.getNumber(), 10); - t = addDigit(t, rotor1.getNumber(), 10); - t = addDigit(t, 9, 20); //Machine #9 + BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); + s = addDigit(s, reflector.getRotation(), 26); + s = addDigit(s, rotor3.getRingSetting(),26); + s = addDigit(s, rotor3.getRotation(), 26); + s = addDigit(s, rotor2.getRingSetting(),26); + s = addDigit(s, rotor2.getRotation(), 26); + s = addDigit(s, rotor1.getRingSetting(), 26); + s = addDigit(s, rotor1.getRotation(), 26); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); + s = addDigit(s, 9, 20); //Machine #9 - save = save+t; - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java index 73791ad..e6f1023 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java @@ -1,14 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import java.security.SecureRandom; -import java.util.Random; +import java.math.BigInteger; -import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of type K (Switzerland) + * Implementation of the Enigma machine of name K (Switzerland) * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -30,29 +29,37 @@ public class Enigma_K_Swiss_Standard extends Enigma_K { public Enigma_K_Swiss_Standard() { - super(90); + super(); machineType = "KS"; } + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + addAvailableRotor(new Rotor.Rotor_KSwiss_Standard_I(0,0)); + addAvailableRotor(new Rotor.Rotor_KSwiss_Standard_II(0,0)); + addAvailableRotor(new Rotor.Rotor_KSwiss_Standard_III(0,0)); + addAvailableReflector(new Reflector.Reflector_K_G260()); + } + @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long t = reflector.getRingSetting(); - t = addDigit(t, reflector.getRotation(), 26); - t = addDigit(t, rotor3.getRingSetting(),26); - t = addDigit(t, rotor3.getRotation(), 26); - t = addDigit(t, rotor2.getRingSetting(),26); - t = addDigit(t, rotor2.getRotation(), 26); - t = addDigit(t, rotor1.getRingSetting(), 26); - t = addDigit(t, rotor1.getRotation(), 26); - t = addDigit(t, rotor3.getNumber(), 10); - t = addDigit(t, rotor2.getNumber(), 10); - t = addDigit(t, rotor1.getNumber(), 10); - t = addDigit(t, 8, 20); //Machine #8 + BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); + s = addDigit(s, reflector.getRotation(), 26); + s = addDigit(s, rotor3.getRingSetting(),26); + s = addDigit(s, rotor3.getRotation(), 26); + s = addDigit(s, rotor2.getRingSetting(),26); + s = addDigit(s, rotor2.getRotation(), 26); + s = addDigit(s, rotor1.getRingSetting(), 26); + s = addDigit(s, rotor1.getRotation(), 26); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); + s = addDigit(s, 8, 20); //Machine #8 - save = save+t; - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java index c4b04ea..f6bee79 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java @@ -1,10 +1,8 @@ package de.vanitasvitae.enigmandroid.enigma; -import java.security.SecureRandom; -import java.sql.Ref; -import java.util.Random; +import java.math.BigInteger; -import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; @@ -29,20 +27,34 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_M3 extends Enigma_I { - protected static int machineTypeOffset = 20; - public Enigma_M3() { super(); machineType = "M3"; } + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_ABCDEF()); + addAvailableRotor(new Rotor.Rotor_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_II(0,0)); + addAvailableRotor(new Rotor.Rotor_III(0,0)); + addAvailableRotor(new Rotor.Rotor_IV(0,0)); + addAvailableRotor(new Rotor.Rotor_V(0,0)); + addAvailableRotor(new Rotor.Rotor_VI(0,0)); + addAvailableRotor(new Rotor.Rotor_VII(0,0)); + addAvailableRotor(new Rotor.Rotor_VIII(0,0)); + addAvailableReflector(new Reflector.Reflector_B()); + addAvailableReflector(new Reflector.Reflector_C()); + } + @Override protected void generateState() { - int rotor1, rotor2=-1, rotor3=-1; - rotor1 = rand.nextInt(8); - while(rotor2 == -1 || rotor2 == rotor1) rotor2 = rand.nextInt(8); - while(rotor3 == -1 || rotor3 == rotor2 || rotor3 == rotor1) rotor3 = rand.nextInt(8); + int r1, r2=-1, r3=-1; + r1 = rand.nextInt(8); + while(r2 == -1 || r2 == r1) r2 = rand.nextInt(8); + while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(8); int ref = rand.nextInt(2); int rot1 = rand.nextInt(26); @@ -52,10 +64,11 @@ public class Enigma_M3 extends Enigma_I int ring2 = rand.nextInt(26); int ring3 = rand.nextInt(26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + rotor1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + rotor2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + rotor3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset + ref); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(ref); this.plugboard = new Plugboard(); plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); @@ -63,20 +76,19 @@ public class Enigma_M3 extends Enigma_I @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long s = rotor3.getRingSetting(); + BigInteger s = Plugboard.configurationToBigInteger(plugboard.getConfiguration()); + s = addDigit(s, rotor3.getRingSetting(), 26); s = addDigit(s, rotor3.getRotation(), 26); s = addDigit(s, rotor2.getRingSetting(), 26); s = addDigit(s, rotor2.getRotation(), 26); s = addDigit(s, rotor1.getRingSetting(), 26); s = addDigit(s, rotor1.getRotation(), 26); - s = addDigit(s, rotor3.getNumber(), 10); - s = addDigit(s, rotor2.getNumber(), 10); - s = addDigit(s, rotor1.getNumber(), 10); + s = addDigit(s, reflector.getIndex(), availableReflectors.size()); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); s = addDigit(s, 1, 20); //Machine #1 - save = save+s; - save = save + ":p" + Plugboard.configurationToString(getState().getConfigurationPlugboard()); - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java index c5a7c99..a96ea93 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java @@ -1,14 +1,17 @@ package de.vanitasvitae.enigmandroid.enigma; -import java.security.SecureRandom; -import java.util.Random; +import android.util.Log; + +import java.math.BigInteger; +import java.util.ArrayList; import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Concrete Implementation of the Enigma Machine type M4 of the german Kriegsmarine + * Concrete Implementation of the Enigma Machine name M4 of the german Kriegsmarine * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -28,34 +31,73 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_M4 extends Enigma { + private ArrayList availableThinRotors; + private EntryWheel entryWheel; private Rotor rotor1; private Rotor rotor2; private Rotor rotor3; + private Rotor rotor4; private Reflector reflector; private Plugboard plugboard; - protected static int machineTypeOffset = 30; - public Enigma_M4() { super(); machineType = "M4"; } + protected void addAvailableThinRotor(Rotor r) + { + if(availableThinRotors == null) availableThinRotors = new ArrayList<>(); + availableThinRotors.add(availableThinRotors.size(), r.setIndex(availableThinRotors.size())); + } + + public Rotor getThinRotor(int index) + { + if(availableThinRotors == null || availableThinRotors.size() == 0) return null; + return availableThinRotors.get(index % availableThinRotors.size()).getInstance(); + } + + public Rotor getThinRotor(int index, int rotation, int ringSettings) + { + Rotor r = getThinRotor(index); + if(r == null) return null; + return r.setRotation(rotation).setRingSetting(ringSettings); + } + + @Override + protected void establishAvailableParts() + { + Log.d(MainActivity.APP_ID, "Established"); + addAvailableEntryWheel(new EntryWheel.EntryWheel_ABCDEF()); + addAvailableRotor(new Rotor.Rotor_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_II(0,0)); + addAvailableRotor(new Rotor.Rotor_III(0,0)); + addAvailableRotor(new Rotor.Rotor_IV(0,0)); + addAvailableRotor(new Rotor.Rotor_V(0,0)); + addAvailableRotor(new Rotor.Rotor_VI(0,0)); + addAvailableRotor(new Rotor.Rotor_VII(0, 0)); + addAvailableRotor(new Rotor.Rotor_VIII(0,0)); + addAvailableThinRotor(new Rotor.Rotor_M4_Beta(0, 0)); + addAvailableThinRotor(new Rotor.Rotor_M4_Gamma(0, 0)); + addAvailableReflector(new Reflector.Reflector_Thin_B()); + addAvailableReflector(new Reflector.ReflectorThinC()); + } + @Override public void initialize() { + Log.d(MainActivity.APP_ID, "Initialized"); this.plugboard = new Plugboard(); - this.rotor1 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.rotor2 = Rotor.createRotor(machineTypeOffset+1, 0, 0); - this.rotor3 = Rotor.createRotor(machineTypeOffset+2, 0, 0); - this.rotor4 = Rotor.createRotor(machineTypeOffset + 8, 0, 0); - this.reflector = Reflector.createReflector(machineTypeOffset); - this.prefAnomaly = ((MainActivity) MainActivity.ActivitySingleton.getInstance() - .getActivity()).getPrefAnomaly(); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(0, 0, 0); + this.rotor2 = getRotor(1, 0, 0); + this.rotor3 = getRotor(2, 0, 0); + this.rotor4 = getThinRotor(0, 0, 0); + this.reflector = getReflector(0); } @Override @@ -69,7 +111,7 @@ public class Enigma_M4 extends Enigma //Rotate rotors rotor1.rotate(); //Eventually turn next rotor (usual turnOver or anomaly) - if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) { rotor2.rotate(); //Set doAnomaly for next call of encryptChar @@ -104,14 +146,13 @@ public class Enigma_M4 extends Enigma int ring4 = rand.nextInt(26); int ringRef = rand.nextInt(26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + r1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + r2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + r3, rot3, ring3); - this.rotor4 = Rotor.createRotor(machineTypeOffset + 8 + r4, rot4, ring4); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.rotor4 = getThinRotor(r4, rot4, ring4); - this.reflector = Reflector.createReflector(machineTypeOffset + ref); - this.reflector.setRotation(rotRef); - this.reflector.setRingSetting(ringRef); + this.reflector = getReflector(ref, rotRef, ringRef); this.plugboard = new Plugboard(); this.plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); @@ -133,6 +174,7 @@ public class Enigma_M4 extends Enigma //Encryption //forward direction x = plugboard.encrypt(x); + x = entryWheel.encryptForward(x); x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); x = rotor1.encryptForward(x); x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); @@ -153,6 +195,7 @@ public class Enigma_M4 extends Enigma x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); x = rotor1.encryptBackward(x); x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); + x = entryWheel.encryptBackward(x); x = plugboard.encrypt(x); return (char) (x + 65); //Add Offset again and cast back to char } @@ -160,11 +203,11 @@ public class Enigma_M4 extends Enigma @Override public void setState(EnigmaStateBundle state) { - rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - rotor4 = Rotor.createRotor(state.getTypeRotor4(), state.getRotationRotor4(), state.getRingSettingRotor4()); - reflector = Reflector.createReflector(state.getTypeReflector()); + rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + rotor4 = getThinRotor(state.getTypeRotor4(), state.getRotationRotor4(), state.getRingSettingRotor4()); + reflector = getReflector(state.getTypeReflector()); plugboard.setConfiguration(state.getConfigurationPlugboard()); } @@ -173,10 +216,12 @@ public class Enigma_M4 extends Enigma public EnigmaStateBundle getState() { EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeRotor1(rotor1.getNumber()); - state.setTypeRotor2(rotor2.getNumber()); - state.setTypeRotor3(rotor3.getNumber()); - state.setTypeRotor4(rotor4.getNumber()); + state.setTypeEntryWheel(entryWheel.getIndex()); + + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); + state.setTypeRotor4(rotor4.getIndex()); state.setRotationRotor1(rotor1.getRotation()); state.setRotationRotor2(rotor2.getRotation()); @@ -188,7 +233,7 @@ public class Enigma_M4 extends Enigma state.setRingSettingRotor3(rotor3.getRingSetting()); state.setRingSettingRotor4(rotor4.getRingSetting()); - state.setTypeReflector(reflector.getNumber()); + state.setTypeReflector(reflector.getIndex()); state.setConfigurationPlugboard(plugboard.getConfiguration()); @@ -196,23 +241,18 @@ public class Enigma_M4 extends Enigma } @Override - public void restoreState(String mem) + public void restoreState(BigInteger s) { - String plugboardConf = mem.substring(mem.lastIndexOf(":p") + 2); - long s = Long.valueOf(mem.substring(0, mem.indexOf(":p"))); - - s = removeDigit(s, 20); //Remove machine type - - int r1 = getValue(s, 10); - s = removeDigit(s, 10); - int r2 = getValue(s, 10); - s = removeDigit(s,10); - int r3 = getValue(s, 10); - s = removeDigit(s,10); - int r4 = getValue(s, 10); - s = removeDigit(s,10); - int ref = getValue(s, 10); - s = removeDigit(s,10); + int r1 = getValue(s, availableRotors.size()); + s = removeDigit(s, availableRotors.size()); + int r2 = getValue(s, availableRotors.size()); + s = removeDigit(s,availableRotors.size()); + int r3 = getValue(s, availableRotors.size()); + s = removeDigit(s,availableRotors.size()); + int r4 = getValue(s, availableThinRotors.size()); + s = removeDigit(s,availableThinRotors.size()); + int ref = getValue(s, availableReflectors.size()); + s = removeDigit(s,availableReflectors.size()); int rot1 = getValue(s, 26); s = removeDigit(s,26); @@ -233,23 +273,21 @@ public class Enigma_M4 extends Enigma int rotRef = getValue(s, 26); s = removeDigit(s,26); int ringRef = getValue(s, 26); + s = removeDigit(s,26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + r1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + r2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + r3, rot3, ring3); - this.rotor4 = Rotor.createRotor(machineTypeOffset + r4, rot4, ring4); - this.reflector = Reflector.createReflector(machineTypeOffset + ref); - this.reflector.setRotation(rotRef); - this.reflector.setRingSetting(ringRef); - + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.rotor4 = getThinRotor(r4, rot4, ring4); + this.reflector = getReflector(ref, rotRef, ringRef); this.plugboard = new Plugboard(); - plugboard.setConfiguration(Plugboard.stringToConfiguration(plugboardConf)); + plugboard.setConfiguration(s); } @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long s = reflector.getRingSetting(); + BigInteger s = Plugboard.configurationToBigInteger(plugboard.getConfiguration()); + s = addDigit(s, reflector.getRingSetting(), 26); s = addDigit(s, reflector.getRotation(), 26); s = addDigit(s, rotor4.getRingSetting(), 26); s = addDigit(s, rotor4.getRotation(), 26); @@ -260,16 +298,13 @@ public class Enigma_M4 extends Enigma s = addDigit(s, rotor1.getRingSetting(), 26); s = addDigit(s, rotor1.getRotation(), 26); - s = addDigit(s, reflector.getNumber(), 10); - s = addDigit(s, rotor4.getNumber(), 10); - s = addDigit(s, rotor3.getNumber(), 10); - s = addDigit(s, rotor2.getNumber(), 10); - s = addDigit(s, rotor1.getNumber(), 10); + s = addDigit(s, reflector.getIndex(), availableReflectors.size()); + s = addDigit(s, rotor4.getIndex(), availableThinRotors.size()); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); s = addDigit(s, 2, 20); - - save = save+s; - save = save + ":p" + Plugboard.configurationToString(getState().getConfigurationPlugboard()); - return save; + return s.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java index c575d04..7b736c6 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java @@ -1,14 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import java.security.SecureRandom; -import java.util.Random; +import java.math.BigInteger; -import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of type R ("Rocket", Reichsbahn) + * Implementation of the Enigma machine of name R ("Rocket", Reichsbahn) * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -28,35 +27,44 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_R extends Enigma { - protected Rotor entryWheel; + protected EntryWheel entryWheel; protected Rotor rotor1; protected Rotor rotor2; protected Rotor rotor3; protected Reflector reflector; - protected static int machineTypeOffset = 110; - public Enigma_R() { super(); machineType = "R"; } + + @Override + protected void establishAvailableParts() { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + addAvailableRotor(new Rotor.Rotor_R_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_R_II(0,0)); + addAvailableRotor(new Rotor.Rotor_R_III(0,0)); + addAvailableReflector(new Reflector.Reflector_R()); + + } + @Override public void initialize() { - this.entryWheel = Rotor.createRotor(1, 0, 0); - this.rotor1 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.rotor2 = Rotor.createRotor(machineTypeOffset+1, 0, 0); - this.rotor3 = Rotor.createRotor(machineTypeOffset+2, 0, 0); - this.reflector = Reflector.createReflector(machineTypeOffset); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(0); + this.rotor2 = getRotor(1); + this.rotor3 = getRotor(2); + this.reflector = getReflector(0); } @Override public void nextState() { rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) { rotor2.rotate(); this.doAnomaly = rotor2.doubleTurnAnomaly(); @@ -70,10 +78,10 @@ public class Enigma_R extends Enigma @Override protected void generateState() { - int rotor1, rotor2=-1, rotor3=-1; - rotor1 = rand.nextInt(3); - while(rotor2 == -1 || rotor2 == rotor1) rotor2 = rand.nextInt(3); - rotor3 = 3 - rotor1 - rotor2; + int r1, r2=-1, r3; + r1 = rand.nextInt(3); + while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); + r3 = 3 - r1 - r2; int rot1 = rand.nextInt(26); int rot2 = rand.nextInt(26); @@ -84,12 +92,11 @@ public class Enigma_R extends Enigma int ring3 = rand.nextInt(26); int ringRef = rand.nextInt(26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + rotor1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + rotor2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + rotor3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset); - reflector.setRotation(rotRef); - reflector.setRingSetting(ringRef); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); } @Override @@ -122,24 +129,22 @@ public class Enigma_R extends Enigma @Override public void setState(EnigmaStateBundle state) { - this.entryWheel = Rotor.createRotor(state.getTypeEntryWheel(), 0, 0); - this.rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = Reflector.createReflector(state.getTypeReflector()); - this.reflector.setRotation(state.getRotationReflector()); - this.reflector.setRingSetting(state.getRingSettingReflector()); + this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); + this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + this.reflector = getReflector(state.getTypeReflector(), state.getRotationReflector(), state.getRingSettingReflector()); } @Override public EnigmaStateBundle getState() { EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getNumber()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getNumber()); - state.setTypeRotor2(rotor2.getNumber()); - state.setTypeRotor3(rotor3.getNumber()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); state.setRotationRotor1(rotor1.getRotation()); state.setRotationRotor2(rotor2.getRotation()); @@ -149,7 +154,7 @@ public class Enigma_R extends Enigma state.setRingSettingRotor2(rotor2.getRingSetting()); state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getNumber()); + state.setTypeReflector(reflector.getIndex()); state.setRotationReflector(reflector.getRotation()); state.setRingSettingReflector(reflector.getRingSetting()); @@ -157,46 +162,43 @@ public class Enigma_R extends Enigma } @Override - public void restoreState(String mem) + public void restoreState(BigInteger s) { - long s = Long.valueOf(mem); - s = removeDigit(s,20); //Remove machine type - int r1 = getValue(s,10); - s = removeDigit(s,10); - int r2 = getValue(s,10); - s = removeDigit(s,10); - int r3 = getValue(s,10); - s = removeDigit(s,10); + int r1 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); + int r2 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); + int r3 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); - int rot1 = getValue(s,26); - s = removeDigit(s,26); - int ring1 = getValue(s,26); - s = removeDigit(s,26); - int rot2 = getValue(s,26); - s = removeDigit(s,26); - int ring2 = getValue(s,26); - s = removeDigit(s,26); - int rot3 = getValue(s,26); - s = removeDigit(s,26); - int ring3 = getValue(s,26); - s = removeDigit(s,26); - int rotRef = getValue(s,26); - s = removeDigit(s,26); - int ringRef = getValue(s,26); + int rot1 = getValue(s, 26); + s = removeDigit(s, 26); + int ring1 = getValue(s, 26); + s = removeDigit(s, 26); + int rot2 = getValue(s, 26); + s = removeDigit(s, 26); + int ring2 = getValue(s, 26); + s = removeDigit(s, 26); + int rot3 = getValue(s, 26); + s = removeDigit(s, 26); + int ring3 = getValue(s, 26); + s = removeDigit(s, 26); + int rotRef = getValue(s, 26); + s = removeDigit(s, 26); + int ringRef = getValue(s, 26); + s = removeDigit(s, 26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + r1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + r2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + r3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset); - this.reflector.setRotation(rotRef); - this.reflector.setRingSetting(ringRef); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); } @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long t = reflector.getRingSetting(); + BigInteger t = BigInteger.valueOf(reflector.getRingSetting()); t = addDigit(t, reflector.getRotation(), 26); t = addDigit(t, rotor3.getRingSetting(),26); t = addDigit(t, rotor3.getRotation(), 26); @@ -204,12 +206,11 @@ public class Enigma_R extends Enigma t = addDigit(t, rotor2.getRotation(), 26); t = addDigit(t, rotor1.getRingSetting(), 26); t = addDigit(t, rotor1.getRotation(), 26); - t = addDigit(t, rotor3.getNumber(), 10); - t = addDigit(t, rotor2.getNumber(), 10); - t = addDigit(t, rotor1.getNumber(), 10); + t = addDigit(t, rotor3.getIndex(), availableRotors.size()); + t = addDigit(t, rotor2.getIndex(), availableRotors.size()); + t = addDigit(t, rotor1.getIndex(), availableRotors.size()); t = addDigit(t, 10, 20); //Machine #10 - save = save+t; - return save; + return t.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java index 714c936..54222b7 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java @@ -1,16 +1,13 @@ package de.vanitasvitae.enigmandroid.enigma; -import android.util.Log; +import java.math.BigInteger; -import java.security.SecureRandom; -import java.util.Random; - -import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel; import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of type T Tirpitz + * Implementation of the Enigma machine of name T Tirpitz * Copyright (C) 2015 Paul Schaub This program is free software; you can redistribute it and/or modify @@ -30,34 +27,46 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_T extends Enigma { - protected Rotor entryWheel; + protected EntryWheel entryWheel; protected Rotor rotor1; protected Rotor rotor2; protected Rotor rotor3; protected Reflector reflector; - protected static int machineTypeOffset = 120; - public Enigma_T() { super(); machineType = "T"; } + @Override + protected void establishAvailableParts() { + addAvailableEntryWheel(new EntryWheel.EntryWheel_T()); + addAvailableRotor(new Rotor.Rotor_T_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_T_II(0,0)); + addAvailableRotor(new Rotor.Rotor_T_III(0,0)); + addAvailableRotor(new Rotor.Rotor_T_IV(0,0)); + addAvailableRotor(new Rotor.Rotor_T_V(0,0)); + addAvailableRotor(new Rotor.Rotor_T_VI(0,0)); + addAvailableRotor(new Rotor.Rotor_T_VII(0,0)); + addAvailableRotor(new Rotor.Rotor_T_VIII(0,0)); + addAvailableReflector(new Reflector.ReflectorEnigma_T()); + } + @Override public void initialize() { - this.entryWheel = Rotor.createRotor(2,0,0); - this.rotor1 = Rotor.createRotor(machineTypeOffset, 0, 0); - this.rotor2 = Rotor.createRotor(machineTypeOffset+1, 0, 0); - this.rotor3 = Rotor.createRotor(machineTypeOffset+2, 0, 0); - this.reflector = Reflector.createReflector(machineTypeOffset); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(0); + this.rotor2 = getRotor(1); + this.rotor3 = getRotor(2); + this.reflector = getReflector(0); } @Override public void nextState() { rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) { rotor2.rotate(); this.doAnomaly = rotor2.doubleTurnAnomaly(); @@ -70,10 +79,10 @@ public class Enigma_T extends Enigma @Override protected void generateState() { - int rotor1, rotor2=-1, rotor3=-1; - rotor1 = rand.nextInt(8); - while(rotor2 == -1 || rotor2 == rotor1) rotor2 = rand.nextInt(8); - while(rotor3 == -1 || rotor3 == rotor2 || rotor3 == rotor1) rotor3 = rand.nextInt(8); + int r1, r2=-1, r3=-1; + r1 = rand.nextInt(8); + while(r2 == -1 || r2 == r1) r2 = rand.nextInt(8); + while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(8); int rot1 = rand.nextInt(26); int rot2 = rand.nextInt(26); @@ -84,12 +93,11 @@ public class Enigma_T extends Enigma int ring3 = rand.nextInt(26); int ringRef = rand.nextInt(26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + rotor1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + rotor2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + rotor3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset); - reflector.setRotation(rotRef); - reflector.setRingSetting(ringRef); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); } @Override @@ -121,13 +129,11 @@ public class Enigma_T extends Enigma @Override public void setState(EnigmaStateBundle state) { - this.entryWheel = Rotor.createRotor(state.getTypeEntryWheel(), 0, 0); - this.rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = Reflector.createReflector(state.getTypeReflector()); - this.reflector.setRotation(state.getRotationReflector()); - this.reflector.setRingSetting(state.getRingSettingReflector()); + this.entryWheel = getEntryWheel(0); + this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + this.reflector = getReflector(state.getTypeReflector(), state.getRotationReflector(), state.getRingSettingReflector()); } @Override @@ -135,9 +141,9 @@ public class Enigma_T extends Enigma { EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeRotor1(rotor1.getNumber()); - state.setTypeRotor2(rotor2.getNumber()); - state.setTypeRotor3(rotor3.getNumber()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); state.setRotationRotor1(rotor1.getRotation()); state.setRotationRotor2(rotor2.getRotation()); @@ -147,25 +153,23 @@ public class Enigma_T extends Enigma state.setRingSettingRotor2(rotor2.getRingSetting()); state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getNumber()); + state.setTypeReflector(reflector.getIndex()); state.setRotationReflector(reflector.getRotation()); state.setRingSettingReflector(reflector.getRingSetting()); - state.setTypeEntryWheel(entryWheel.getNumber()); + state.setTypeEntryWheel(entryWheel.getIndex()); return state; } @Override - public void restoreState(String mem) + public void restoreState(BigInteger s) { - long s = Long.valueOf(mem); - s = removeDigit(s,20); //Remove machine type - int r1 = getValue(s,10); - s = removeDigit(s,10); - int r2 = getValue(s,10); - s = removeDigit(s,10); - int r3 = getValue(s,10); - s = removeDigit(s,10); + int r1 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); + int r2 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); + int r3 = getValue(s,availableRotors.size()); + s = removeDigit(s,availableRotors.size()); int rot1 = getValue(s,26); s = removeDigit(s,26); @@ -183,19 +187,16 @@ public class Enigma_T extends Enigma s = removeDigit(s,26); int ringRef = getValue(s,26); - this.rotor1 = Rotor.createRotor(machineTypeOffset + r1, rot1, ring1); - this.rotor2 = Rotor.createRotor(machineTypeOffset + r2, rot2, ring2); - this.rotor3 = Rotor.createRotor(machineTypeOffset + r3, rot3, ring3); - this.reflector = Reflector.createReflector(machineTypeOffset); - this.reflector.setRotation(rotRef); - this.reflector.setRingSetting(ringRef); + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); } @Override public String stateToString() { - String save = MainActivity.APP_ID+"/"; - long t = reflector.getRingSetting(); + BigInteger t = BigInteger.valueOf(reflector.getRingSetting()); t = addDigit(t, reflector.getRotation(), 26); t = addDigit(t, rotor3.getRingSetting(),26); t = addDigit(t, rotor3.getRotation(), 26); @@ -203,12 +204,11 @@ public class Enigma_T extends Enigma t = addDigit(t, rotor2.getRotation(), 26); t = addDigit(t, rotor1.getRingSetting(), 26); t = addDigit(t, rotor1.getRotation(), 26); - t = addDigit(t, rotor3.getNumber(), 10); - t = addDigit(t, rotor2.getNumber(), 10); - t = addDigit(t, rotor1.getNumber(), 10); + t = addDigit(t, rotor3.getIndex(), availableRotors.size()); + t = addDigit(t, rotor2.getIndex(), availableRotors.size()); + t = addDigit(t, rotor1.getIndex(), availableRotors.size()); t = addDigit(t, 11, 20); //Machine #11 - save = save+t; - return save; + return t.toString(16); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java index 5eb8015..c04b60e 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java @@ -1,7 +1,12 @@ package de.vanitasvitae.enigmandroid.enigma; +import android.util.Log; + +import java.math.BigInteger; +import java.util.Arrays; import java.util.Random; +import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer; /** @@ -25,7 +30,7 @@ import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer; */ public class Plugboard { - public static final int[] empty = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}; + private static final int[] empty = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}; private int[] plugs; public Plugboard() @@ -43,6 +48,21 @@ public class Plugboard this.plugs = conf; } + public BigInteger setConfiguration(BigInteger b) + { + String s = ""; + + int x; + while((x = Enigma.getValue(b, 27)) != 26 || b.compareTo(BigInteger.ZERO) > 1) + { + s = ((char) (x+65))+s; + b = Enigma.removeDigit(b, 27); + } + Log.d(MainActivity.APP_ID, "Restored: " + s); + this.setConfiguration(stringToConfiguration(s)); + return b; + } + public int[] getConfiguration() { return this.plugs; @@ -70,7 +90,8 @@ public class Plugboard String pairs = trimString(new InputPreparer.RemoveIllegalCharacters().prepareString(in)); int[] out = empty; //Check if in is too long or odd - if(pairs.length() > 26 || pairs.length()/2 == (pairs.length()-1)/2) + int l = pairs.length(); + if(l>1 && (pairs.length() > 26 || pairs.length()/2 == (pairs.length()-1)/2)) { //Odd length. remove last char. Information loss! pairs = pairs.substring(0,pairs.length()-1); @@ -137,13 +158,12 @@ public class Plugboard */ public static int[] seedToReflectorConfiguration(Random rand) { - int[] out = empty; - for(int i=0; i 1) + { + s = ((char) (x+65))+s; + b = Enigma.removeDigit(b, 27); + } + Log.d(MainActivity.APP_ID, "Restored: "+s); + return stringToConfiguration(s); + } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java index e55e3ae..89bf6d1 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java @@ -79,11 +79,11 @@ public abstract class EditTextAdapter public static class EditTextAdapterGap extends EditTextAdapter { - protected int blocksize; - public EditTextAdapterGap(EditText editText, int blocksize) + protected int blockSize; + public EditTextAdapterGap(EditText editText, int blockSize) { super(editText); - this.blocksize = blocksize; + this.blockSize = blockSize; } @Override @@ -92,12 +92,12 @@ public abstract class EditTextAdapter this.content = text; String out = ""; int i; - for(i=0; i 1) + { + s = ((char) (x+65))+s; + b = Enigma.removeDigit(b, 27); + } + Log.d(MainActivity.APP_ID, "Restored: " + s); + this.setConfiguration(Plugboard.stringToConfiguration(s)); + return b; } public int[] getConfiguration() @@ -83,7 +128,7 @@ public class Reflector /** * Factory method to create reflectors. - * @param type type of the created reflector + * @param type name of the created reflector * 1 -> ReflectorA * 2 -> ReflectorB * 3 -> ReflectorC @@ -100,61 +145,25 @@ public class Reflector */ public static Reflector createReflector(int type) { - Log.d(MainActivity.APP_ID, "Reflectorcreation: "+type); switch (type) { - //Enigma I - case 10: return new ReflectorA().setNumber(type); - case 11: return new ReflectorB().setNumber(type); - case 12: return new ReflectorC().setNumber(type); - - //Enigma M3 - case 20: return new ReflectorB().setNumber(type); - case 21: return new ReflectorC().setNumber(type); - - //Enigma M4 - case 30: return new ReflectorThinB().setNumber(type); - case 31: return new ReflectorThinC().setNumber(type); - - //Enigma G31 - case 40: return new ReflectorEnigma_D_KD_G31().setNumber(type); - - //Enigma G312 - case 50: return new Reflector_G312().setNumber(type); - - //Enigma G260 - case 60: return new Reflector_G260().setNumber(type); - - //Enigma D - case 70: return new ReflectorEnigma_D_KD_G31().setNumber(type); - - //Enigma K - case 80: return new ReflectorEnigma_K().setNumber(type); - - //Enigma K Swiss - case 90: return new ReflectorEnigma_K().setNumber(type); - - //Enigma K Swiss Airforce - case 100: return new ReflectorEnigma_K().setNumber(type); - - //Enigma R - case 110: return new Reflector_R().setNumber(type); - - //Enigma T - case 120: return new ReflectorEnigma_T().setNumber(type); + case 0: return new Reflector_A(); + case 1: return new Reflector_B(); + case 2: return new Reflector_C(); + case 10: return new Reflector_Thin_B(); + case 11: return new ReflectorThinC(); + case 20: return new ReflectorEnigma_D_KD_G31(); + case 30: return new Reflector_G312(); + case 40: return new Reflector_K_G260(); + case 50: return new Reflector_R(); + case 60: return new ReflectorEnigma_T(); default: - Log.e(MainActivity.APP_ID," Tried to create Reflector of invalid type "+type); + Log.e(MainActivity.APP_ID," Tried to create Reflector of invalid name "+type); return null; } } - public Reflector setNumber(int nr) - { - this.number = nr; - return this; - } - /** * Substitute an input signal via the wiring of the reflector with a different (!) output. * The output MUST not be equal to the input for any input, since this was not possible @@ -167,20 +176,6 @@ public class Reflector return this.connections[normalize(input)]; } - /** - * Return the type indicator of the reflector - * @return type - */ - public String getType() - { - return this.type; - } - - public int getNumber() - { - return this.number; - } - /** * Return the size (ie the number of wires/length of the connections array) of the reflector * @return size @@ -208,11 +203,12 @@ public class Reflector * Used in Enigma I * AE BJ CM DZ FL GY HX IV KW NR OQ PU ST */ - private static class ReflectorA extends Reflector + public static class Reflector_A extends Reflector { - public ReflectorA() + public Reflector_A() { - super("A", new int[]{4,9,12,25,0,11,24,23,21,1,22,5,2,17,16,20,14,13,19,18,15,8,10,7,6,3}); + super(0, "A", "EJMZALYXVBWFCRQUONTSPIKHGD", + new int[]{4,9,12,25,0,11,24,23,21,1,22,5,2,17,16,20,14,13,19,18,15,8,10,7,6,3}); } } @@ -221,11 +217,12 @@ public class Reflector * Used in Enigma I, M3 * AY BR CU DH EQ FS GL IP JX KN MO TZ VW */ - private static class ReflectorB extends Reflector + public static class Reflector_B extends Reflector { - public ReflectorB() + public Reflector_B() { - super("B", new int[]{24,17,20,7,16,18,11,3,15,23,13,6,14,10,12,8,4,1,5,25,2,22,21,9,0,19}); + super(1, "B", "YRUHQSLDPXNGOKMIEBFZCWVJAT", + new int[]{24,17,20,7,16,18,11,3,15,23,13,6,14,10,12,8,4,1,5,25,2,22,21,9,0,19}); } } @@ -234,112 +231,110 @@ public class Reflector * Used in Enigma I, M3 * AF BV CP DJ EI GO HY KR LZ MX NW QT SU */ - private static class ReflectorC extends Reflector + public static class Reflector_C extends Reflector { - public ReflectorC() + public Reflector_C() { - super("C", new int[]{5,21,15,9,8,0,14,24,4,3,17,25,23,22,6,2,19,10,20,16,18,1,13,12,7,11}); + super(2, "C", "FVPJIAOYEDRZXWGCTKUGSBNMHL", + new int[]{5,21,15,9,8,0,14,24,4,3,17,25,23,22,6,2,19,10,20,16,18,1,13,12,7,11}); } } /** - * Concrete implementation of thin reflector type b (not equal to normal type b!) + * Concrete implementation of thin reflector name b (not equal to normal name b!) * When used with Rotor Beta on rotation 0, the pair was equivalent to normal reflector B * S->Beta->ThinB->Beta'->X == X->UKWB->S * Used in Enigma M4 * E N K Q A U Y W J I C O P B L M D X Z V F T H R G S */ - private static class ReflectorThinB extends Reflector + public static class Reflector_Thin_B extends Reflector { - public ReflectorThinB() + public Reflector_Thin_B() { - super("ThinB", new int[]{4,13,10,16,0,20,24,22,9,8,2,14,15,1,11,12,3,23,25,21,5,19,7,17,6,18}); + super(10, "Thin-B", "ENKQAUYWJICOPBLMDXZVFTHRGS", + new int[]{4,13,10,16,0,20,24,22,9,8,2,14,15,1,11,12,3,23,25,21,5,19,7,17,6,18}); } } /** - * Concrete implementation of thin reflector type c (not equal to normal type c!) + * Concrete implementation of thin reflector name c (not equal to normal name c!) * When used with Rotor Gamma on rotation 0, the pair was equivalent to normal reflector C * S->Gamma->ThinC->Gamma'->X == X->UKWC->S * Used in Enigma M4 * R D O B J N T K V E H M L F C W Z A X G Y I P S U Q */ - private static class ReflectorThinC extends Reflector + public static class ReflectorThinC extends Reflector { public ReflectorThinC() { - super("ThinC", new int[]{17,3,14,1,9,13,19,10,21,4,7,12,11,5,2,22,25,0,23,6,24,8,15,18,20,16}); + super(11, "ThinC", "RDOBJNTKVEHMLFCWZAXGYIPSUQ", + new int[]{17,3,14,1,9,13,19,10,21,4,7,12,11,5,2,22,25,0,23,6,24,8,15,18,20,16}); } } /** - * Pluggable Reflector of the Enigma machine of type D and KD + * Pluggable Reflector of the Enigma machine of name D and KD * Standard wiring: AI,BM,CE,DT,FG,HR,JY,KS,LQ,NZ,OX,PW,UV * Has additional ringSetting and can rotate */ public static class ReflectorEnigma_D_KD_G31 extends Reflector { + public static final int[] defaultWiring_D_KD_G31 = {8,12,4,19,2,6,5,17,0,24,18,16,1,25,23,22,11,7,10,3,21,20,15,14,9,13}; public ReflectorEnigma_D_KD_G31() { - super("Ref-D", defaultWiring_D_KD_G31); + super(20, "Ref-D", "Default: IMETCGFRAYSQBZXWLHKDVUPOJN", defaultWiring_D_KD_G31); } } /** - * Reflector as used in various Enigma models of the K-Series - * I M E T C G F R A Y S Q B Z X W L H K D V U P O J N - */ - private static class ReflectorEnigma_K extends Reflector - { - public ReflectorEnigma_K() - { - super("Ref-K", new int[]{8,12,4,19,2,6,5,17,0,24,18,16,1,25,23,22,11,7,10,3,21,20,15,14,9,13}); - } - } - /** - * Reflector as used in the Enigma type T (Tirpitz) - * G E K P B T A U M O C N I L J D X Z Y F H W V Q S R - */ - private static class ReflectorEnigma_T extends Reflector - { - public ReflectorEnigma_T() - { - super("Ref-T", new int[]{6,4,10,15,1,19,0,20,12,14,2,13,8,11,9,3,23,25,24,5,7,22,21,16,18,17}); - } - } - - /** - * Reflector as used in the Enigma type G-312 Abwehr + * Reflector as used in the Enigma name G-312 Abwehr * R U L Q M Z J S Y G O C E T K W D A H N B X P V I F */ - private static class Reflector_G312 extends Reflector + public static class Reflector_G312 extends Reflector { public Reflector_G312() { - super("Ref-G312", new int[]{17,20,11,16,12,25,9,18,24,6,14,2,4,19,10,22,3,0,7,13,1,23,15,21,8,5}); + super(30, "Ref-G312", "RULQMZJSYGOCETKWDAHNBXPVIF", + new int[]{17,20,11,16,12,25,9,18,24,6,14,2,4,19,10,22,3,0,7,13,1,23,15,21,8,5}); } } /** - * Reflector as used in the Enigma type G-260 Abwehr + * Reflector as used in the Enigma name G-260 Abwehr * I M E T C G F R A Y S Q B Z X W L H K D V U P O J N */ - private static class Reflector_G260 extends Reflector + public static class Reflector_K_G260 extends Reflector { - public Reflector_G260() + public Reflector_K_G260() { - super("Ref-G260", new int[]{8,12,4,19,2,6,5,17,0,24,18,16,1,25,23,22,11,7,10,3,21,20,15,14,9,13}); + super(40,"Ref-K/G260", "IMETCGFRAYSQBZXWLHKDVUPOJN", + new int[]{8,12,4,19,2,6,5,17,0,24,18,16,1,25,23,22,11,7,10,3,21,20,15,14,9,13}); } } /** * Reflector as used in the Enigma Type R "Rocket" (Reichsbahn) + * Q Y H O G N E C V P U Z T F D J A X W M K J S R B L */ - private static class Reflector_R extends Reflector + public static class Reflector_R extends Reflector { public Reflector_R() { - super("Ref-R", new int[]{16,24,7,14,6,13,4,2,21,15,20,25,19,5,3,9,0,23,22,12,10,8,18,17,1,11}); + super(50, "Ref-R", "QYHOGNECVPUZTFDJAXWMKJSRBL", + new int[]{16,24,7,14,6,13,4,2,21,15,20,25,19,5,3,9,0,23,22,12,10,8,18,17,1,11}); + } + } + + /** + * Reflector as used in the Enigma name T (Tirpitz) + * G E K P B T A U M O C N I L J D X Z Y F H W V Q S R + */ + public static class ReflectorEnigma_T extends Reflector + { + public ReflectorEnigma_T() + { + super(60, "Ref-T", "GEKPBTAUMOCNILJDXZYFHWVQSR", + new int[]{6,4,10,15,1,19,0,20,12,14,2,13,8,11,9,3,23,25,24,5,7,22,21,16,18,17}); } } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java index 4e5fc8e..5bb4730 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java @@ -28,15 +28,33 @@ import de.vanitasvitae.enigmandroid.MainActivity; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * @author vanitasvitae */ -public class Rotor +public abstract class Rotor { - protected String type; - protected int number; + /** Number of the rotor (used internally to create the Rotor via createRotor() ) */ + protected int type; + + /** Identifier of the Rotor */ + protected String name; + + /** Index of the Rotor in the parent machine's selection Spinner */ + protected int index; + + /** Summary of the connections (internal wiring) */ + protected String summary; + + /** Wiring of the rotor when the signal passes the first time */ protected Integer[] connections; + + /** Wiring of the rotor when the signal passes the second time (inverse of the first time) */ protected Integer[] reversedConnections; + + /** When the Rotor is at this Position and jumps one over, it also turns the next */ protected Integer[] turnOverNotches; + /** Offset of the labeled ring of the rotor */ protected int ringSetting; + + /** Rotation of the rotor */ protected int rotation; /** @@ -45,7 +63,7 @@ public class Rotor * Note that connections and reversedConnections MUST be of the same size and that * neither connections nor reversedConnections respectively MUST have any number between * 0 and connections.length-1 only once (ie they represent permutations) - * @param type type indicator + * @param name name indicator * @param connections wiring of the rotor as Integer array * @param reversedConnections inverse wiring used to encryptString in the opposite direction * (connections[reversedConnections[i]] = i @@ -54,10 +72,12 @@ public class Rotor * @param ringSetting setting of the ring that holds the letters * @param rotation rotation of the rotor */ - protected Rotor(String type, Integer[] connections, Integer[] reversedConnections, + protected Rotor(int type, String name, String summary, Integer[] connections, Integer[] reversedConnections, Integer[] turnOverNotches, int ringSetting, int rotation) { this.type = type; + this.name = name; + this.summary = summary; this.connections = connections; this.reversedConnections = reversedConnections; this.turnOverNotches = turnOverNotches; @@ -65,111 +85,83 @@ public class Rotor this.rotation = rotation; } - protected Rotor setTypeNumber(int typenr) + /** + * Create a new Rotor of the same type as the callee. + * @param rotation rotation of the new Rotor + * @param ringSetting ringSetting of the new Rotor + * @return new Rotor + */ + public Rotor getInstance(int rotation, int ringSetting) { - this.number = typenr; - return this; + //noinspection ConstantConditions + return createRotor(this.type, rotation, ringSetting).setIndex(this.getIndex()); } /** - * Factory method that creates a rotor accordingly to the type. - * Also initialize the rotor with ringSetting and rotation. - * @param type type indicator (1..10) - * 1..8 -> I..VIII - * 9,10 -> Beta, Gamma - * 11..13 -> DI..DIII - * @param ringSetting setting of the outer ring (0..25) - * @param rotation rotation of the rotor - * @return Concrete rotor + * Create a new Rotor of the same type as the callee. + * @return new Rotor */ - public static Rotor createRotor(int type, int rotation, int ringSetting) + public Rotor getInstance() { - Log.d(MainActivity.APP_ID, "Rotorcreation: "+type); + //noinspection ConstantConditions + return createRotor(this.type, 0, 0).setIndex(this.getIndex()); + } + + private static Rotor createRotor(int type, int rotation, int ringSetting) + { + Log.d(MainActivity.APP_ID, "Rotor creation: "+type); switch (type) { - case 1: return new EntryWheel_QWERTZ().setTypeNumber(type); - case 2: return new EntryWheel_T().setTypeNumber(type); + case 0: return new Rotor_I(rotation, ringSetting); + case 1: return new Rotor_II(rotation, ringSetting); + case 2: return new Rotor_III(rotation, ringSetting); + case 3: return new Rotor_IV(rotation, ringSetting); + case 4: return new Rotor_V(rotation, ringSetting); + case 5: return new Rotor_VI(rotation, ringSetting); + case 6: return new Rotor_VII(rotation, ringSetting); + case 7: return new Rotor_VIII(rotation, ringSetting); - //Enigma I - case 10: return new Rotor_I(rotation, ringSetting).setTypeNumber(type); - case 11: return new Rotor_II(rotation, ringSetting).setTypeNumber(type); - case 12: return new Rotor_III(rotation, ringSetting).setTypeNumber(type); - case 13: return new Rotor_IV(rotation, ringSetting).setTypeNumber(type); - case 14: return new Rotor_V(rotation, ringSetting).setTypeNumber(type); + case 10: return new Rotor_M4_Beta(rotation, ringSetting); + case 11: return new Rotor_M4_Gamma(rotation, ringSetting); - //Enigma M3 - case 20: return new Rotor_I(rotation, ringSetting).setTypeNumber(type); - case 21: return new Rotor_II(rotation, ringSetting).setTypeNumber(type); - case 22: return new Rotor_III(rotation, ringSetting).setTypeNumber(type); - case 23: return new Rotor_IV(rotation, ringSetting).setTypeNumber(type); - case 24: return new Rotor_V(rotation, ringSetting).setTypeNumber(type); - case 25: return new Rotor_VI(rotation, ringSetting).setTypeNumber(type); - case 26: return new Rotor_VII(rotation, ringSetting).setTypeNumber(type); - case 27: return new Rotor_VIII(rotation, ringSetting).setTypeNumber(type); + case 20: return new Rotor_G31_I(rotation, ringSetting); + case 21: return new Rotor_G31_II(rotation, ringSetting); + case 22: return new Rotor_G31_III(rotation, ringSetting); - //Enigma M4 - case 30: return new Rotor_I(rotation, ringSetting).setTypeNumber(type); - case 31: return new Rotor_II(rotation, ringSetting).setTypeNumber(type); - case 32: return new Rotor_III(rotation, ringSetting).setTypeNumber(type); - case 33: return new Rotor_IV(rotation, ringSetting).setTypeNumber(type); - case 34: return new Rotor_V(rotation, ringSetting).setTypeNumber(type); - case 35: return new Rotor_VI(rotation, ringSetting).setTypeNumber(type); - case 36: return new Rotor_VII(rotation, ringSetting).setTypeNumber(type); - case 37: return new Rotor_VIII(rotation, ringSetting).setTypeNumber(type); - case 38: return new Rotor_M4_Beta(rotation, ringSetting).setTypeNumber(type); - case 39: return new Rotor_M4_Gamma(rotation, ringSetting).setTypeNumber(type); + case 30: return new Rotor_G312_I(rotation, ringSetting); + case 31: return new Rotor_G312_II(rotation, ringSetting); + case 32: return new Rotor_G312_III(rotation, ringSetting); - //Enigma G31 - case 40: return new Rotor_G31_I(rotation, ringSetting).setTypeNumber(type); - case 41: return new Rotor_G31_II(rotation, ringSetting).setTypeNumber(type); - case 42: return new Rotor_G31_III(rotation, ringSetting).setTypeNumber(type); + case 40: return new Rotor_G260_I(rotation, ringSetting); + case 41: return new Rotor_G260_II(rotation, ringSetting); + case 42: return new Rotor_G260_III(rotation, ringSetting); - //Enigma G312 - case 50: return new Rotor_G312_I(rotation, ringSetting).setTypeNumber(type); - case 51: return new Rotor_G312_II(rotation, ringSetting).setTypeNumber(type); - case 52: return new Rotor_G312_III(rotation, ringSetting).setTypeNumber(type); + case 50: return new Rotor_K_D_I(rotation, ringSetting); + case 51: return new Rotor_K_D_II(rotation, ringSetting); + case 52: return new Rotor_K_D_III(rotation, ringSetting); - //Enigma G260 - case 60: return new Rotor_G260_I(rotation, ringSetting).setTypeNumber(type); - case 61: return new Rotor_G260_II(rotation, ringSetting).setTypeNumber(type); - case 62: return new Rotor_G260_III(rotation, ringSetting).setTypeNumber(type); + case 60: return new Rotor_KSwiss_Standard_I(rotation, ringSetting); + case 61: return new Rotor_KSwiss_Standard_II(rotation, ringSetting); + case 62: return new Rotor_KSwiss_Standard_III(rotation, ringSetting); - //Enigma D - case 70: return new Rotor_D_I(rotation, ringSetting).setTypeNumber(type); - case 71: return new Rotor_D_II(rotation, ringSetting).setTypeNumber(type); - case 72: return new Rotor_D_III(rotation, ringSetting).setTypeNumber(type); + case 70: return new Rotor_K_Swiss_Airforce_I(rotation, ringSetting); + case 71: return new Rotor_K_Swiss_Airforce_II(rotation, ringSetting); + case 72: return new Rotor_K_Swiss_Airforce_III(rotation, ringSetting); - //Enigma K - case 80: return new Rotor_K_I(rotation, ringSetting).setTypeNumber(type); - case 81: return new Rotor_K_II(rotation, ringSetting).setTypeNumber(type); - case 82: return new Rotor_K_III(rotation, ringSetting).setTypeNumber(type); + case 80: return new Rotor_R_I(rotation, ringSetting); + case 81: return new Rotor_R_II(rotation, ringSetting); + case 82: return new Rotor_R_III(rotation, ringSetting); - //Enigma K Swiss - case 90: return new Rotor_KSwiss_Standard_I(rotation, ringSetting).setTypeNumber(type); - case 91: return new Rotor_KSwiss_Standard_II(rotation, ringSetting).setTypeNumber(type); - case 92: return new Rotor_KSwiss_Standard_III(rotation, ringSetting).setTypeNumber(type); + case 90: return new Rotor_T_I(rotation, ringSetting); + case 91: return new Rotor_T_II(rotation, ringSetting); + case 92: return new Rotor_T_III(rotation, ringSetting); + case 93: return new Rotor_T_IV(rotation, ringSetting); + case 94: return new Rotor_T_V(rotation, ringSetting); + case 95: return new Rotor_T_VI(rotation, ringSetting); + case 96: return new Rotor_T_VII(rotation, ringSetting); + case 97: return new Rotor_T_VIII(rotation, ringSetting); - //Enigma K Swiss Airforce - case 100: return new Rotor_K_Swiss_Airforce_I(rotation, ringSetting).setTypeNumber(type); - case 101: return new Rotor_K_Swiss_Airforce_II(rotation, ringSetting).setTypeNumber(type); - case 102: return new Rotor_K_Swiss_Airforce_III(rotation, ringSetting).setTypeNumber(type); - - //Enigma R - case 110: return new Rotor_R_I(rotation, ringSetting).setTypeNumber(type); - case 111: return new Rotor_R_II(rotation, ringSetting).setTypeNumber(type); - case 112: return new Rotor_R_III(rotation, ringSetting).setTypeNumber(type); - - //Enigma T - case 120: return new Rotor_T_I(rotation, ringSetting).setTypeNumber(type); - case 121: return new Rotor_T_II(rotation, ringSetting).setTypeNumber(type); - case 122: return new Rotor_T_III(rotation, ringSetting).setTypeNumber(type); - case 123: return new Rotor_T_IV(rotation, ringSetting).setTypeNumber(type); - case 124: return new Rotor_T_V(rotation, ringSetting).setTypeNumber(type); - case 125: return new Rotor_T_VI(rotation, ringSetting).setTypeNumber(type); - case 126: return new Rotor_T_VII(rotation, ringSetting).setTypeNumber(type); - case 127: return new Rotor_T_VIII(rotation, ringSetting).setTypeNumber(type); - - default: Log.e(MainActivity.APP_ID," Tried to create Rotor of invalid type "+type); + default: Log.e(MainActivity.APP_ID," Tried to create Rotor of invalid name "+type); return null; } } @@ -197,17 +189,45 @@ public class Rotor } /** - * Return the type indicator (usually 1..5) - * @return type indicator + * Return the name indicator (usually 1..5) + * @return name indicator */ - public String getType() + public String getName() { - return this.type; + return this.name; } - public int getNumber() + /** + * Return the index of this Rotor + * @return index + */ + public int getIndex() { - return this.number; + return this.index; + } + + /** + * Set the index of this Rotor. The index refers to the selector Spinner of the machine, this + * Rotor is used in. + * @param index index in the Spinner + * @return this + */ + public Rotor setIndex(int index) + { + this.index = index; + return this; + } + + public Rotor setRotation(int r) + { + this.rotation = r%this.getRotorSize(); + return this; + } + + public Rotor setRingSetting(int r) + { + this.ringSetting = r%this.getRotorSize(); + return this; } /** @@ -223,9 +243,10 @@ public class Rotor /** * Increment rotation of the rotor by one. */ - public void rotate() + public Rotor rotate() { this.rotation = normalize(this.getRotation()+1); + return this; } /** @@ -303,15 +324,15 @@ public class Rotor } /** - * Concrete implementation of Rotor of type 1 (I) + * Concrete implementation of Rotor of name 1 (I) * Used in Enigma I, M3, M4 * E K M F L G D Q V Z N T O W Y H X U S P A I B R C J */ - private static class Rotor_I extends Rotor + public static class Rotor_I extends Rotor { public Rotor_I(int rotation, int ringSetting) { - super("I", + super(0, "I", "EKMFLGDQVZNTOWYHXUSPAIBRCJ", new Integer[]{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}, new Integer[]{20, 22, 24, 6, 0, 3, 5, 15, 21, 25, 1, 4, 2, 10, 12, 19, 7, 23, 18, 11, 17, 8, 13, 16, 14, 9}, new Integer[]{17}, ringSetting, rotation); @@ -319,15 +340,15 @@ public class Rotor } /** - * Concrete implementation of Rotor of type 2 (II) + * Concrete implementation of Rotor of name 2 (II) * Used in Enigma I, M3, M4 * A J D K S I R U X B L H W T M C Q G Z N P Y F V O E */ - private static class Rotor_II extends Rotor + public static class Rotor_II extends Rotor { public Rotor_II(int rotation, int ringSetting) { - super("II", + super(1, "II", "AJDKSIRUXBLHWTMCQGZNPYFVOE", new Integer[]{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}, new Integer[]{0, 9, 15, 2, 25, 22, 17, 11, 5, 1, 3, 10, 14, 19, 24, 20, 16, 6, 4, 13, 7, 23, 12, 8, 21, 18}, new Integer[]{5}, ringSetting, rotation); @@ -335,15 +356,15 @@ public class Rotor } /** - * Concrete implementation of Rotor of type 3 (III) + * Concrete implementation of Rotor of name 3 (III) * Used in Enigma I, M3, M4 * B D F H J L C P R T X V Z N Y E I W G A K M U S Q O */ - private static class Rotor_III extends Rotor + public static class Rotor_III extends Rotor { public Rotor_III(int rotation, int ringSetting) { - super("III", + super(2, "III", "BDFHJLCPRTXVZNYEIWGAKMUSQO", new Integer[]{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}, new Integer[]{19, 0, 6, 1, 15, 2, 18, 3, 16, 4, 20, 5, 21, 13, 25, 7, 24, 8, 23, 9, 22, 11, 17, 10, 14, 12}, new Integer[]{22}, ringSetting, rotation); @@ -351,15 +372,15 @@ public class Rotor } /** - * Concrete implementation of Rotor of type 4 (IV) + * Concrete implementation of Rotor of name 4 (IV) * Used in Enigma M3, M4 * E S O V P Z J A Y Q U I R H X L N F T G K D C M W B */ - private static class Rotor_IV extends Rotor + public static class Rotor_IV extends Rotor { public Rotor_IV(int rotation, int ringSetting) { - super("IV", + super(3, "IV", "ESOVPZJAYQUIRHXLNFTGKDCMWB", new Integer[]{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}, new Integer[]{7, 25, 22, 21, 0, 17, 19, 13, 11, 6, 20, 15, 23, 16, 2, 4, 9, 12, 1, 18, 10, 3, 24, 14, 8, 5}, new Integer[]{10}, ringSetting, rotation); @@ -367,15 +388,15 @@ public class Rotor } /** - * Concrete implementation of Rotor of type 5 (V) + * Concrete implementation of Rotor of name 5 (V) * Used in Enigma M3, M4 * V Z B R G I T Y U P S D N H L X A W M J Q O F E C K */ - private static class Rotor_V extends Rotor + public static class Rotor_V extends Rotor { public Rotor_V(int rotation, int ringSetting) { - super("V", + super(4, "V", "VZBRGITYUPSDNHLXAWMJQOFECK", new Integer[]{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}, new Integer[]{16, 2, 24, 11, 23, 22, 4, 13, 5, 19, 25, 14, 18, 12, 21, 9, 20, 3, 10, 6, 8, 0, 17, 15, 7, 1}, new Integer[]{0}, ringSetting, rotation); @@ -383,15 +404,15 @@ public class Rotor } /** - * Concrete implementation of Rotor of type 6 (VI) + * Concrete implementation of Rotor of name 6 (VI) * Used in Enigma M3, M4 * J P G V O U M F Y Q B E N H Z R D K A S X L I C T W */ - private static class Rotor_VI extends Rotor + public static class Rotor_VI extends Rotor { public Rotor_VI(int rotation, int ringSetting) { - super("VI", + super(5, "VI", "JPGVOUMFYQBENHZRDKASXLICTW", new Integer[]{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}, new Integer[]{18,10,23,16,11,7,2,13,22,0,17,21,6,12,4,1,9,15,19,24,5,3,25,20,8,14}, new Integer[]{0,13}, ringSetting, rotation); @@ -399,15 +420,15 @@ public class Rotor } /** - * Concrete implementation of Rotor of type 7 (VII) + * Concrete implementation of Rotor of name 7 (VII) * Used in Enigma M3, M4 * N Z J H G R C X M Y S W B O U F A I V L P E K Q D T */ - private static class Rotor_VII extends Rotor + public static class Rotor_VII extends Rotor { public Rotor_VII(int rotation, int ringSetting) { - super("VII", + super(6, "VII", "NZJHGRCXMYSWBOUFAIVLPEKQDT", new Integer[]{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}, new Integer[]{16,12,6,24,21,15,4,3,17,2,22,19,8,0,13,20,23,5,10,25,14,18,11,7,9,1}, new Integer[]{0,13}, ringSetting, rotation); @@ -415,15 +436,15 @@ public class Rotor } /** - * Concrete implementation of Rotor of type 8 (VIII) + * Concrete implementation of Rotor of name 8 (VIII) * Used in Enigma M3, M4 * F K Q H T L X O C B J S P D Z R A M E W N I U Y G V */ - private static class Rotor_VIII extends Rotor + public static class Rotor_VIII extends Rotor { public Rotor_VIII(int rotation, int ringSetting) { - super("VIII", + super(7, "VIII", "FKQHTLXOCBJSPDZRAMEWNIUYGV", new Integer[]{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}, new Integer[]{16,9,8,13,18,0,24,3,21,10,1,5,17,20,7,12,2,15,11,4,22,25,19,6,23,14}, new Integer[]{0,13}, ringSetting, rotation); @@ -431,26 +452,27 @@ public class Rotor } /** - * Concrete implementation of Rotor of type beta (Griechenwalze Beta) + * Concrete implementation of Rotor of name beta (Griechenwalze Beta) * Beta was used as a "thin" rotor in the M4. It was thinner than a "normal" rotor, so it * could be used together with one of the two thin reflectors as one rotor. * When used together with ReflectorThinB, Beta was equivalent to Reflector B (if rotation == 0) * That way the M4 was backwards compatible to the M3 * Used in M4 */ - private static class Rotor_M4_Beta extends Rotor + public static class Rotor_M4_Beta extends Rotor { public Rotor_M4_Beta(int rotation, int ringSetting) { - super("Beta", + super(10, "Beta", "LEYJVCNIXWPBQMDRTAKZGFUHOS", new Integer[]{11,4,24,9,21,2,13,8,23,22,15,1,16,12,3,17,19,0,10,25,6,5,20,7,14,18}, new Integer[]{17,11,5,14,1,21,20,23,7,3,18,0,13,6,24,10,12,15,25,16,22,4,9,8,2,19}, new Integer[]{}, ringSetting, rotation); } @Override - public void rotate() + public Rotor rotate() { - //Thin rotors are fixed in position, so they dont rotate + //Thin rotors are fixed in position, so they don't rotate + return this; } @Override @@ -462,26 +484,27 @@ public class Rotor } /** - * Concrete implementation of Rotor of type gamma (Griechenwalze Gamma) + * Concrete implementation of Rotor of name gamma (Griechenwalze Gamma) * Gamma was used as a "thin" rotor in the M4. It was thinner than a "normal" rotor, so it * could be used together with one of the two thin reflectors as one rotor. * When used together with ReflectorThinC, Gamma is equivalent to Reflector C * (if rotation == 0). That way the M4 was backwards compatible to the M3 * Used in M4 */ - private static class Rotor_M4_Gamma extends Rotor + public static class Rotor_M4_Gamma extends Rotor { public Rotor_M4_Gamma(int rotation, int ringSetting) { - super("Gamma", + super(11, "Gamma", "FSOKANUERHMBTIYCWLQPZXVGJD", new Integer[]{5,18,14,10,0,13,20,4,17,7,12,1,19,8,24,2,22,11,16,15,25,23,21,6,9,3}, new Integer[]{4,11,15,25,7,0,23,9,13,24,3,17,10,5,2,19,18,8,1,12,6,22,16,21,14,20}, new Integer[]{}, ringSetting, rotation); } @Override - public void rotate() + public Rotor rotate() { //Thin rotors are fixed in position, so they don't rotate + return this; } @Override @@ -492,388 +515,16 @@ public class Rotor } } - /** - * EntryWheel as used in the Enigma models D, K, G - * Q W E R T Z U I O A S D F G H J K P Y X C V B N M L - */ - private static class EntryWheel_QWERTZ extends Rotor - { - public EntryWheel_QWERTZ() - { - super("ETW-D", - new Integer[]{9,22,20,11,2,12,13,14,7,15,16,25,24,23,8,17,0,3,10,4,6,21,1,19,18,5}, - new Integer[]{16,22,4,17,19,25,20,8,14,0,18,3,5,6,7,9,10,15,24,23,2,21,1,13,12,11}, - new Integer[]{}, 0, 0); - } - @Override - public void rotate() - { - //EntryWheel doesn't rotate - } - - @Override - public boolean doubleTurnAnomaly() - { - //\forall s \in States : nope - return false; - } - } - - /** - * Rotor I as used in the Enigma Type D - * L P G S Z M H A E O Q K V X R F Y B U T N I C J D W - * Turnover Z - */ - private static class Rotor_D_I extends Rotor - { - public Rotor_D_I(int rotation, int ringSetting) - { - super("D-I", - new Integer[]{11,15,6,18,25,12,7,0,4,14,16,10,21,23,17,5,24,1,20,19,13,8,2,9,3,22}, - new Integer[]{7,17,22,24,8,15,2,6,21,23,11,0,5,20,9,1,10,14,3,19,18,12,25,13,16,4}, - new Integer[]{25}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type D - * S L V G B T F X J Q O H E W I R Z Y A M K P C N D U - * Turnover F - */ - private static class Rotor_D_II extends Rotor - { - public Rotor_D_II(int rotation, int ringSetting) - { - super("D-II", - new Integer[]{18,11,21,6,1,19,5,23,9,16,14,7,4,22,8,17,25,24,0,12,10,15,2,13,3,20}, - new Integer[]{18,4,22,24,12,6,3,11,14,8,20,1,19,23,10,21,9,15,0,5,25,2,13,7,17,16}, - new Integer[]{5}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type D - * C J G D P S H K T U R A W Z X F M Y N Q O B V L I E - * Turnover O - */ - private static class Rotor_D_III extends Rotor - { - public Rotor_D_III(int rotation, int ringSetting) - { - super("D-III", - new Integer[]{2,9,6,3,15,18,7,10,19,20,17,0,22,25,23,5,12,24,13,16,14,1,21,11,8,4}, - new Integer[]{11,21,0,3,25,15,2,6,24,1,7,23,16,18,20,4,19,10,5,8,9,22,12,14,17,13}, - new Integer[]{14}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type K (Switzerland) - * P E Z U O H X S C V F M T B G L R I N Q J W A Y D K - * Turnover Z - */ - private static class Rotor_KSwiss_Standard_I extends Rotor - { - public Rotor_KSwiss_Standard_I(int rotation, int ringSetting) - { - super("KS-I", - new Integer[]{15,4,25,20,14,7,23,18,2,21,5,12,19,1,6,11,17,8,13,16,9,22,0,24,3,10}, - new Integer[]{22,13,8,24,1,10,14,5,17,20,25,15,11,18,4,0,19,16,7,12,3,9,21,6,23,2}, - new Integer[]{25}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type K (Switzerland) - * Z O U E S Y D K F W P C I Q X H M V B L G N J R A T - * Turnover F - */ - private static class Rotor_KSwiss_Standard_II extends Rotor - { - public Rotor_KSwiss_Standard_II(int rotation, int ringSetting) - { - super("KS-II", - new Integer[]{25,14,20,4,18,24,3,10,5,22,15,2,8,16,23,7,12,21,1,11,6,13,9,17,0,19}, - new Integer[]{24,18,11,6,3,8,20,15,12,22,7,19,16,21,1,10,13,23,4,25,2,17,9,14,5,0}, - new Integer[]{5}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type K (Switzerland) - * E H R V X G A O B Q U S I M Z F L Y N W K T P D J C - * Turnover O - */ - private static class Rotor_KSwiss_Standard_III extends Rotor - { - public Rotor_KSwiss_Standard_III(int rotation, int ringSetting) - { - super("KS-III", - new Integer[]{4,7,17,21,23,6,0,14,1,16,20,18,8,12,25,5,11,24,13,22,10,19,15,3,9,2}, - new Integer[]{6,8,25,23,0,15,5,1,12,24,20,16,13,18,7,22,9,2,11,21,10,3,19,4,17,14}, - new Integer[]{14}, ringSetting, rotation); - } - } - - /** - * EntryWheel as used only in the Enigma Type T Tirpitz - * K Z R O U Q H Y A I G B L W V S T D X F P N M C J E - */ - private static class EntryWheel_T extends Rotor - { - public EntryWheel_T() - { - super("T-ETW", - new Integer[]{8,11,23,17,25,19,10,6,9,24,0,12,22,21,3,20,5,2,15,16,4,14,13,18,7,1}, - new Integer[]{10,25,17,14,20,16,7,24,0,8,6,1,11,22,21,18,19,3,23,5,15,13,12,2,9,4}, - new Integer[]{}, 0, 0); - } - @Override - public void rotate() - { - //EntryWheel doesn't rotate - } - - @Override - public boolean doubleTurnAnomaly() - { - //\forall s \in States : nope - return false; - } - } - /** - * Rotor I as used in the Enigma Type T Tirpitz - * K P T Y U E L O C V G R F Q D A N J M B S W H Z X I - * Turnover X A F L R - */ - private static class Rotor_T_I extends Rotor - { - public Rotor_T_I(int rotation, int ringSetting) - { - super("T-I", - new Integer[]{10,15,19,24,20,4,11,14,2,21,6,17,5,16,3,0,13,9,12,1,18,22,7,25,23,8}, - new Integer[]{15,19,8,14,5,12,10,22,25,17,0,6,18,16,7,1,13,11,20,2,4,9,21,24,3,23}, - new Integer[]{23,0,5,11,17}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type T Tirpitz - * U P H Z L W E Q M T D J X C A K S O I G V B Y F N R - * Turnover X A G M S - */ - private static class Rotor_T_II extends Rotor - { - public Rotor_T_II(int rotation, int ringSetting) - { - super("T-II", - new Integer[]{20,15,7,25,11,22,4,16,12,19,3,9,23,2,0,10,18,14,8,6,21,1,24,5,13,17}, - new Integer[]{14,21,13,10,6,23,19,2,18,11,15,4,8,24,17,1,7,25,16,9,0,20,5,12,22,3}, - new Integer[]{23,0,6,12,18}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type T Tirpitz - * Q U D L Y R F E K O N V Z A X W H M G P J B S I C T - * Turnover X A F L R - */ - private static class Rotor_T_III extends Rotor - { - public Rotor_T_III(int rotation, int ringSetting) { - super("T-III", - new Integer[]{16,20,3,11,24,17,5,4,10,14,13,21,25,0,23,22,7,12,6,15,9,1,18,8,2,19}, - new Integer[]{13,21,24,2,7,6,18,16,23,20,8,3,17,10,9,19,0,5,22,25,1,11,15,14,4,12}, - new Integer[]{23,0,5,11,17}, ringSetting, rotation); - } - } - - /** - * Rotor IV as used in the Enigma Type T Tirpitz - * C I W T B K X N R E S P F L Y D A G V H Q U O J Z M - * Turnover X A G M S - */ - private static class Rotor_T_IV extends Rotor - { - public Rotor_T_IV(int rotation, int ringSetting) - { - super("T-IV", - new Integer[]{2,8,22,19,1,10,23,13,17,4,18,15,5,11,24,3,0,6,21,7,16,20,14,9,25,12}, - new Integer[]{16,4,0,15,9,12,17,19,1,23,5,13,25,7,22,11,20,8,10,3,21,18,2,6,14,24}, - new Integer[]{23,0,6,12,18}, ringSetting, rotation); - } - } - - /** - * Rotor V as used in the Enigma Type T Tirpitz - * U A X G I S N J B V E R D Y L F Z W T P C K O H M Q - * Turnover Z D G L S - */ - private static class Rotor_T_V extends Rotor - { - public Rotor_T_V(int rotation, int ringSetting) - { - super("T-V", - new Integer[]{20,0,23,6,8,18,13,9,1,21,4,17,3,24,11,5,25,22,19,15,2,10,14,7,12,16}, - new Integer[]{1,8,20,12,10,15,3,23,4,7,21,14,24,6,22,19,25,11,5,18,0,9,17,2,13,16}, - new Integer[]{25,3,6,11,18}, ringSetting, rotation); - } - } - - /** - * Rotor VI as used in the Enigma Type T Tirpitz - * X F U Z G A L V H C N Y S E W Q T D M R B K P I O J - * Turnover Y F J N R - */ - private static class Rotor_T_VI extends Rotor - { - public Rotor_T_VI(int rotation, int ringSetting) - { - super("T-VI", - new Integer[]{23,5,20,25,6,0,11,21,7,2,13,24,18,4,22,16,19,3,12,17,1,10,15,8,14,9}, - new Integer[]{5,20,9,17,13,1,4,8,23,25,21,6,18,10,24,22,15,19,12,16,2,7,14,0,11,3}, - new Integer[]{24,5,9,13,17}, ringSetting, rotation); - } - } - - /** - * Rotor VII as used in the Enigma Type T Tirpitz - * B J V F T X P L N A Y O Z I K W G D Q E R U C H S M - * Turnover Z D G L S - */ - private static class Rotor_T_VII extends Rotor - { - public Rotor_T_VII(int rotation, int ringSetting) - { - super("T-VII", - new Integer[]{1,9,21,5,19,23,15,11,13,0,24,14,25,8,10,22,6,3,16,4,17,20,2,7,18,12}, - new Integer[]{9,0,22,17,19,3,16,23,13,1,14,7,25,8,11,6,18,20,24,4,21,2,15,5,10,12}, - new Integer[]{25,3,6,11,18}, ringSetting, rotation); - } - } - - /** - * Rotor VIII as used in the Enigma Type T Tirpitz - * Y M T P N Z H W K O D A J X E L U Q V G C B I S F R - * Turnover Y F J N R - */ - private static class Rotor_T_VIII extends Rotor - { - public Rotor_T_VIII(int rotation, int ringSetting) - { - super("T-VIII", - new Integer[]{24,12,19,15,13,25,7,22,10,14,3,0,9,23,4,11,20,16,21,6,2,1,8,18,5,17}, - new Integer[]{11,21,20,10,14,24,19,6,22,12,8,15,1,4,9,3,17,25,23,2,16,18,7,13,0,5}, - new Integer[]{24,5,9,13,17}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type G312 Abwehr - * D M T W S I L R U Y Q N K F E J C A Z B P G X O H V - * Turnover T V W X A B C D F G H J L M P Q R - */ - private static class Rotor_G312_I extends Rotor - { - public Rotor_G312_I(int rotation, int ringSetting) - { - super("G312-I", - new Integer[]{3,12,19,22,18,8,11,17,20,24,16,13,10,5,4,9,2,0,25,1,15,6,23,14,7,21}, - new Integer[]{17,19,16,0,14,13,21,24,5,15,12,6,1,11,23,20,10,7,4,2,8,25,3,22,9,18}, - new Integer[]{19,21,22,23,0,1,2,3,5,6,7,9,11,12,15,16,17}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type G312 Abwehr - * H Q Z G P J T M O B L N C I F D Y A W V E U S R K X - * Turnover T U W Z A B D E G H I L N O R - */ - private static class Rotor_G312_II extends Rotor - { - public Rotor_G312_II(int rotation, int ringSetting) - { - super("G312-II", - new Integer[]{7,16,25,6,15,9,19,12,14,1,11,13,2,8,5,3,24,0,22,21,4,20,18,17,10,23}, - new Integer[]{17,9,12,15,20,14,3,0,13,5,24,10,7,11,8,4,1,23,22,6,21,19,18,25,16,2}, - new Integer[]{19,20,22,25,0,1,3,4,6,7,8,11,13,14,17}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type G312 Abwehr - * U Q N T L S Z F M R E H D P X K I B V Y G J C W O A - * Turnover V X Y B F G I L N O S - */ - private static class Rotor_G312_III extends Rotor - { - public Rotor_G312_III(int rotation, int ringSetting) - { - super("G312-III", - new Integer[]{20,16,13,19,11,18,25,5,12,17,4,7,3,15,23,10,8,1,21,24,6,9,2,22,14,0}, - new Integer[]{25,17,22,12,10,7,20,11,16,21,15,4,8,2,24,13,1,9,5,3,0,18,23,14,19,6}, - new Integer[]{21,23,24,1,5,6,8,11,13,14,18}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type G260 Abwehr - * R C S P B L K Q A U M H W Y T I F Z V G O J N E X D - * Turnover T V W X A B C D F G H J L M P Q R - */ - private static class Rotor_G260_I extends Rotor - { - public Rotor_G260_I(int rotation, int ringSetting) - { - super("G260-I", - new Integer[]{17,2,18,15,1,11,10,16,0,20,12,7,22,24,19,8,5,25,21,6,14,9,13,4,23,3}, - new Integer[]{8,4,1,25,23,16,19,11,15,21,6,5,10,22,20,3,7,0,2,14,9,18,12,24,13,17}, - new Integer[]{19,21,22,23,0,1,2,3,5,6,7,9,11,12,15,16,17}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type G260 Abwehr - * W C M I B V P J X A R O S G N D L Z K E Y H U F Q T - * Turnover T U W Z A B D E G H I L N O R - */ - private static class Rotor_G260_II extends Rotor - { - public Rotor_G260_II(int rotation, int ringSetting) - { - super("G260-II", - new Integer[]{22,2,12,8,1,21,15,9,23,0,17,14,18,6,13,3,11,25,10,4,24,7,20,5,16,19}, - new Integer[]{9,4,1,15,19,23,13,21,3,7,18,16,2,14,11,6,24,10,12,25,22,5,0,8,20,17}, - new Integer[]{19,20,22,25,0,1,3,4,6,7,8,11,13,14,17}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type G260 Abwehr - * F V D H Z E L S Q M A X O K Y I W P G C B U J T N R - * Turnover V X Y B F G I L N O S - */ - private static class Rotor_G260_III extends Rotor - { - public Rotor_G260_III(int rotation, int ringSetting) - { - super("G260-III", - new Integer[]{5,21,3,7,25,4,11,18,16,12,0,23,14,10,24,8,22,15,6,2,1,20,9,19,13,17}, - new Integer[]{10,20,19,2,5,0,18,3,15,22,13,6,9,24,12,17,8,25,7,23,21,1,16,11,14,4}, - new Integer[]{21,23,24,1,5,6,8,11,13,14,18}, ringSetting, rotation); - } - } - /** * Rotor I as used in the Enigma Type G31 Abwehr * L P G S Z M H A E O Q K V X R F Y B U T N I C J D W * Turnover T V W X A B C D F G H J L M P Q R */ - private static class Rotor_G31_I extends Rotor + public static class Rotor_G31_I extends Rotor { public Rotor_G31_I(int rotation, int ringSetting) { - super("G31-I", + super(20, "G31-I", "LPGSZMHAEOQKVXRFYBUTNICJDW", new Integer[]{11,15,6,18,25,12,7,0,4,14,16,10,21,23,17,5,24,1,20,19,13,8,2,9,3,22}, new Integer[]{7,17,22,24,8,15,2,6,21,23,11,0,5,20,9,1,10,14,3,19,18,12,25,13,16,4}, new Integer[]{19,21,22,23,0,1,2,3,5,6,7,9,11,12,15,16,17}, ringSetting, rotation); @@ -885,11 +536,11 @@ public class Rotor * S L V G B T F X J Q O H E W I R Z Y A M K P C N D U * Turnover T U W Z A B D E G H I L N O R */ - private static class Rotor_G31_II extends Rotor + public static class Rotor_G31_II extends Rotor { public Rotor_G31_II(int rotation, int ringSetting) { - super("G31_II", + super(21, "G31_II", "SLVGBTFXJQOHEWIRZYAMKPCNDU", new Integer[]{18,11,21,6,1,19,5,23,9,16,14,7,4,22,8,17,25,24,0,12,10,15,2,13,3,20}, new Integer[]{18,4,22,24,12,6,3,11,14,8,20,1,19,23,10,21,9,15,0,5,25,2,13,7,17,16}, new Integer[]{19,20,22,25,0,1,3,4,6,7,8,11,13,14,17}, ringSetting, rotation); @@ -901,11 +552,11 @@ public class Rotor * C J G D P S H K T U R A W Z X F M Y N Q O B V L I E * Turnover V X Y B F G I L N O S */ - private static class Rotor_G31_III extends Rotor + public static class Rotor_G31_III extends Rotor { public Rotor_G31_III(int rotation, int ringSetting) { - super("G31_III", + super(22, "G31_III", "CJGDPSHKTURAWZXFMYNQOBVLIE", new Integer[]{2,9,6,3,15,18,7,10,19,20,17,0,22,25,23,5,12,24,13,16,14,1,21,11,8,4}, new Integer[]{11,21,0,3,25,15,2,6,24,1,7,23,16,18,20,4,19,10,5,8,9,22,12,14,17,13}, new Integer[]{21,23,24,1,5,6,8,11,13,14,18}, ringSetting, rotation); @@ -913,15 +564,111 @@ public class Rotor } /** - * Rotor I as used in the Enigma Type K + * Rotor I as used in the Enigma Type G312 Abwehr + * D M T W S I L R U Y Q N K F E J C A Z B P G X O H V + * Turnover T V W X A B C D F G H J L M P Q R + */ + public static class Rotor_G312_I extends Rotor + { + public Rotor_G312_I(int rotation, int ringSetting) + { + super(30, "G312-I", "DMTWSILRUYQNKFEJCAZBPGXOHV", + new Integer[]{3,12,19,22,18,8,11,17,20,24,16,13,10,5,4,9,2,0,25,1,15,6,23,14,7,21}, + new Integer[]{17,19,16,0,14,13,21,24,5,15,12,6,1,11,23,20,10,7,4,2,8,25,3,22,9,18}, + new Integer[]{19,21,22,23,0,1,2,3,5,6,7,9,11,12,15,16,17}, ringSetting, rotation); + } + } + + /** + * Rotor II as used in the Enigma Type G312 Abwehr + * H Q Z G P J T M O B L N C I F D Y A W V E U S R K X + * Turnover T U W Z A B D E G H I L N O R + */ + public static class Rotor_G312_II extends Rotor + { + public Rotor_G312_II(int rotation, int ringSetting) + { + super(31, "G312-II", "HQZGPJTMOBLNCIFDYAWVEUSRKX", + new Integer[]{7,16,25,6,15,9,19,12,14,1,11,13,2,8,5,3,24,0,22,21,4,20,18,17,10,23}, + new Integer[]{17,9,12,15,20,14,3,0,13,5,24,10,7,11,8,4,1,23,22,6,21,19,18,25,16,2}, + new Integer[]{19,20,22,25,0,1,3,4,6,7,8,11,13,14,17}, ringSetting, rotation); + } + } + + /** + * Rotor III as used in the Enigma Type G312 Abwehr + * U Q N T L S Z F M R E H D P X K I B V Y G J C W O A + * Turnover V X Y B F G I L N O S + */ + public static class Rotor_G312_III extends Rotor + { + public Rotor_G312_III(int rotation, int ringSetting) + { + super(32, "G312-III", "UQNTLSZFMREHDPXKIBVYGJCWOA", + new Integer[]{20,16,13,19,11,18,25,5,12,17,4,7,3,15,23,10,8,1,21,24,6,9,2,22,14,0}, + new Integer[]{25,17,22,12,10,7,20,11,16,21,15,4,8,2,24,13,1,9,5,3,0,18,23,14,19,6}, + new Integer[]{21,23,24,1,5,6,8,11,13,14,18}, ringSetting, rotation); + } + } + + /** + * Rotor I as used in the Enigma Type G260 Abwehr + * R C S P B L K Q A U M H W Y T I F Z V G O J N E X D + * Turnover T V W X A B C D F G H J L M P Q R + */ + public static class Rotor_G260_I extends Rotor + { + public Rotor_G260_I(int rotation, int ringSetting) + { + super(40, "G260-I", "RCSPBLKQAUMHWYTIFZVGOJNEXD", + new Integer[]{17,2,18,15,1,11,10,16,0,20,12,7,22,24,19,8,5,25,21,6,14,9,13,4,23,3}, + new Integer[]{8,4,1,25,23,16,19,11,15,21,6,5,10,22,20,3,7,0,2,14,9,18,12,24,13,17}, + new Integer[]{19,21,22,23,0,1,2,3,5,6,7,9,11,12,15,16,17}, ringSetting, rotation); + } + } + + /** + * Rotor II as used in the Enigma Type G260 Abwehr + * W C M I B V P J X A R O S G N D L Z K E Y H U F Q T + * Turnover T U W Z A B D E G H I L N O R + */ + public static class Rotor_G260_II extends Rotor + { + public Rotor_G260_II(int rotation, int ringSetting) + { + super(41, "G260-II", "WCMIBVPJXAROSGNDLZKEYHUFQT", + new Integer[]{22,2,12,8,1,21,15,9,23,0,17,14,18,6,13,3,11,25,10,4,24,7,20,5,16,19}, + new Integer[]{9,4,1,15,19,23,13,21,3,7,18,16,2,14,11,6,24,10,12,25,22,5,0,8,20,17}, + new Integer[]{19,20,22,25,0,1,3,4,6,7,8,11,13,14,17}, ringSetting, rotation); + } + } + + /** + * Rotor III as used in the Enigma Type G260 Abwehr + * F V D H Z E L S Q M A X O K Y I W P G C B U J T N R + * Turnover V X Y B F G I L N O S + */ + public static class Rotor_G260_III extends Rotor + { + public Rotor_G260_III(int rotation, int ringSetting) + { + super(42, "G260-III", "FVDHZELSQMAXOKYIWPGCBUJTNR", + new Integer[]{5,21,3,7,25,4,11,18,16,12,0,23,14,10,24,8,22,15,6,2,1,20,9,19,13,17}, + new Integer[]{10,20,19,2,5,0,18,3,15,22,13,6,9,24,12,17,8,25,7,23,21,1,16,11,14,4}, + new Integer[]{21,23,24,1,5,6,8,11,13,14,18}, ringSetting, rotation); + } + } + + /** + * Rotor I as used in the Enigma Type K,D * L P G S Z M H A E O Q K V X R F Y B U T N I C J D W * Turnover Z */ - private static class Rotor_K_I extends Rotor + public static class Rotor_K_D_I extends Rotor { - public Rotor_K_I(int rotation, int ringSetting) + public Rotor_K_D_I(int rotation, int ringSetting) { - super("K-I", + super(50, "K/D-I", "LPGSZMHAEOQKVXRFYBUTNICJDW", new Integer[]{11,15,6,18,25,12,7,0,4,14,16,10,21,23,17,5,24,1,20,19,13,8,2,9,3,22}, new Integer[]{7,17,22,24,8,15,2,6,21,23,11,0,5,20,9,1,10,14,3,19,18,12,25,13,16,4}, new Integer[]{25}, ringSetting, rotation); @@ -929,15 +676,15 @@ public class Rotor } /** - * Rotor II as used in the Enigma type K + * Rotor II as used in the Enigma Type K,D * S L V G B T F X J Q O H E W I R Z Y A M K P C N D U * Turnover F */ - private static class Rotor_K_II extends Rotor + public static class Rotor_K_D_II extends Rotor { - public Rotor_K_II(int rotation, int ringSetting) + public Rotor_K_D_II(int rotation, int ringSetting) { - super("K-II", + super(51, "K/D-II", "SLVGBTFXJQOHEWIRZYAMKPCNDU", new Integer[]{18,11,21,6,1,19,5,23,9,16,14,7,4,22,8,17,25,24,0,12,10,15,2,13,3,20}, new Integer[]{18,4,22,24,12,6,3,11,14,8,20,1,19,23,10,21,9,15,0,5,25,2,13,7,17,16}, new Integer[]{5}, ringSetting, rotation); @@ -945,31 +692,79 @@ public class Rotor } /** - * Rotor III as used in the Enigma Type K + * Rotor III as used in the Enigma Type K,D * C J G D P S H K T U R A W Z X F M Y N Q O B V L I E * Turnover O */ - private static class Rotor_K_III extends Rotor + public static class Rotor_K_D_III extends Rotor { - public Rotor_K_III(int rotation, int ringSetting) + public Rotor_K_D_III(int rotation, int ringSetting) { - super("K-III", + super(52, "K/D-III", "CJGDPSHKTURAWZXFMYNQOBVLIE", new Integer[]{2,9,6,3,15,18,7,10,19,20,17,0,22,25,23,5,12,24,13,16,14,1,21,11,8,4}, new Integer[]{11,21,0,3,25,15,2,6,24,1,7,23,16,18,20,4,19,10,5,8,9,22,12,14,17,13}, new Integer[]{14}, ringSetting, rotation); } } + /** + * Rotor I as used in the Enigma Type K (Switzerland) + * P E Z U O H X S C V F M T B G L R I N Q J W A Y D K + * Turnover Z + */ + public static class Rotor_KSwiss_Standard_I extends Rotor + { + public Rotor_KSwiss_Standard_I(int rotation, int ringSetting) + { + super(60, "KS-I", "PEZUOHXSCVFMTBGLRINQJWAYDK", + new Integer[]{15,4,25,20,14,7,23,18,2,21,5,12,19,1,6,11,17,8,13,16,9,22,0,24,3,10}, + new Integer[]{22,13,8,24,1,10,14,5,17,20,25,15,11,18,4,0,19,16,7,12,3,9,21,6,23,2}, + new Integer[]{25}, ringSetting, rotation); + } + } + + /** + * Rotor II as used in the Enigma Type K (Switzerland) + * Z O U E S Y D K F W P C I Q X H M V B L G N J R A T + * Turnover F + */ + public static class Rotor_KSwiss_Standard_II extends Rotor + { + public Rotor_KSwiss_Standard_II(int rotation, int ringSetting) + { + super(61, "KS-II", "ZOUESYDKFWPCIQXHMVBLGNJRAT", + new Integer[]{25,14,20,4,18,24,3,10,5,22,15,2,8,16,23,7,12,21,1,11,6,13,9,17,0,19}, + new Integer[]{24,18,11,6,3,8,20,15,12,22,7,19,16,21,1,10,13,23,4,25,2,17,9,14,5,0}, + new Integer[]{5}, ringSetting, rotation); + } + } + + /** + * Rotor III as used in the Enigma Type K (Switzerland) + * E H R V X G A O B Q U S I M Z F L Y N W K T P D J C + * Turnover O + */ + public static class Rotor_KSwiss_Standard_III extends Rotor + { + public Rotor_KSwiss_Standard_III(int rotation, int ringSetting) + { + super(62, "KS-III", "EHRVXGAOBQUSIMZFLYNWKTPDJC", + new Integer[]{4,7,17,21,23,6,0,14,1,16,20,18,8,12,25,5,11,24,13,22,10,19,15,3,9,2}, + new Integer[]{6,8,25,23,0,15,5,1,12,24,20,16,13,18,7,22,9,2,11,21,10,3,19,4,17,14}, + new Integer[]{14}, ringSetting, rotation); + } + } + /** * Rotor I as used in the Enigma Type K (Swiss, Airforce) * PEZUOHXSCVFMTBGLRINQJWAYDK * Turnover Z */ - private static class Rotor_K_Swiss_Airforce_I extends Rotor + public static class Rotor_K_Swiss_Airforce_I extends Rotor { public Rotor_K_Swiss_Airforce_I(int rotation, int ringSetting) { - super("KSA-I", + super(70, "KSA-I", "PEZUOHXSCVFMTBGLRINQJWAYDK", new Integer[]{15,4,25,20,14,7,23,18,2,21,5,12,19,1,6,11,17,8,13,16,9,22,0,24,3,10}, new Integer[]{22,13,8,24,1,10,14,5,17,20,25,15,11,18,4,0,19,16,7,12,3,9,21,6,23,2}, new Integer[]{25}, ringSetting, rotation); @@ -980,11 +775,11 @@ public class Rotor * ZOUESYDKFWPCIQXHMVBLGNJRAT * Turnover F */ - private static class Rotor_K_Swiss_Airforce_II extends Rotor + public static class Rotor_K_Swiss_Airforce_II extends Rotor { public Rotor_K_Swiss_Airforce_II(int rotation, int ringSetting) { - super("KSA-II", + super(71, "KSA-II", "ZOUESYDKFWPCIQXHMVBLGNJRAT", new Integer[]{25,14,20,4,18,24,3,10,5,22,15,2,8,16,23,7,12,21,1,11,6,13,9,17,0,19}, new Integer[]{24,18,11,6,3,8,20,15,12,22,7,19,16,21,1,10,13,23,4,25,2,17,9,14,5,0}, new Integer[]{5}, ringSetting, rotation); @@ -995,11 +790,11 @@ public class Rotor * EHRVXGAOBQUSIMZFLYNWKTPDJC * Turnover O */ - private static class Rotor_K_Swiss_Airforce_III extends Rotor + public static class Rotor_K_Swiss_Airforce_III extends Rotor { public Rotor_K_Swiss_Airforce_III(int rotation, int ringSetting) { - super("KSA-III", + super(72, "KSA-III", "EHRVXGAOBQUSIMZFLYNWKTPDJC", new Integer[]{4,7,17,21,23,6,0,14,1,16,20,18,8,12,25,5,11,24,13,22,10,19,15,3,9,2}, new Integer[]{6,8,25,23,0,15,5,1,12,24,20,16,13,18,7,22,9,2,11,21,10,3,19,4,17,14}, new Integer[]{14}, ringSetting, rotation); @@ -1011,11 +806,11 @@ public class Rotor * JGDQOXUSCAMIFRVTPNEWKBLZYH * Turnover O */ - private static class Rotor_R_I extends Rotor + public static class Rotor_R_I extends Rotor { public Rotor_R_I(int rotation, int ringSetting) { - super("R-I", + super(80, "R-I", "JGDQOXUSCAMIFRVTPNEWKBLZYH", new Integer[]{9,6,3,16,14,23,20,18,2,0,12,8,5,17,21,19,15,13,4,22,10,1,11,25,24,7}, new Integer[]{9,21,8,2,18,12,1,25,11,0,20,22,10,17,4,16,3,13,7,15,6,14,19,5,24,23}, new Integer[]{14}, ringSetting, rotation); @@ -1027,11 +822,11 @@ public class Rotor * NTZPSFBOKMWRCJDIVLAEYUXHGQ * Turnover F */ - private static class Rotor_R_II extends Rotor + public static class Rotor_R_II extends Rotor { public Rotor_R_II(int rotation, int ringSetting) { - super("R-II", + super(81, "R-II", "NTZPSFBOKMWRCJDIVLAEYUXHGQ", new Integer[]{13,19,25,15,18,5,1,14,10,12,22,17,2,9,3,8,21,11,0,4,24,20,23,7,6,16}, new Integer[]{18,6,12,14,19,5,24,23,15,13,8,17,9,0,7,3,25,11,4,1,21,16,10,22,20,2}, new Integer[]{5}, ringSetting, rotation); @@ -1043,14 +838,141 @@ public class Rotor * JVIUBHTCDYAKEQZPOSGXNRMWFL * Turnover Z */ - private static class Rotor_R_III extends Rotor + public static class Rotor_R_III extends Rotor { public Rotor_R_III(int rotation, int ringSetting) { - super("R-III", + super(82, "R-III", "JVIUBHTCDYAKEQZPOSGXNRMWFL", new Integer[]{9,21,8,20,1,7,19,2,3,24,0,10,4,16,25,15,14,18,6,23,13,17,12,22,5,11}, new Integer[]{10,4,7,8,12,24,18,5,2,0,11,25,22,20,16,15,13,21,17,6,3,1,23,19,9,14}, new Integer[]{25}, ringSetting, rotation); } } -} + + /** + * Rotor I as used in the Enigma Type T Tirpitz + * K P T Y U E L O C V G R F Q D A N J M B S W H Z X I + * Turnover X A F L R + */ + public static class Rotor_T_I extends Rotor + { + public Rotor_T_I(int rotation, int ringSetting) + { + super(90, "T-I", "KPTYUELOCVGRFQDANJMBSWHZXI", + new Integer[]{10,15,19,24,20,4,11,14,2,21,6,17,5,16,3,0,13,9,12,1,18,22,7,25,23,8}, + new Integer[]{15,19,8,14,5,12,10,22,25,17,0,6,18,16,7,1,13,11,20,2,4,9,21,24,3,23}, + new Integer[]{23,0,5,11,17}, ringSetting, rotation); + } + } + + /** + * Rotor II as used in the Enigma Type T Tirpitz + * U P H Z L W E Q M T D J X C A K S O I G V B Y F N R + * Turnover X A G M S + */ + public static class Rotor_T_II extends Rotor + { + public Rotor_T_II(int rotation, int ringSetting) + { + super(91, "T-II", "UPHZLWEQMTDJXCAKSOIGVBYFNR", + new Integer[]{20,15,7,25,11,22,4,16,12,19,3,9,23,2,0,10,18,14,8,6,21,1,24,5,13,17}, + new Integer[]{14,21,13,10,6,23,19,2,18,11,15,4,8,24,17,1,7,25,16,9,0,20,5,12,22,3}, + new Integer[]{23,0,6,12,18}, ringSetting, rotation); + } + } + + /** + * Rotor III as used in the Enigma Type T Tirpitz + * Q U D L Y R F E K O N V Z A X W H M G P J B S I C T + * Turnover X A F L R + */ + public static class Rotor_T_III extends Rotor + { + public Rotor_T_III(int rotation, int ringSetting) { + super(92, "T-III", "QUDLYRFEKONVZAXWHMGPJBSICT", + new Integer[]{16,20,3,11,24,17,5,4,10,14,13,21,25,0,23,22,7,12,6,15,9,1,18,8,2,19}, + new Integer[]{13,21,24,2,7,6,18,16,23,20,8,3,17,10,9,19,0,5,22,25,1,11,15,14,4,12}, + new Integer[]{23,0,5,11,17}, ringSetting, rotation); + } + } + + /** + * Rotor IV as used in the Enigma Type T Tirpitz + * C I W T B K X N R E S P F L Y D A G V H Q U O J Z M + * Turnover X A G M S + */ + public static class Rotor_T_IV extends Rotor + { + public Rotor_T_IV(int rotation, int ringSetting) + { + super(93, "T-IV", "CIWTBKXNRESPFLYDAGVHQUOJZM", + new Integer[]{2,8,22,19,1,10,23,13,17,4,18,15,5,11,24,3,0,6,21,7,16,20,14,9,25,12}, + new Integer[]{16,4,0,15,9,12,17,19,1,23,5,13,25,7,22,11,20,8,10,3,21,18,2,6,14,24}, + new Integer[]{23,0,6,12,18}, ringSetting, rotation); + } + } + + /** + * Rotor V as used in the Enigma Type T Tirpitz + * U A X G I S N J B V E R D Y L F Z W T P C K O H M Q + * Turnover Z D G L S + */ + public static class Rotor_T_V extends Rotor + { + public Rotor_T_V(int rotation, int ringSetting) + { + super(94, "T-V", "UAXGISNJBVERDYLFZWTPCKOHMQ", + new Integer[]{20,0,23,6,8,18,13,9,1,21,4,17,3,24,11,5,25,22,19,15,2,10,14,7,12,16}, + new Integer[]{1,8,20,12,10,15,3,23,4,7,21,14,24,6,22,19,25,11,5,18,0,9,17,2,13,16}, + new Integer[]{25,3,6,11,18}, ringSetting, rotation); + } + } + + /** + * Rotor VI as used in the Enigma Type T Tirpitz + * X F U Z G A L V H C N Y S E W Q T D M R B K P I O J + * Turnover Y F J N R + */ + public static class Rotor_T_VI extends Rotor + { + public Rotor_T_VI(int rotation, int ringSetting) + { + super(95, "T-VI", "XFUZGALVHCNYSEWQTDMRBKPIOJ", + new Integer[]{23,5,20,25,6,0,11,21,7,2,13,24,18,4,22,16,19,3,12,17,1,10,15,8,14,9}, + new Integer[]{5,20,9,17,13,1,4,8,23,25,21,6,18,10,24,22,15,19,12,16,2,7,14,0,11,3}, + new Integer[]{24,5,9,13,17}, ringSetting, rotation); + } + } + + /** + * Rotor VII as used in the Enigma Type T Tirpitz + * B J V F T X P L N A Y O Z I K W G D Q E R U C H S M + * Turnover Z D G L S + */ + public static class Rotor_T_VII extends Rotor + { + public Rotor_T_VII(int rotation, int ringSetting) + { + super(96, "T-VII", "BJVFTXPLNAYOZIKWGDQERUCHSM", + new Integer[]{1,9,21,5,19,23,15,11,13,0,24,14,25,8,10,22,6,3,16,4,17,20,2,7,18,12}, + new Integer[]{9,0,22,17,19,3,16,23,13,1,14,7,25,8,11,6,18,20,24,4,21,2,15,5,10,12}, + new Integer[]{25,3,6,11,18}, ringSetting, rotation); + } + } + + /** + * Rotor VIII as used in the Enigma Type T Tirpitz + * Y M T P N Z H W K O D A J X E L U Q V G C B I S F R + * Turnover Y F J N R + */ + public static class Rotor_T_VIII extends Rotor + { + public Rotor_T_VIII(int rotation, int ringSetting) + { + super(97, "T-VIII", "YMTPNZHWKODAJXELUQVGCBISFR", + new Integer[]{24,12,19,15,13,25,7,22,10,14,3,0,9,23,4,11,20,16,21,6,2,1,8,18,5,17}, + new Integer[]{11,21,20,10,14,24,19,6,22,12,8,15,1,4,9,3,17,25,23,2,16,18,7,13,0,5}, + new Integer[]{24,5,9,13,17}, ringSetting, rotation); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java index ca3ac01..8f51cda 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java @@ -1,7 +1,6 @@ package de.vanitasvitae.enigmandroid.layout; import android.view.View; -import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java index f1930a1..0497e3f 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java @@ -1,11 +1,5 @@ package de.vanitasvitae.enigmandroid.layout; -import android.widget.ArrayAdapter; -import android.widget.Spinner; - -import de.vanitasvitae.enigmandroid.R; -import de.vanitasvitae.enigmandroid.enigma.Enigma; -import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_G260; /** @@ -30,11 +24,9 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_G260; */ public class LayoutContainer_G260 extends LayoutContainer_G31 { - protected static int offsetRot = 60; - public LayoutContainer_G260() { - super(60); + super(); main.setTitle("G260 - EnigmAndroid"); this.resetLayout(); } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java index 7550b21..1e82510 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java @@ -1,6 +1,5 @@ package de.vanitasvitae.enigmandroid.layout; -import android.widget.ArrayAdapter; import android.widget.Spinner; import de.vanitasvitae.enigmandroid.R; @@ -32,8 +31,6 @@ public class LayoutContainer_G31 extends LayoutContainer { protected Enigma enigma; - protected int offsetRot = 40; - protected Spinner rotor1View; protected Spinner rotor2View; protected Spinner rotor3View; @@ -43,12 +40,6 @@ public class LayoutContainer_G31 extends LayoutContainer protected Spinner rotor3PositionView; protected Spinner reflectorPositionView; - public LayoutContainer_G31(int off) - { - super(); - this.offsetRot = off; - } - public LayoutContainer_G31() { super(); @@ -94,9 +85,9 @@ public class LayoutContainer_G31 extends LayoutContainer @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - offsetRot); - this.rotor2View.setSelection(state.getTypeRotor2() - offsetRot); - this.rotor3View.setSelection(state.getTypeRotor3() - offsetRot); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -107,9 +98,9 @@ public class LayoutContainer_G31 extends LayoutContainer public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + offsetRot); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + offsetRot); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + offsetRot); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G312.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G312.java index e0d807f..2e29560 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G312.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G312.java @@ -1,11 +1,5 @@ package de.vanitasvitae.enigmandroid.layout; -import android.widget.ArrayAdapter; -import android.widget.Spinner; - -import de.vanitasvitae.enigmandroid.R; -import de.vanitasvitae.enigmandroid.enigma.Enigma; -import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_G312; /** @@ -32,7 +26,7 @@ public class LayoutContainer_G312 extends LayoutContainer_G31 { public LayoutContainer_G312() { - super(50); + super(); main.setTitle("G312 - EnigmAndroid"); this.resetLayout(); } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java index c94582d..2c6bade 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java @@ -1,7 +1,6 @@ package de.vanitasvitae.enigmandroid.layout; import android.view.View; -import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; @@ -90,10 +89,10 @@ public class LayoutContainer_I extends LayoutContainer @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - 10); - this.rotor2View.setSelection(state.getTypeRotor2() - 10); - this.rotor3View.setSelection(state.getTypeRotor3() - 10); - this.reflectorView.setSelection(state.getTypeReflector() - 10); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); + this.reflectorView.setSelection(state.getTypeReflector()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -103,10 +102,10 @@ public class LayoutContainer_I extends LayoutContainer public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 10); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 10); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 10); - state.setTypeReflector(reflectorView.getSelectedItemPosition() + 10); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); + state.setTypeReflector(reflectorView.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java index a0fc8b1..2b52ce5 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java @@ -1,13 +1,11 @@ package de.vanitasvitae.enigmandroid.layout; -import android.widget.ArrayAdapter; import android.widget.Spinner; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_K; -import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Standard; /** * LayoutContainer for the Enigma Model K @@ -88,9 +86,9 @@ public class LayoutContainer_K extends LayoutContainer @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - 80); - this.rotor2View.setSelection(state.getTypeRotor2() - 80); - this.rotor3View.setSelection(state.getTypeRotor3() - 80); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -101,9 +99,9 @@ public class LayoutContainer_K extends LayoutContainer public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 80); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 80); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 80); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss.java index dd8a38c..6a0274d 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss.java @@ -1,12 +1,10 @@ package de.vanitasvitae.enigmandroid.layout; -import android.widget.ArrayAdapter; import android.widget.Spinner; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; -import de.vanitasvitae.enigmandroid.enigma.Enigma_K; import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Standard; /** @@ -88,9 +86,9 @@ public class LayoutContainer_K_Swiss extends LayoutContainer @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - 90); - this.rotor2View.setSelection(state.getTypeRotor2() - 90); - this.rotor3View.setSelection(state.getTypeRotor3() - 90); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -101,9 +99,9 @@ public class LayoutContainer_K_Swiss extends LayoutContainer public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 90); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 90); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 90); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java index b30b0c2..fc4826e 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java @@ -1,12 +1,10 @@ package de.vanitasvitae.enigmandroid.layout; -import android.widget.ArrayAdapter; import android.widget.Spinner; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; -import de.vanitasvitae.enigmandroid.enigma.Enigma_K; import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Airforce; /** @@ -88,9 +86,9 @@ public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - 100); - this.rotor2View.setSelection(state.getTypeRotor2() - 100); - this.rotor3View.setSelection(state.getTypeRotor3() - 100); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -101,9 +99,9 @@ public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 100); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 100); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 100); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java index 01c383c..8c9be62 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java @@ -1,7 +1,6 @@ package de.vanitasvitae.enigmandroid.layout; import android.view.View; -import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; @@ -82,10 +81,10 @@ public class LayoutContainer_M3 extends LayoutContainer_I @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - 20); - this.rotor2View.setSelection(state.getTypeRotor2() - 20); - this.rotor3View.setSelection(state.getTypeRotor3() - 20); - this.reflectorView.setSelection(state.getTypeReflector() - 20); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); + this.reflectorView.setSelection(state.getTypeReflector()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -95,10 +94,10 @@ public class LayoutContainer_M3 extends LayoutContainer_I public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 20); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 20); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 20); - state.setTypeReflector(reflectorView.getSelectedItemPosition() + 20); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); + state.setTypeReflector(reflectorView.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java index a3d519d..196f9e9 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java @@ -1,7 +1,6 @@ package de.vanitasvitae.enigmandroid.layout; import android.view.View; -import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; @@ -100,11 +99,11 @@ public class LayoutContainer_M4 extends LayoutContainer @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - 30); - this.rotor2View.setSelection(state.getTypeRotor2() - 30); - this.rotor3View.setSelection(state.getTypeRotor3() - 30); - this.rotor4View.setSelection(state.getTypeRotor4() - 38); - this.reflectorView.setSelection(state.getTypeReflector() - 30); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); + this.rotor4View.setSelection(state.getTypeRotor4()); + this.reflectorView.setSelection(state.getTypeReflector()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -114,11 +113,11 @@ public class LayoutContainer_M4 extends LayoutContainer @Override public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 30); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 30); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 30); - state.setTypeRotor4(rotor4View.getSelectedItemPosition() + 38); - state.setTypeReflector(reflectorView.getSelectedItemPosition() + 30); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); + state.setTypeRotor4(rotor4View.getSelectedItemPosition()); + state.setTypeReflector(reflectorView.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java index e759bb6..eaaaf0f 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java @@ -1,6 +1,5 @@ package de.vanitasvitae.enigmandroid.layout; -import android.widget.ArrayAdapter; import android.widget.Spinner; import de.vanitasvitae.enigmandroid.R; @@ -86,9 +85,9 @@ public class LayoutContainer_R extends LayoutContainer @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - 110); - this.rotor2View.setSelection(state.getTypeRotor2() - 110); - this.rotor3View.setSelection(state.getTypeRotor3() - 110); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -99,9 +98,9 @@ public class LayoutContainer_R extends LayoutContainer public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 110); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 110); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 110); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java index 51c35eb..af30bc6 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java @@ -85,9 +85,9 @@ public class LayoutContainer_T extends LayoutContainer @Override public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1() - 120); - this.rotor2View.setSelection(state.getTypeRotor2() - 120); - this.rotor3View.setSelection(state.getTypeRotor3() - 120); + this.rotor1View.setSelection(state.getTypeRotor1()); + this.rotor2View.setSelection(state.getTypeRotor2()); + this.rotor3View.setSelection(state.getTypeRotor3()); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); @@ -99,9 +99,9 @@ public class LayoutContainer_T extends LayoutContainer public void syncStateFromLayoutToEnigma() { EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 120); - state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 120); - state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 120); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java index 8d1d002..301b39e 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java @@ -35,10 +35,10 @@ import de.vanitasvitae.enigmandroid.R; */ public class PassphraseDialogBuilder { - MainActivity main; - View passphraseDialogView; - EditText passphrase; - Button positive; + private MainActivity main; + private View passphraseDialogView; + private EditText passphrase; + private Button positive; public PassphraseDialogBuilder() { main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java index 8cfb9a6..b772ab7 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java @@ -314,7 +314,7 @@ public class PluggableDialogBuilder /** * Set buttons to not connected. That includes changing background to grey, set connection to * itself. - * @param b + * @param b index of button */ private void setButtonFree(int b) { diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/RingSettingsDialogBuilder.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/RingSettingsDialogBuilder.java index 6873c26..a9f7d07 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/RingSettingsDialogBuilder.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/RingSettingsDialogBuilder.java @@ -112,7 +112,8 @@ public abstract class RingSettingsDialogBuilder { if(adapters.length != 3 || rIDs.length != 3) { - Log.d("Enigm|RingSettings", "Length of adapters array or length of rIDs array not equal to 3!"); + Log.e(MainActivity.APP_ID, "RingSettingsDialogBuilderRotRotRot.showDialog():" + + " Length of adapters array or length of rIDs array not equal to 3!"); } final Actions3 action = (Actions3) actions; final MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); @@ -232,7 +233,8 @@ public abstract class RingSettingsDialogBuilder protected void showDialog(final EnigmaStateBundle stateBundle, ArrayAdapter[] adapters, int[] rIDs, Actions actions) { if(adapters.length != 4 || rIDs.length != 4) { - Log.d("Enigm|RingSettings", "Length of adapters array or length of rIDs array not equal to 4!"); + Log.d(MainActivity.APP_ID, "RingSettingsDialogBuilderRotRotRotRef.showDialog():" + + "Length of adapters array or length of rIDs array not equal to 4!"); } final Actions4 action = (Actions4) actions; final MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); diff --git a/app/src/main/res/layout-land/activity_main_g_k_t.xml b/app/src/main/res/layout-land/activity_main_g_k_r_t.xml similarity index 100% rename from app/src/main/res/layout-land/activity_main_g_k_t.xml rename to app/src/main/res/layout-land/activity_main_g_k_r_t.xml diff --git a/app/src/main/res/layout-land/dialog_plugs.xml b/app/src/main/res/layout-land/dialog_plugs.xml index b233d6b..9a915b5 100755 --- a/app/src/main/res/layout-land/dialog_plugs.xml +++ b/app/src/main/res/layout-land/dialog_plugs.xml @@ -1,5 +1,6 @@ + android:background="@drawable/button_grey" + tools:ignore="HardcodedText" />