Browse Source

Major bug fixes and improvements

tags/v0.1.9-09.10.2015-beta
VanitasVitae 4 years ago
parent
commit
992c9a924d
52 changed files with 1626 additions and 1391 deletions
  1. 4
    1
      CHANGELOG.txt
  2. 1
    0
      app/src/main/AndroidManifest.xml
  3. 2
    2
      app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java
  4. 42
    33
      app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java
  5. 101
    38
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java
  6. 53
    50
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java
  7. 21
    10
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java
  8. 54
    59
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java
  9. 22
    10
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java
  10. 68
    51
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java
  11. 66
    73
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java
  12. 28
    21
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java
  13. 28
    21
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java
  14. 34
    22
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java
  15. 101
    66
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java
  16. 75
    74
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java
  17. 59
    59
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java
  18. 46
    10
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java
  19. 6
    6
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java
  20. 1
    2
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/InputPreparer.java
  21. 135
    0
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/EntryWheel.java
  22. 116
    121
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java
  23. 374
    452
      app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java
  24. 0
    1
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java
  25. 1
    9
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java
  26. 6
    15
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java
  27. 1
    7
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G312.java
  28. 8
    9
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java
  29. 6
    8
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java
  30. 6
    8
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss.java
  31. 6
    8
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java
  32. 8
    9
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java
  33. 10
    11
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java
  34. 6
    7
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java
  35. 6
    6
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java
  36. 4
    4
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java
  37. 1
    1
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java
  38. 4
    2
      app/src/main/java/de/vanitasvitae/enigmandroid/layout/RingSettingsDialogBuilder.java
  39. 0
    0
      app/src/main/res/layout-land/activity_main_g_k_r_t.xml
  40. 53
    26
      app/src/main/res/layout-land/dialog_plugs.xml
  41. 0
    1
      app/src/main/res/layout/activity_main_d.xml
  42. 0
    0
      app/src/main/res/layout/activity_main_g_k_r_t.xml
  43. 0
    1
      app/src/main/res/layout/dialog_about.xml
  44. 7
    12
      app/src/main/res/layout/dialog_passphrase.xml
  45. 53
    26
      app/src/main/res/layout/dialog_plugs.xml
  46. 0
    6
      app/src/main/res/values-de/strings.xml
  47. 0
    6
      app/src/main/res/values-de/strings_activity_settings.xml
  48. 2
    2
      app/src/main/res/values/about_dialog_resources.xml
  49. 0
    6
      app/src/main/res/values/strings.xml
  50. 0
    7
      app/src/main/res/values/strings_activity_settings.xml
  51. 0
    11
      app/src/main/res/xml/pref_page.xml
  52. 1
    1
      gradlew

+ 4
- 1
CHANGELOG.txt View File

@@ -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)


+ 1
- 0
app/src/main/AndroidManifest.xml View File

@@ -4,6 +4,7 @@

<application
android:allowBackup="true"
android:fullBackupContent="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

+ 2
- 2
app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java View File

@@ -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
*/

+ 42
- 33
app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java View File

@@ -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) {

+ 101
- 38
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java View File

@@ -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<EntryWheel> availableEntryWheels;
protected ArrayList<Rotor> availableRotors;
protected ArrayList<Reflector> availableReflectors;

protected Random rand;
public Enigma(int off)

public Enigma()
{
this.machineTypeOffset = off;
establishAvailableParts();
initialize();
}

public Enigma()
/**
* 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)
{
initialize();
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<EntryWheel> getAvailableEntryWheels()
{
return availableEntryWheels;
}

public ArrayList<Rotor> getAvailableRotors()
{
return availableRotors;
}

public ArrayList<Reflector> 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 int getMachineTypeOffset()
public Rotor getRotor(int index, int rotation, int ringSetting)
{
return machineTypeOffset;
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)
{
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)
public static String chooseEnigmaFromSave(BigInteger save)
{
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;
}
}

+ 53
- 50
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java View File

@@ -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);
}
}

+ 21
- 10
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java View File

@@ -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);
}
}

+ 54
- 59
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java View File

@@ -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)
public Enigma_G31()
{
super(off);
super();
machineType = "G31";
}

public Enigma_G31()
@Override
protected void establishAvailableParts()
{
super(40);
machineType = "G31";
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);
}
}

+ 22
- 10
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java View File

@@ -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);
}
}

+ 68
- 51
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java View File

@@ -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.setTypeRotor1(rotor1.getIndex());
state.setTypeRotor2(rotor2.getIndex());
state.setTypeRotor3(rotor3.getIndex());

state.setTypeReflector(reflector.getNumber());
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);
}
}

+ 66
- 73
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java View File

@@ -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)
public Enigma_K()
{
super(off);
super();
machineType = "K";
}

public Enigma_K()
@Override
protected void establishAvailableParts()
{
super(80);
machineType = "K";
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

save = save+t;
return save;
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

return s.toString(16);
}
}

+ 28
- 21
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java View File

@@ -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

save = save+t;
return save;
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

return s.toString(16);
}
}

+ 28
- 21
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java View File

@@ -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

save = save+t;
return save;
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

return s.toString(16);
}

}

+ 34
- 22
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java View File

@@ -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);
}
}

+ 101
- 66
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java View File

@@ -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<Rotor> 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);
}
}

+ 75
- 74
app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java View File

@@ -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;