From 1f3c3d33a7d33d5fb615ffca63c182a292843863 Mon Sep 17 00:00:00 2001 From: VanitasVitae Date: Fri, 30 Oct 2015 17:22:00 +0100 Subject: [PATCH] Added Enigma KD, Added protocol version to ensure compatibility with upcoming releases, Added options to share configuration as string, organized sharing/receiving of configuration, moved preference-management to SettingsActivity, Added whats-New-Dialog that informs user about latest changes --- CHANGELOG.txt | 21 +- app/src/main/AndroidManifest.xml | 1 + .../de/vanitasvitae/enigmandroid/Main2.java | 336 ---------- .../enigmandroid/MainActivity.java | 420 +++++++------ .../enigmandroid/SettingsActivity.java | 119 +++- .../enigmandroid/enigma/Enigma.java | 441 +++++++------- .../enigmandroid/enigma/Enigma_D.java | 323 +++++----- .../enigmandroid/enigma/Enigma_G260.java | 65 +- .../enigmandroid/enigma/Enigma_G31.java | 342 ++++++----- .../enigmandroid/enigma/Enigma_G312.java | 65 +- .../enigmandroid/enigma/Enigma_I.java | 354 +++++------ .../enigmandroid/enigma/Enigma_K.java | 338 ++++++----- .../enigmandroid/enigma/Enigma_KD.java | 240 ++++++++ .../enigma/Enigma_K_Swiss_Airforce.java | 67 +- .../enigma/Enigma_K_Swiss_Standard.java | 67 +- .../enigmandroid/enigma/Enigma_M3.java | 117 ++-- .../enigmandroid/enigma/Enigma_M4.java | 501 +++++++-------- .../enigmandroid/enigma/Enigma_R.java | 340 ++++++----- .../enigmandroid/enigma/Enigma_T.java | 91 +-- .../enigmandroid/enigma/Plugboard.java | 2 +- .../enigmandroid/enigma/rotors/Reflector.java | 572 +++++++++--------- .../enigmandroid/enigma/rotors/Rotor.java | 120 ++-- .../enigmandroid/layout/LayoutContainer.java | 250 ++++---- .../layout/LayoutContainer_D.java | 10 +- .../layout/LayoutContainer_G260.java | 27 +- .../layout/LayoutContainer_G31.java | 156 ++--- .../layout/LayoutContainer_I.java | 10 +- .../layout/LayoutContainer_K.java | 10 +- .../layout/LayoutContainer_KD.java | 135 +++++ .../layout/LayoutContainer_M3.java | 2 +- .../layout/LayoutContainer_M4.java | 11 +- .../layout/LayoutContainer_R.java | 10 +- .../layout/LayoutContainer_T.java | 11 +- .../layout/PassphraseDialogBuilder.java | 4 +- app/src/main/res/layout/activity_main_kd.xml | 170 ++++++ app/src/main/res/layout/dialog_passphrase.xml | 2 +- .../main/res/layout/dialog_two_options.xml | 17 + app/src/main/res/layout/dialog_whats_new.xml | 23 + app/src/main/res/menu/main.xml | 29 +- app/src/main/res/values-de/strings.xml | 17 +- .../values-de/strings_activity_settings.xml | 3 +- app/src/main/res/values/dialog_whats_new.xml | 10 + app/src/main/res/values/strings.xml | 16 +- .../res/values/strings_activity_settings.xml | 2 + 44 files changed, 3253 insertions(+), 2614 deletions(-) delete mode 100644 app/src/main/java/de/vanitasvitae/enigmandroid/Main2.java create mode 100644 app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_KD.java create mode 100644 app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_KD.java create mode 100755 app/src/main/res/layout/activity_main_kd.xml create mode 100644 app/src/main/res/layout/dialog_two_options.xml create mode 100755 app/src/main/res/layout/dialog_whats_new.xml create mode 100644 app/src/main/res/values/dialog_whats_new.xml diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 6da5ff1..c626e58 100755 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,4 +1,22 @@ CHANGELOG ENIGMANDROID +v1.0.0-not-yet-released< +*Added Enigma KD +*Added "protocol version" to QR-Code-shared configuration strings. + This breaks backwards compatibility to older versions, but I added it to enable backwards- + compatibility in upcoming releases. +*Configurations can now be shared to EnigmAndroid as text +*Moved preference management to SettingsActivity +*Added dialog to choose whether to share configuration via text or via QR-code +*Same for receiving +*Added TextBox to configuration-share-dialog that lets the user see and copy the configuration string. +*Added Whats-new-Dialog +*TODO: Move KD right below K? +*TODO: Add intent filters to recognize and automatically handle shared/copied configuration Strings + TODO: These are Strings starting with "EnigmAndroid/" +*TODO: Write tests to ensure correct functionality (Pull Requests welcome) +*TODO: Add multi-Enigma (select any rotor/reflector etc. Probably wont happen too soon) + + v0.1.9-09.10.2015< *Added option to share/receive configurations via QR-Code (ZXing Barcode Scanner) *Prevent user from setting incomplete reflector wiring @@ -7,9 +25,6 @@ v0.1.9-09.10.2015< *Completely verified correct functionality of Enigma T *Added number spelling in spanish, italian *Added backwards compatibility to API level 10 (Gingerbread 2.3.3) -*TODO: Write tests to ensure correct functionality -*TODO: Migrate preferences to SettingsActivity -*TODO: Add multi-Enigma (select any rotor/reflector etc. Probably wont happen too soon) v0.1.8-27.09.2015< *Added Enigma G31 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 284c739..9a0a91b 100755 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,6 +16,7 @@ + diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/Main2.java b/app/src/main/java/de/vanitasvitae/enigmandroid/Main2.java deleted file mode 100644 index 701a1a1..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/Main2.java +++ /dev/null @@ -1,336 +0,0 @@ -package de.vanitasvitae.enigmandroid; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.os.Bundle; -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.layout.LayoutContainer; -import de.vanitasvitae.enigmandroid.layout.PassphraseDialogBuilder; - -/** - * Reimplementation of MainActivity - * Created by vanitas on 11.10.15. - */ -public class Main2 extends Activity -{ - private static final int RESULT_SETTINGS = 1; - private static final String URI_CHANGELOG = - "https://github.com/vanitasvitae/EnigmAndroid/blob/master/CHANGELOG.txt"; - public static final String APP_ID = "EnigmAndroid"; - - private LayoutContainer layoutContainer; - private SecureRandom numberGenerator; - - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - this.numberGenerator = new SecureRandom(); - MainActivity.ActivitySingleton singleton = MainActivity.ActivitySingleton.getInstance(); - singleton.setActivity(this); - numberGenerator = new SecureRandom(); - restoreEnigmaModelAndState(); - - //Handle shared text - Intent intent = getIntent(); - String action = intent.getAction(); - String type = intent.getType(); - if (Intent.ACTION_SEND.equals(action) && type != null) { - if ("text/plain".equals(type)) - { - String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); - if (sharedText != null) - { - layoutContainer.getInput().setRawText(sharedText); - } - } - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) - { - this.getMenuInflater().inflate(R.menu.main, menu); - return true; - } - - @Override - /** - * Handle Options menu clicks - */ - public boolean onOptionsItemSelected(MenuItem item) - { - int id = item.getItemId(); - if (id == R.id.action_reset) - { - layoutContainer.resetLayout(); - Toast.makeText(getApplicationContext(), R.string.message_reset, - Toast.LENGTH_SHORT).show(); - return true; - } - else if (id == R.id.action_send) - { - actionShareMessage(); - } - else if (id == R.id.action_choose_ringsetting) - { - layoutContainer.showRingSettingsDialog(); - return true; - } - else if(id == R.id.action_enter_seed) - { - new PassphraseDialogBuilder().showDialog(); - return true; - } - else if (id == R.id.action_receive_scan) - { - IntentIntegrator integrator = new IntentIntegrator(this); - integrator.initiateScan(); - return true; - } - else if(id == R.id.action_share_scan) - { - actionShareConfiguration(); - return true; - } - else if (id == R.id.action_random_configuration) - { - actionRandomConfiguration(); - return true; - } - else if (id == R.id.action_settings) - { - Intent i = new Intent(this, SettingsActivity.class); - startActivityForResult(i, RESULT_SETTINGS); - } - else if (id == R.id.action_about) - { - actionAbout(); - return true; - } - - return super.onOptionsItemSelected(item); - } - - /** - * Handle preference changes - * @param requestCode requestCode - * @param resultCode resultCode (RESULT_SETTINGS is defined at the top) - * @param data data (not important here) - */ - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - - switch (requestCode) { - //Settings - case RESULT_SETTINGS: - { - restoreEnigmaModelAndState(); - break; - } - //QR_Scanner - case IntentIntegrator.REQUEST_CODE: - IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); - 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!"); - restoreEnigmaModelAndState(content); - } - } - } - } - - @Override - protected void onPause() - { - super.onPause(); - //TODO: Save state - } - - private boolean restoreEnigmaModelAndState() - { - String savedState = SettingsActivity.SettingsSingleton.getInstance().getPrefSavedEnigmaState(); - if (savedState.equals("-1") || SettingsActivity.SettingsSingleton.getInstance().prefSavedEnigmaStateChanged()) - { - layoutContainer = LayoutContainer.createLayoutContainer( - SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType()); - return false; - } - //No changes - return false; - } - - private boolean restoreEnigmaModelAndState(BigInteger savedState) - { - SettingsActivity.SettingsSingleton.getInstance().setPrefMachineType( - Enigma.chooseEnigmaFromSave(savedState)); - String savedInput = ""; - if(layoutContainer != null) savedInput = layoutContainer.getInput().getText(); - layoutContainer = LayoutContainer.createLayoutContainer( - SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType()); - layoutContainer.getEnigma().restoreState(Enigma.removeDigit(savedState, 20)); - layoutContainer.syncStateFromEnigmaToLayout(); - layoutContainer.getInput().setText(savedInput); - layoutContainer.getOutput().setText(""); - return true; - } - - /** - * Restore Enigma state from String. String has to start with "EnigmAndroid/" - * @param savedState String - * @return success - */ - private boolean restoreEnigmaModelAndState(String savedState) - { - if(!savedState.startsWith(APP_ID+"/")) - { - Toast.makeText(this, R.string.error_no_valid_qr, Toast.LENGTH_LONG).show(); - return false; - } - else - { - savedState = savedState.substring((APP_ID+"/").length()); - BigInteger s = new BigInteger(savedState, 16); - return restoreEnigmaModelAndState(s); - } - } - - /** - * Set EnigmAndroid into a state calculated from the seed. - * @param seed seed - */ - public void createStateFromSeed(String seed) - { - String savedInput = ""; - if(layoutContainer != null) savedInput = layoutContainer.getInput().getText(); - SettingsActivity.SettingsSingleton.getInstance().setPrefMachineType( - Enigma.chooseEnigmaFromSeed(seed)); - layoutContainer = LayoutContainer.createLayoutContainer(); - layoutContainer.getEnigma().setStateFromSeed(seed); - layoutContainer.syncStateFromEnigmaToLayout(); - layoutContainer.getInput().setText(savedInput); - layoutContainer.getOutput().setText(""); - } - - /** - * 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 text box and update the spinners to their new positions. - * @param v View - */ - public void doCrypto(View v) - { - layoutContainer.doCrypto(); - } - - /** - * If there is any message inside the right text field, share it via intent. - * Otherwise show a Toast. - */ - private void actionShareMessage() - { - if(layoutContainer.getOutput().getText().length() == 0) - { - Toast.makeText(this, R.string.error_no_text_to_send, Toast.LENGTH_SHORT).show(); - } - else - { - Intent sendIntent = new Intent(); - sendIntent.setAction(Intent.ACTION_SEND); - sendIntent.putExtra(Intent.EXTRA_TEXT, layoutContainer.getOutput().getModifiedText()); - sendIntent.setType("text/plain"); - startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); - } - } - - /** - * Share current enigma configuration via QR-Code - */ - private void actionShareConfiguration() - { - IntentIntegrator QRIntegrator = new IntentIntegrator(this); - layoutContainer.syncStateFromLayoutToEnigma(); - Log.d(APP_ID, - "Sharing configuration to QR: "+layoutContainer.getEnigma().stateToString()); - QRIntegrator.shareText(APP_ID+"/"+layoutContainer.getEnigma().stateToString()); - } - - /** - * Set the enigma to a random state. - * Do not change enigma model. - */ - private void actionRandomConfiguration() - { - layoutContainer.getEnigma().randomState(); - layoutContainer.syncStateFromEnigmaToLayout(); - Toast.makeText(getApplicationContext(), R.string.message_random, - Toast.LENGTH_SHORT).show(); - layoutContainer.getOutput().setText(""); - } - - /** - * Show the credits dialog of the app - */ - private void actionAbout() - { - 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();} - assert pInfo != null; - 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) - .setCancelable(true) - .setPositiveButton(R.string.dialog_positive, new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, int id) - { - dialog.dismiss(); - } - }) - .setNegativeButton(R.string.button_show_changelog, new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, int id) - { - dialog.cancel(); - Uri webPage = Uri.parse(URI_CHANGELOG); - Intent intent = new Intent(Intent.ACTION_VIEW, webPage); - if (intent.resolveActivity(getPackageManager()) != null) { - startActivity(intent); - } - } - }).show(); - } - - public SecureRandom getNumberGenerator() - { - return this.numberGenerator; - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java b/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java index bc427e8..0081a35 100755 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java @@ -1,7 +1,10 @@ package de.vanitasvitae.enigmandroid; +import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; +import android.app.Dialog; +import android.content.ClipData; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -11,10 +14,15 @@ import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; +import android.text.InputType; import android.util.Log; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; @@ -55,11 +63,10 @@ public class MainActivity extends Activity private static final String URI_CHANGELOG = "https://github.com/vanitasvitae/EnigmAndroid/blob/master/CHANGELOG.txt"; public static final String APP_ID = "EnigmAndroid"; + public static final int latest_protocol_version = 1; + public static final int max_protocol_version = 256; private LayoutContainer layoutContainer; - private String prefMachineType; - private String prefNumericLanguage; - private String prefMessageFormatting; private SecureRandom secureRandom; @@ -68,15 +75,13 @@ public class MainActivity extends Activity { super.onCreate(savedInstanceState); secureRandom = new SecureRandom(); - ActivitySingleton singleton = ActivitySingleton.getInstance(); - singleton.setActivity(this); - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources(). - getStringArray(R.array.pref_alias_machine_type)[0]); + ActivitySingleton.getInstance().setActivity(this); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + SettingsActivity.SettingsSingleton.getInstance(prefs, getResources()); + layoutContainer = LayoutContainer.createLayoutContainer(); - updateContentView(); - layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType); - updatePreferenceValues(); + //Handle whats-new dialog + handleVersionUpdate(); //Handle shared text Intent intent = getIntent(); @@ -88,7 +93,12 @@ public class MainActivity extends Activity String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); if (sharedText != null) { - layoutContainer.getInput().setRawText(sharedText); + //If shared text consists of an encoded configuration, try to restore it + if(sharedText.startsWith(APP_ID+"/")) + restoreStateFromCode(sharedText); + //Else put it in the input text box + else + layoutContainer.getInput().setRawText(sharedText); } } } @@ -97,114 +107,6 @@ public class MainActivity extends Activity @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); - this.updateContentView(); - } - - private void updateContentView() - { - switch (prefMachineType) - { - case "I": - setContentView(R.layout.activity_main_i_m3); - break; - case "M3": - this.setContentView(R.layout.activity_main_i_m3); - break; - case "M4": - this.setContentView(R.layout.activity_main_m4); - break; - case "D": - this.setContentView(R.layout.activity_main_d); - break; - case "K": - case "KS": - case "KSA": - case "T": - case "R": - case "G31": - case "G312": - case "G260": - this.setContentView(R.layout.activity_main_g_k_r_t); - break; - default: - this.setContentView(R.layout.activity_main_i_m3); - break; - } - } - - private void updatePreferenceValues() - { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - this.setPrefMachineType(sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources(). - getStringArray(R.array.pref_alias_machine_type)[0])); - 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])); - } - - private void setPrefMachineType(String type) - { - if(prefMachineType == null || !prefMachineType.equals(type)) - { - prefMachineType = type; - String savedInput = ""; - if(layoutContainer != null) - { - savedInput = layoutContainer.getInput().getText(); - } - updateContentView(); - layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType); - layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); - layoutContainer.getInput().setText(savedInput); - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - sharedPreferences.edit().putString(SettingsActivity.PREF_MACHINE_TYPE, type).apply(); - } - } - - public String getPrefMachineType() - { - if(prefMachineType != null) return prefMachineType; - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources(). - getStringArray(R.array.pref_alias_machine_type)[0]); - return prefMachineType; - } - - public void setPrefNumericLanguage(String lang) - { - if(prefNumericLanguage == null || !prefNumericLanguage.equals(lang)) - { - prefNumericLanguage = lang; - layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); - } - } - - public String getPrefNumericLanguage() - { - if(prefNumericLanguage != null) return prefNumericLanguage; - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - this.prefNumericLanguage = sharedPreferences.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources(). - getStringArray(R.array.pref_alias_numeric_spelling_language)[0]); - return prefNumericLanguage; - } - - public void setPrefMessageFormatting(String messageFormatting) - { - if(prefMessageFormatting == null || !prefMessageFormatting.equals(messageFormatting)) - { - prefMessageFormatting = messageFormatting; - layoutContainer.setEditTextAdapter(messageFormatting); - } - } - - public String getPrefMessageFormatting() - { - if(prefMessageFormatting != null) return prefMessageFormatting; - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - this.prefMessageFormatting = sharedPreferences.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, getResources(). - getStringArray(R.array.pref_alias_message_formatting)[0]); - return prefMessageFormatting; } public SecureRandom getSecureRandom() @@ -238,31 +140,7 @@ public class MainActivity extends Activity Toast.LENGTH_SHORT).show(); return true; } - else if (id == R.id.action_random_configuration) - { - layoutContainer.getEnigma().randomState(); - layoutContainer.syncStateFromEnigmaToLayout(); - Toast.makeText(getApplicationContext(), R.string.message_random, - Toast.LENGTH_SHORT).show(); - layoutContainer.getOutput().setText(""); - return true; - } - else if (id == R.id.action_choose_ringsetting) - { - layoutContainer.showRingSettingsDialog(); - return true; - } - else if (id == R.id.action_settings) - { - Intent i = new Intent(this, SettingsActivity.class); - startActivityForResult(i, RESULT_SETTINGS); - } - else if (id == R.id.action_about) - { - showAboutDialog(); - return true; - } - else if (id == R.id.action_send) + else if (id == R.id.action_send_message) { if(layoutContainer.getOutput().getText().length() == 0) { @@ -277,23 +155,37 @@ public class MainActivity extends Activity startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); } } - else if (id == R.id.action_receive_scan) + else if (id == R.id.action_choose_ringsetting) { - IntentIntegrator integrator = new IntentIntegrator(this); - integrator.initiateScan(); + layoutContainer.showRingSettingsDialog(); return true; } - else if(id == R.id.action_share_scan) + else if(id == R.id.action_share_configuration) { - IntentIntegrator QRIntegrator = new IntentIntegrator(this); - layoutContainer.syncStateFromLayoutToEnigma(); - Log.d(APP_ID, "Sharing configuration to QR: " + layoutContainer.getEnigma().stateToString()); - QRIntegrator.shareText(APP_ID+"/"+layoutContainer.getEnigma().stateToString()); + showShareConfigurationDialog(); + } + else if (id == R.id.action_restore_configuration) + { + showReceiveConfigurationDialog(); return true; } - else if(id == R.id.action_enter_seed) + else if (id == R.id.action_random_configuration) { - new PassphraseDialogBuilder().showDialog(); + layoutContainer.getEnigma().randomState(); + layoutContainer.syncStateFromEnigmaToLayout(); + Toast.makeText(getApplicationContext(), R.string.message_random, + Toast.LENGTH_SHORT).show(); + layoutContainer.getOutput().setText(""); + return true; + } + else if (id == R.id.action_settings) + { + Intent i = new Intent(this, SettingsActivity.class); + startActivityForResult(i, RESULT_SETTINGS); + } + else if (id == R.id.action_about) + { + showAboutDialog(); return true; } return super.onOptionsItemSelected(item); @@ -310,6 +202,80 @@ public class MainActivity extends Activity layoutContainer.doCrypto(); } + private void shareConfigurationAsQR() + { + IntentIntegrator QRIntegrator = new IntentIntegrator(this); + layoutContainer.syncStateFromLayoutToEnigma(); + String encoded_state = APP_ID+"/"+layoutContainer.getEnigma().getEncodedState().toString(16); + Log.d(APP_ID, "Sharing configuration to QR: "+encoded_state); + QRIntegrator.shareText(encoded_state); + } + + private void shareConfigurationAsText() + { + Intent sendIntent = new Intent(); + sendIntent.setAction(Intent.ACTION_SEND); + sendIntent.putExtra(Intent.EXTRA_TEXT, + APP_ID+"/"+layoutContainer.getEnigma().getEncodedState().toString(16)); + sendIntent.setType("text/plain"); + startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); + } + + private void receiveConfigurationQR() + { + IntentIntegrator integrator = new IntentIntegrator(this); + integrator.initiateScan(); + } + + private void receiveConfigurationText() + { + new PassphraseDialogBuilder().showDialog(); + } + + private void handleVersionUpdate() + { + int currentVersionNumber = 0; + int savedVersionNumber = SettingsActivity.SettingsSingleton.getInstance().getVersionNumber(); + try + { + PackageInfo p = getPackageManager().getPackageInfo(getPackageName(), 0); + currentVersionNumber = p.versionCode; + } + catch (Exception ignored) {} + if(currentVersionNumber > savedVersionNumber) + { + showWhatsNewDialog(); + SettingsActivity.SettingsSingleton.getInstance().setVersionNumber(currentVersionNumber); + } + + } + + private void showWhatsNewDialog() + { + PackageInfo pInfo = null; + try{ pInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);} + catch (PackageManager.NameNotFoundException e){ e.printStackTrace();} + assert pInfo != null; + String version = pInfo.versionName; + LayoutInflater li = LayoutInflater.from(this); + @SuppressLint("InflateParams") + View dialog = li.inflate(R.layout.dialog_whats_new, null); + ((TextView) dialog.findViewById(R.id.dialog_whats_new_header)).setText( + String.format(getResources().getText(R.string.dialog_whats_new_header).toString(),version)); + ((TextView) dialog.findViewById(R.id.dialog_whats_new_details)).setText( + R.string.dialog_whats_new_content); + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setView(dialog).setTitle(R.string.dialog_whats_new_title) + .setPositiveButton(R.string.dialog_positive, new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dialog, int which) + { + dialog.dismiss(); + } + }); + builder.create().show(); + } /** * Show a Dialog containing information about the app, license, usage, author and a link * to the changelog @@ -348,27 +314,110 @@ public class MainActivity extends Activity }).show(); } + private void showShareConfigurationDialog() + { + final String configuration = APP_ID+"/"+layoutContainer.getEnigma().getEncodedState().toString(16); + final View shareView = View.inflate(this, R.layout.dialog_two_options, null); + LinearLayout l = (LinearLayout) shareView.findViewById(R.id.dialog_two_options_lay); + EditText e = new EditText(this); + e.setText(configuration); + e.setInputType(InputType.TYPE_NULL); + e.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){ + android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE); + ClipData clip; + clip = ClipData.newPlainText("label", configuration); + clipboard.setPrimaryClip(clip); + } else{ + @SuppressWarnings("deprecation") + android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(CLIPBOARD_SERVICE); + clipboard.setText(configuration); + } + Toast.makeText(getApplicationContext(), R.string.message_clipboard, Toast.LENGTH_SHORT).show(); + } + }); + l.addView(e); + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.dialog_title_share_configuration) + .setView(shareView).setCancelable(true); + final Dialog d = builder.create(); + Button one = (Button) shareView.findViewById(R.id.dialog_two_options_2); + one.setText(R.string.dialog_share_qr); + one.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + shareConfigurationAsQR(); + d.dismiss(); + } + }); + Button two = (Button) shareView.findViewById(R.id.dialog_two_options_1); + two.setText(R.string.dialog_share_code); + two.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + shareConfigurationAsText(); + d.dismiss(); + } + }); + d.show(); + } + + private void showReceiveConfigurationDialog() + { + final View shareView = View.inflate(this, R.layout.dialog_two_options, null); + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.dialog_title_restore_configuration) + .setView(shareView).setCancelable(true); + final Dialog d = builder.create(); + Button one = (Button) shareView.findViewById(R.id.dialog_two_options_2); + one.setText(R.string.dialog_restore_qr); + one.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + receiveConfigurationQR(); + d.dismiss(); + } + }); + Button two = (Button) shareView.findViewById(R.id.dialog_two_options_1); + two.setText(R.string.dialog_restore_code); + two.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + receiveConfigurationText(); + d.dismiss(); + } + }); + d.show(); + } + /** - * Handle preference changes + * Handle Activity Results * @param requestCode requestCode * @param resultCode resultCode (RESULT_SETTINGS is defined at the top) - * @param data data (not important here) + * @param data data */ protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - switch (requestCode) { + //Come back from Settings case RESULT_SETTINGS: { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - this.setPrefMachineType(sharedPrefs.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources() - .getStringArray(R.array.pref_alias_machine_type)[0])); - 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, - getResources().getStringArray(R.array.pref_alias_message_formatting)[0])); + applyPreferenceChanges(); break; } + // Receive from QR case IntentIntegrator.REQUEST_CODE: IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (scanResult != null) { @@ -382,6 +431,26 @@ public class MainActivity extends Activity } } + /** + * Handle changes in preferences and apply those changes to the app + */ + private void applyPreferenceChanges() + { + SettingsActivity s = SettingsActivity.SettingsSingleton.getInstance(); + if(s.prefMachineTypeChanged()) + { + layoutContainer = LayoutContainer.createLayoutContainer(); + } + if(s.prefMessageFormattingChanged()) + { + layoutContainer.setEditTextAdapter(s.getPrefMessageFormatting()); + } + if(s.prefNumericLanguageChanged()) + { + layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); + } + } + /** * Set EnigmAndroid into a certain state as described in the QR-Code * @param mem content of the QR-Code @@ -397,11 +466,14 @@ public class MainActivity extends Activity String inputString = layoutContainer.getInput().getText(); mem = mem.substring((APP_ID+"/").length()); BigInteger s = new BigInteger(mem, 16); - Log.d(APP_ID, "Try to restore configuration from BigInteger value "+ s.toString()); - setPrefMachineType(Enigma.chooseEnigmaFromSave(s)); - updateContentView(); - layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType()); - layoutContainer.getEnigma().restoreState(Enigma.removeDigit(s,20)); + int protocol_version = Enigma.getValue(s, max_protocol_version); + s = Enigma.removeDigit(s, max_protocol_version); + Log.d(APP_ID, + "Try to restore configuration from BigInteger value "+s.toString()+" in protocol version "+protocol_version+"."); + SettingsActivity.SettingsSingleton.getInstance() + .setPrefMachineType(Enigma.chooseEnigmaFromSave(s)); + layoutContainer = LayoutContainer.createLayoutContainer(); + layoutContainer.getEnigma().restoreState(Enigma.removeDigit(s,20), protocol_version); layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); layoutContainer.syncStateFromEnigmaToLayout(); layoutContainer.getInput().setText(inputString); @@ -413,12 +485,12 @@ public class MainActivity extends Activity * Set EnigmAndroid into a state calculated from the seed. * @param seed seed */ - public void createStateFromSeed(String seed) + public void applyStateFromSeed(String seed) { String inputString = layoutContainer.getInput().getText(); - setPrefMachineType(Enigma.chooseEnigmaFromSeed(seed)); - updateContentView(); - layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType()); + SettingsActivity.SettingsSingleton.getInstance() + .setPrefMachineType(Enigma.chooseEnigmaFromSeed(seed)); + layoutContainer = LayoutContainer.createLayoutContainer(); layoutContainer.getEnigma().setStateFromSeed(seed); layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); layoutContainer.syncStateFromEnigmaToLayout(); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java b/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java index 869b890..694a337 100755 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java @@ -1,8 +1,10 @@ package de.vanitasvitae.enigmandroid; +import android.content.SharedPreferences; +import android.content.res.Resources; import android.os.Bundle; import android.preference.PreferenceActivity; -import android.preference.PreferenceManager; +import android.util.Log; /** * Class that represents the settings activity. @@ -30,6 +32,7 @@ public class SettingsActivity extends PreferenceActivity public static final String PREF_MESSAGE_FORMATTING = "prefMessageFormatting"; public static final String PREF_REPLACE_SPECIAL_CHARACTERS = "prefReplaceSpecialCharacters"; public static final String PREF_SAVED_ENIGMA_STATE = "prefSavedEnigmaState"; + public static final String PREF_VERSION_NUMBER = "prefVersionNumber"; private String previousPrefNumericLanguage; private String previousPrefMachineType; @@ -37,29 +40,55 @@ public class SettingsActivity extends PreferenceActivity private boolean previousPrefReplaceSpecialCharacters; private String previousPrefSavedEnigmaState; + SharedPreferences prefs; + Resources res; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //noinspection deprecation addPreferencesFromResource(R.xml.pref_page); - this.previousPrefMachineType = getPrefMachineType(); - this.previousPrefSavedEnigmaState = getPrefSavedEnigmaState(); - this.previousPrefMessageFormatting = getPrefMessageFormatting(); - this.previousPrefNumericLanguage = getPrefNumericLanguage(); } + public void setSharedPreferences(SharedPreferences prefs) + { + this.prefs = prefs; + } + + public void setResources(Resources res) + { + this.res = res; + } + + private boolean isFullyInitilaized() + { + if(prefs == null) + { + Log.e(MainActivity.APP_ID, "SharedPreferences not initialized via setSharedPreferences!"); + return false; + } + if(res == null) + { + Log.e(MainActivity.APP_ID, "Resources not initialized via setResources!"); + return false; + } + return true; + } + public String getPrefNumericLanguage() { - return PreferenceManager.getDefaultSharedPreferences(this).getString( + if(isFullyInitilaized()) + return prefs.getString( PREF_NUMERIC_LANGUAGE, - getResources().getStringArray(R.array.pref_alias_message_formatting)[0]); + res.getStringArray(R.array.pref_alias_message_formatting)[0]); + else return null; } public void setPrefNumericLanguage(String lang) { - PreferenceManager.getDefaultSharedPreferences(this).edit() - .putString(PREF_NUMERIC_LANGUAGE, lang).apply(); + if(isFullyInitilaized()) + prefs.edit().putString(PREF_NUMERIC_LANGUAGE, lang).apply(); } public boolean prefNumericLanguageChanged() @@ -67,6 +96,7 @@ public class SettingsActivity extends PreferenceActivity if(this.previousPrefNumericLanguage == null || !this.previousPrefNumericLanguage.equals(getPrefNumericLanguage())) { this.previousPrefNumericLanguage = this.getPrefNumericLanguage(); + Log.d(MainActivity.APP_ID, PREF_NUMERIC_LANGUAGE +" changed!"); return true; } return false; @@ -74,14 +104,15 @@ public class SettingsActivity extends PreferenceActivity public boolean getPrefReplaceSpecialCharacters() { - return PreferenceManager.getDefaultSharedPreferences(this).getBoolean( - PREF_REPLACE_SPECIAL_CHARACTERS, true); + if(isFullyInitilaized()) + return prefs.getBoolean(PREF_REPLACE_SPECIAL_CHARACTERS, true); + else return false; } public void setPrefReplaceSpecialCharacters(boolean replace) { - PreferenceManager.getDefaultSharedPreferences(this).edit() - .putBoolean(PREF_REPLACE_SPECIAL_CHARACTERS, replace).apply(); + if(isFullyInitilaized()) + prefs.edit().putBoolean(PREF_REPLACE_SPECIAL_CHARACTERS, replace).apply(); } public boolean prefReplaceSpecialCharactersChanged() @@ -90,6 +121,7 @@ public class SettingsActivity extends PreferenceActivity if(changed) { previousPrefReplaceSpecialCharacters = getPrefReplaceSpecialCharacters(); + Log.d(MainActivity.APP_ID, PREF_REPLACE_SPECIAL_CHARACTERS +" changed!"); return true; } return false; @@ -97,15 +129,16 @@ public class SettingsActivity extends PreferenceActivity public String getPrefMachineType() { - return PreferenceManager.getDefaultSharedPreferences(this).getString( - PREF_MACHINE_TYPE, - getResources().getStringArray(R.array.pref_alias_machine_type)[0]); + if(isFullyInitilaized()) + return prefs.getString(PREF_MACHINE_TYPE, + res.getStringArray(R.array.pref_alias_machine_type)[0]); + else return null; } public void setPrefMachineType(String pref) { - PreferenceManager.getDefaultSharedPreferences(this).edit() - .putString(PREF_MACHINE_TYPE, pref).apply(); + if(isFullyInitilaized()) + prefs.edit().putString(PREF_MACHINE_TYPE, pref).apply(); } public boolean prefMachineTypeChanged() @@ -113,6 +146,7 @@ public class SettingsActivity extends PreferenceActivity if(this.previousPrefMachineType == null || !this.previousPrefMachineType.equals(getPrefMachineType())) { this.previousPrefMachineType = this.getPrefMachineType(); + Log.d(MainActivity.APP_ID, PREF_MACHINE_TYPE +" changed!"); return true; } return false; @@ -120,8 +154,9 @@ public class SettingsActivity extends PreferenceActivity public String getPrefSavedEnigmaState() { - return PreferenceManager.getDefaultSharedPreferences(this) - .getString(PREF_SAVED_ENIGMA_STATE, "-1"); + if(isFullyInitilaized()) + return prefs.getString(PREF_SAVED_ENIGMA_STATE, "-1"); + else return null; } /** @@ -129,8 +164,8 @@ public class SettingsActivity extends PreferenceActivity */ public void setPrefSavedEnigmaState(String state) { - PreferenceManager.getDefaultSharedPreferences(this).edit() - .putString(PREF_SAVED_ENIGMA_STATE, state).apply(); + if(isFullyInitilaized()) + prefs.edit().putString(PREF_SAVED_ENIGMA_STATE, state).apply(); } public boolean prefSavedEnigmaStateChanged() @@ -139,6 +174,7 @@ public class SettingsActivity extends PreferenceActivity .equals(getPrefSavedEnigmaState())) { this.previousPrefSavedEnigmaState = this.getPrefSavedEnigmaState(); + Log.d(MainActivity.APP_ID, PREF_SAVED_ENIGMA_STATE +" changed!"); return true; } return false; @@ -146,15 +182,16 @@ public class SettingsActivity extends PreferenceActivity public String getPrefMessageFormatting() { - return PreferenceManager.getDefaultSharedPreferences(this) - .getString(SettingsActivity.PREF_MESSAGE_FORMATTING, getResources(). - getStringArray(R.array.pref_alias_message_formatting)[0]); + if(isFullyInitilaized()) + return prefs.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, + res.getStringArray(R.array.pref_alias_message_formatting)[0]); + else return null; } public void setPrefMessageFormatting(String format) { - PreferenceManager.getDefaultSharedPreferences(this).edit() - .putString(PREF_MESSAGE_FORMATTING, format).apply(); + if(isFullyInitilaized()) + prefs.edit().putString(PREF_MESSAGE_FORMATTING, format).apply(); } public boolean prefMessageFormattingChanged() @@ -163,11 +200,25 @@ public class SettingsActivity extends PreferenceActivity .equals(getPrefMessageFormatting())) { this.previousPrefMessageFormatting = this.getPrefMessageFormatting(); + Log.d(MainActivity.APP_ID, PREF_MESSAGE_FORMATTING +" changed!"); return true; } return false; } + public int getVersionNumber() + { + if(isFullyInitilaized()) + return prefs.getInt(PREF_VERSION_NUMBER, -1); + else return -1; + } + + public void setVersionNumber(int v) + { + if(isFullyInitilaized()) + prefs.edit().putInt(PREF_VERSION_NUMBER, v).apply(); + } + public static class SettingsSingleton extends SettingsActivity { private static SettingsActivity instance; @@ -176,9 +227,21 @@ public class SettingsActivity extends PreferenceActivity super(); } + public static SettingsActivity getInstance(SharedPreferences prefs, Resources res) + { + instance = new SettingsActivity(); + instance.setSharedPreferences(prefs); + instance.setResources(res); + return instance; + } + public static SettingsActivity getInstance() { - if(instance == null) instance = new SettingsActivity(); + if(instance == null) + { + instance = new SettingsActivity(); + Log.d(MainActivity.APP_ID, "Created new SettingsActivity!"); + } return instance; } } 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 41e420f..916f23f 100755 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java @@ -31,255 +31,262 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public abstract class Enigma { - protected static String machineType; + protected static String machineType; - protected boolean doAnomaly = false; //Has the time come to handle an anomaly? + protected boolean doAnomaly = false; //Has the time come to handle an anomaly? - protected ArrayList availableEntryWheels; - protected ArrayList availableRotors; - protected ArrayList availableReflectors; + protected ArrayList availableEntryWheels; + protected ArrayList availableRotors; + protected ArrayList availableReflectors; - protected Random rand; + protected Random rand; - public Enigma() - { - establishAvailableParts(); - initialize(); - } + public Enigma() + { + establishAvailableParts(); + initialize(); + } - /** - * In this method, available EntryWheels, Rotors and Reflectors can be defined. - */ - protected abstract void establishAvailableParts(); + /** + * 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) - { - if(availableRotors == null) availableRotors = new ArrayList<>(); - availableRotors.add(availableRotors.size(), r.setIndex(availableRotors.size())); - } + /** + * 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) + { + 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 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())); - } + 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 getAvailableEntryWheels() + { + return availableEntryWheels; + } - public ArrayList getAvailableRotors() - { - return availableRotors; - } + public ArrayList getAvailableRotors() + { + return availableRotors; + } - public ArrayList getAvailableReflectors() - { - return availableReflectors; - } + 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 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) + { + 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 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) + { + 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); - } + public Reflector getReflector(int index, int rotation, int ringSetting) + { + return getReflector(index).setRotation(rotation).setRingSetting(ringSetting); + } - /** - * Set the enigma to an initial state - */ - public abstract void initialize(); + /** + * Set the enigma to an initial state + */ + public abstract void initialize(); - /** - * Encrypt / Decrypt a given String w. - * w must be prepared using prepare(w) beforehand. - * Doing so changes the state of the rotors but not the state of the plugboard and the - * ringSettings - * - * @param w Text to decrypt/encryptString - * @return encrypted/decrypted string - */ - public String encryptString(String w) - { - //output string - String output = ""; - //for each char x in k - for (char x : w.toCharArray()) - { - output = output + this.encryptChar(x); - } - //return en-/decrypted string - return output; - } + /** + * Encrypt / Decrypt a given String w. + * w must be prepared using prepare(w) beforehand. + * Doing so changes the state of the rotors but not the state of the plugboard and the + * ringSettings + * + * @param w Text to decrypt/encryptString + * @return encrypted/decrypted string + */ + public String encryptString(String w) + { + //output string + String output = ""; + //for each char x in k + for (char x : w.toCharArray()) + { + output = output + this.encryptChar(x); + } + //return en-/decrypted string + return output; + } - /** - * Set the enigma into the next mechanical state. - * This rotates the first rotor and eventually also the second/third. - * Also this method handles the anomaly in case it should happen. - */ - public abstract void nextState(); + /** + * Set the enigma into the next mechanical state. + * This rotates the first rotor and eventually also the second/third. + * Also this method handles the anomaly in case it should happen. + */ + public abstract void nextState(); - /** - * Set the enigma into a completely random state using a unseeded SecureRandom object. - */ - public void randomState() - { - this.rand = ((MainActivity) (MainActivity.ActivitySingleton.getInstance().getActivity())) - .getSecureRandom(); - generateState(); - } + /** + * Set the enigma into a completely random state using a unseeded SecureRandom object. + */ + public void randomState() + { + this.rand = ((MainActivity) (MainActivity.ActivitySingleton.getInstance().getActivity())) + .getSecureRandom(); + generateState(); + } - /** - * Set the enigma to a random state based on the initialization of rand. - * Don not choose a rotor twice, set random rotations, ringSettings, ukw and possibly - * plugboard / rewirable ukw configurations. - */ - protected abstract void generateState(); + /** + * Set the enigma to a random state based on the initialization of rand. + * Don not choose a rotor twice, set random rotations, ringSettings, ukw and possibly + * plugboard / rewirable ukw configurations. + */ + protected abstract void generateState(); - /** - * Substitute char k by sending the signal through the enigma. - * The signal passes the plugboard, the rotors and returns back after going through the - * reflector wheel. - * - * @param k input char - * @return substituted output char - */ - public abstract char encryptChar(char k); + /** + * Substitute char k by sending the signal through the enigma. + * The signal passes the plugboard, the rotors and returns back after going through the + * reflector wheel. + * + * @param k input char + * @return substituted output char + */ + public abstract char encryptChar(char k); - /** - * Set the state of the enigma - * @param state new state - */ - public abstract void setState(EnigmaStateBundle state); + /** + * Set the state of the enigma + * @param state new state + */ + public abstract void setState(EnigmaStateBundle state); - /** - * Return an object representing the current state of the enigma - * @return state - */ - public abstract EnigmaStateBundle getState(); + /** + * Return an object representing the current state of the enigma + * @return state + */ + public abstract EnigmaStateBundle getState(); - /** - * Set the rand into a certain state based on seed. - * Then set the enigmas state. - * @param seed passphrase - */ - public void setStateFromSeed(String seed) - { - rand = new Random(seed.hashCode()); - generateState(); - } + /** + * Set the rand into a certain state based on seed. + * Then set the enigmas state. + * @param seed passphrase + */ + public void setStateFromSeed(String seed) + { + rand = new Random(seed.hashCode()); + generateState(); + } - public abstract void restoreState(BigInteger mem); + public abstract void restoreState(BigInteger mem, int protocol_version); - public abstract String stateToString(); + public BigInteger getEncodedState() + { + return getEncodedState(MainActivity.latest_protocol_version); + } + public abstract BigInteger getEncodedState(int protocol_version); - public static String numToMachineType(int n) - { - n = (12+(n+12)%12)%12; //Problem? Trolololo - switch (n) { - case 0: return "I"; - case 1: return "M3"; - case 2: return "M4"; - case 3: return "G31"; - case 4: return "G312"; - case 5: return "G260"; - case 6: return "D"; - case 7: return "K"; - case 8: return "KS"; - case 9: return "KSA"; - case 10: return "R"; - default: return "T"; - } - } + public static String numToMachineType(int n) + { + int m = 13; + n = (m+(n+m)%m)%m; //Problem? Trolololo + switch (n) { + case 0: return "I"; + case 1: return "M3"; + case 2: return "M4"; + case 3: return "G31"; + case 4: return "G312"; + case 5: return "G260"; + case 6: return "D"; + case 7: return "K"; + case 8: return "KS"; + case 9: return "KSA"; + case 10: return "R"; + case 11: return "T"; + case 12: return "KD"; + default: return "KD"; + } + } - public static String chooseEnigmaFromSeed(String seed) - { - return numToMachineType(seed.hashCode() % 12); - } + public static String chooseEnigmaFromSeed(String seed) + { + return numToMachineType(seed.hashCode() % 13); + } - public static String chooseEnigmaFromSave(BigInteger save) - { - return numToMachineType(getValue(save,20)); - } + public static String chooseEnigmaFromSave(BigInteger save) + { + return numToMachineType(getValue(save, 20)); + } - /** - * Return the name indicator of the enigma machine - * @return name - */ - public String getMachineType() - { - return machineType; - } + /** + * Return the name indicator of the enigma machine + * @return name + */ + public String getMachineType() + { + return machineType; + } - /** - * - * @param s source - * @param d domain (max value) of the value - * @return value - */ - public static int getValue(BigInteger s, int d) - { - BigInteger o = s.mod(BigInteger.valueOf(d)).add(BigInteger.valueOf(d)).mod(BigInteger.valueOf(d)); - return Integer.valueOf(o.toString()); - } + /** + * + * @param s source + * @param d domain (max value) of the value + * @return value + */ + public static int getValue(BigInteger s, int d) + { + BigInteger o = s.mod(BigInteger.valueOf(d)).add(BigInteger.valueOf(d)).mod(BigInteger.valueOf(d)); + return Integer.valueOf(o.toString()); + } - /** - * remove a digit of domain d from source s - * @param s source - * @param d domain (max value) - * @return trimmed source - */ - public static BigInteger removeDigit(BigInteger s, int d) - { - s = s.subtract(s.mod(BigInteger.valueOf(d))); - s = s.divide(BigInteger.valueOf(d)); - return s; - } + /** + * remove a digit of domain d from source s + * @param s source + * @param d domain (max value) + * @return trimmed source + */ + public static BigInteger removeDigit(BigInteger s, int d) + { + s = s.subtract(s.mod(BigInteger.valueOf(d))); + s = s.divide(BigInteger.valueOf(d)); + return s; + } - /** - * - * @param s source - * @param b base (max value) - * @param v actual value - * @return lengthened source - */ - public static BigInteger addDigit(BigInteger s, int v, int b) - { - s = s.multiply(BigInteger.valueOf(b)); - s = s.add(BigInteger.valueOf(v % b)); - return s; - } + /** + * + * @param s source + * @param b base (max value) + * @param v actual value + * @return lengthened source + */ + public static BigInteger addDigit(BigInteger s, int v, int b) + { + 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 c513df3..9d2ccf9 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 @@ -31,182 +31,191 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_D extends Enigma { - protected EntryWheel entryWheel; - protected Rotor rotor1; - protected Rotor rotor2; - protected Rotor rotor3; - protected Reflector reflector; + protected EntryWheel entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; + protected Reflector reflector; - public Enigma_D() - { - super(); - machineType = "D"; - Log.d(MainActivity.APP_ID, "Created Enigma D"); - } + public Enigma_D() + { + super(); + machineType = "D"; + Log.d(MainActivity.APP_ID, "Created Enigma D"); + } - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + @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)); + 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()); - } + addAvailableReflector(new Reflector.ReflectorEnigma_D_G31()); + } - @Override - public void initialize() - { - 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 initialize() + { + 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) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + @Override + public void nextState() + { + rotor1.rotate(); + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) + { + rotor2.rotate(); + this.doAnomaly = rotor2.doubleTurnAnomaly(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - @Override - protected void generateState() { - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); + @Override + protected void generateState() { + int rot1 = rand.nextInt(26); + int rot2 = rand.nextInt(26); + int rot3 = rand.nextInt(26); + int rotRef = rand.nextInt(26); + int ring1 = rand.nextInt(26); + int ring2 = rand.nextInt(26); + int ring3 = rand.nextInt(26); + int ringRef = rand.nextInt(26); - this.rotor1.setRotation(rot1).setRingSetting(ring1); - this.rotor2.setRotation(rot2).setRingSetting(ring2); - this.rotor3.setRotation(rot3).setRingSetting(ring3); + this.rotor1.setRotation(rot1).setRingSetting(ring1); + this.rotor2.setRotation(rot2).setRingSetting(ring2); + this.rotor3.setRotation(rot3).setRingSetting(ring3); - this.reflector.setRotation(rotRef).setRingSetting(ringRef) - .setConfiguration(Plugboard.seedToReflectorConfiguration(rand)); - } + this.reflector.setRotation(rotRef).setRingSetting(ringRef) + .setConfiguration(Plugboard.seedToReflectorConfiguration(rand)); + } - @Override - public char encryptChar(char k) - { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - 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()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - 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); - return (char) (x + 65); //Add Offset again, cast back to char and return - } + @Override + public char encryptChar(char k) + { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + 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()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + 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); + return (char) (x + 65); //Add Offset again, cast back to char and return + } - @Override - public void setState(EnigmaStateBundle state) - { - 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 + public void setState(EnigmaStateBundle state) + { + 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 - public EnigmaStateBundle getState() - { - EnigmaStateBundle state = new EnigmaStateBundle(); + @Override + public EnigmaStateBundle getState() + { + EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getIndex()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); - state.setConfigurationReflector(reflector.getConfiguration()); + state.setTypeReflector(reflector.getIndex()); + state.setRotationReflector(reflector.getRotation()); + state.setRingSettingReflector(reflector.getRingSetting()); + state.setConfigurationReflector(reflector.getConfiguration()); - return state; - } + return state; + } - @Override - public void restoreState(BigInteger s) - { - 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); + @Override + public void restoreState(BigInteger s, int protocol_version) + { + switch(protocol_version) + { + case 1: + 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 = 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); - } + 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); + break; - @Override - public String stateToString() { - 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); - 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, 6, 20); //Machine #6 - return s.toString(16); - } + default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); + } + } + + @Override + public BigInteger getEncodedState(int protocol_version) { + 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); + 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, 6, 20); //Machine #6 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + return s; + } } 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 e78ceee..65aa989 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 @@ -30,41 +30,42 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_G260 extends Enigma_G31 { - public Enigma_G260() - { - super(); - machineType = "G260"; - Log.d(MainActivity.APP_ID, "Created Enigma G260"); - } + public Enigma_G260() + { + super(); + machineType = "G260"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 + 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() - { - 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); + @Override + public BigInteger getEncodedState(int protocol_version) + { + 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, 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 + s = addDigit(s, 5, 20); //Machine #5 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - return s.toString(16); - } + return s; + } } 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 e751e4f..c8578f9 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 @@ -30,194 +30,202 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_G31 extends Enigma { - protected EntryWheel entryWheel; - protected Rotor rotor1; - protected Rotor rotor2; - protected Rotor rotor3; - protected Reflector reflector; + protected EntryWheel entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; + protected Reflector reflector; - public Enigma_G31() - { - super(); - machineType = "G31"; - Log.d(MainActivity.APP_ID, "Created Enigma G31"); - } + public Enigma_G31() + { + super(); + machineType = "G31"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 + 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_G31()); + } - @Override - public void initialize() - { - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(0); - this.rotor2 = getRotor(1); - this.rotor3 = getRotor(2); - this.reflector = getReflector(0); - } + @Override + public void initialize() + { + 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()) - { - rotor2.rotate(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - if(rotor3.isAtTurnoverPosition()) - { - reflector.setRotation(reflector.getRotation()+1); - } - } - } - } + @Override + public void nextState() + { + rotor1.rotate(); + if (rotor1.isAtTurnoverPosition()) + { + rotor2.rotate(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + if(rotor3.isAtTurnoverPosition()) + { + reflector.setRotation(reflector.getRotation()+1); + } + } + } + } - protected void generateState() - { - int r1, r2=-1, r3; - r1 = rand.nextInt(3); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); - r3 = 3 - r1 - r2; + protected void generateState() + { + 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); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); + int rot1 = rand.nextInt(26); + int rot2 = rand.nextInt(26); + int rot3 = rand.nextInt(26); + int rotRef = rand.nextInt(26); + int ring1 = rand.nextInt(26); + int ring2 = rand.nextInt(26); + int ring3 = rand.nextInt(26); + int ringRef = rand.nextInt(26); - 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); - } + 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 char encryptChar(char k) { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - 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()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - 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); - return (char) (x + 65); //Add Offset again, cast back to char and return - } + @Override + public char encryptChar(char k) { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + 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()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + 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); + return (char) (x + 65); //Add Offset again, cast back to char and return + } - @Override - public void setState(EnigmaStateBundle state) - { - 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 void setState(EnigmaStateBundle state) + { + 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(); + @Override + public EnigmaStateBundle getState() { + EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getIndex()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); + state.setTypeReflector(reflector.getIndex()); + state.setRotationReflector(reflector.getRotation()); + state.setRingSettingReflector(reflector.getRingSetting()); - return state; - } + return state; + } - @Override - public void restoreState(BigInteger s) - { - 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()); + @Override + public void restoreState(BigInteger s, int protocol_version) + { + switch (protocol_version) + { + case 1: + 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); - 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); - } + 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); + break; - @Override - public String stateToString() - { - 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); + default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); + } + } - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); + @Override + public BigInteger getEncodedState(int protocol_version) + { + 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, 3, 20); //Machine #3 - return s.toString(16); - } + 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 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + return s; + } } 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 d820b17..55ee967 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 @@ -30,41 +30,42 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_G312 extends Enigma_G31 { - public Enigma_G312() - { - super(); - machineType = "G312"; - Log.d(MainActivity.APP_ID, "Created Enigma G312"); - } + public Enigma_G312() + { + super(); + machineType = "G312"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 + 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() - { - 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); + @Override + public BigInteger getEncodedState(int protocol_version) + { + 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, 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 + s = addDigit(s, 4, 20); //Machine #4 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - return s.toString(16); - } + return s; + } } 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 ba65ffc..79aa424 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 @@ -30,200 +30,210 @@ 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 EntryWheel entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; + protected Reflector reflector; - protected Plugboard plugboard; + protected Plugboard plugboard; - public Enigma_I() - { - super(); - machineType = "I"; - Log.d(MainActivity.APP_ID, "Created Enigma I"); - } + public Enigma_I() + { + super(); + machineType = "I"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 + 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.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 initialize() + { + this.plugboard= new Plugboard(); + 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) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + @Override + public void nextState() + { + rotor1.rotate(); + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) + { + rotor2.rotate(); + this.doAnomaly = rotor2.doubleTurnAnomaly(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - @Override - protected void generateState() { - 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); + @Override + protected void generateState() { + 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); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); + int rot1 = rand.nextInt(26); + int rot2 = rand.nextInt(26); + int rot3 = rand.nextInt(26); + int ring1 = rand.nextInt(26); + int ring2 = rand.nextInt(26); + int ring3 = rand.nextInt(26); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(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)); - } + this.plugboard = new Plugboard(); + plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); + } - @Override - public char encryptChar(char k) - { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //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()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - 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 - } + @Override + public char encryptChar(char k) + { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //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()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + 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 + } - @Override - public void setState(EnigmaStateBundle state) - { - plugboard.setConfiguration(state.getConfigurationPlugboard()); - 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 + public void setState(EnigmaStateBundle state) + { + plugboard.setConfiguration(state.getConfigurationPlugboard()); + 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 - public EnigmaStateBundle getState() - { - EnigmaStateBundle state = new EnigmaStateBundle(); + @Override + public EnigmaStateBundle getState() + { + EnigmaStateBundle state = new EnigmaStateBundle(); - state.setConfigurationPlugboard(plugboard.getConfiguration()); + state.setConfigurationPlugboard(plugboard.getConfiguration()); - state.setTypeEntryWheel(entryWheel.getIndex()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); - state.setTypeReflector(reflector.getIndex()); + state.setTypeReflector(reflector.getIndex()); - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); - return state; - } + return state; + } - @Override - public void restoreState(BigInteger s) - { - 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); - 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); + @Override + public void restoreState(BigInteger s, int protocol_version) + { + switch (protocol_version) + { + case 1: + 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); + 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); - 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.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(s); - } + this.plugboard = new Plugboard(); + plugboard.setConfiguration(s); + break; - @Override - public String stateToString() { - 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, 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 + default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); + } + } - return s.toString(16); - } + @Override + public BigInteger getEncodedState(int protocol_version) { + 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, 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 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + + return s; + } } 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 adfd0b5..99d4032 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 @@ -30,189 +30,199 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_K extends Enigma { - protected EntryWheel entryWheel; - protected Rotor rotor1; - protected Rotor rotor2; - protected Rotor rotor3; - protected Reflector reflector; + protected EntryWheel entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; + protected Reflector reflector; - public Enigma_K() - { - super(); - machineType = "K"; - Log.d(MainActivity.APP_ID, "Created Enigma K"); - } + public Enigma_K() + { + super(); + machineType = "K"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 + 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 = 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 initialize() + { + 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) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + @Override + public void nextState() + { + rotor1.rotate(); + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) + { + rotor2.rotate(); + this.doAnomaly = rotor2.doubleTurnAnomaly(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - @Override - protected void generateState() { - int r1, r2=-1, r3; - r1 = rand.nextInt(3); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); - r3 = 3 - r1 - r2; + @Override + protected void generateState() { + 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); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); + int rot1 = rand.nextInt(26); + int rot2 = rand.nextInt(26); + int rot3 = rand.nextInt(26); + int rotRef = rand.nextInt(26); + int ring1 = rand.nextInt(26); + int ring2 = rand.nextInt(26); + int ring3 = rand.nextInt(26); + int ringRef = rand.nextInt(26); - 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); - } + 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 char encryptChar(char k) { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - 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()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - 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); - return (char) (x + 65); //Add Offset again, cast back to char and return - } + @Override + public char encryptChar(char k) { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + 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()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + 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); + return (char) (x + 65); //Add Offset again, cast back to char and return + } - @Override - public void setState(EnigmaStateBundle state) - { - 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 void setState(EnigmaStateBundle state) + { + 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(); + @Override + public EnigmaStateBundle getState() + { + EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getIndex()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); + state.setTypeReflector(reflector.getIndex()); + state.setRotationReflector(reflector.getRotation()); + state.setRingSettingReflector(reflector.getRingSetting()); - return state; - } + return state; + } - @Override - public void restoreState(BigInteger s) - { - 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()); + @Override + public void restoreState(BigInteger s, int protocol_version) + { + switch (protocol_version) + { + case 1: + 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); - 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); - } + 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); + break; - @Override - public String stateToString() - { - 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 + default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); + } + } - return s.toString(16); - } + @Override + public BigInteger getEncodedState(int protocol_version) + { + 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 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + + return s; + } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_KD.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_KD.java new file mode 100644 index 0000000..87c756e --- /dev/null +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_KD.java @@ -0,0 +1,240 @@ +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; + +/** + * Concrete implementation of an enigma machine of name D + * This machine has a rewirable UKW, non changeable rotors. + * Copyright (C) 2015 Paul Schaub + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae + */ +public class Enigma_KD extends Enigma { + + protected EntryWheel entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; + protected Reflector reflector; + + public Enigma_KD() + { + super(); + machineType = "KD"; + Log.d(MainActivity.APP_ID, "Created Enigma KD"); + } + + @Override + protected void establishAvailableParts() + { + addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); //TODO: Really? + + addAvailableRotor(new Rotor.Rotor_KD_I(0, 0)); + addAvailableRotor(new Rotor.Rotor_KD_II(0, 0)); + addAvailableRotor(new Rotor.Rotor_KD_III(0, 0)); + + addAvailableReflector(new Reflector.ReflectorEnigma_KD()); + } + + @Override + public void initialize() + { + 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) + { + rotor2.rotate(); + this.doAnomaly = rotor2.doubleTurnAnomaly(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } + + @Override + protected void generateState() { + 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); + int rot3 = rand.nextInt(26); + int rotRef = rand.nextInt(26); + int ring1 = rand.nextInt(26); + int ring2 = rand.nextInt(26); + int ring3 = rand.nextInt(26); + int ringRef = rand.nextInt(26); + + 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); + this.reflector.setRotation(rotRef).setRingSetting(ringRef) + .setConfiguration(Plugboard.seedToReflectorConfiguration(rand)); + } + + @Override + public char encryptChar(char k) + { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + 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()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + 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); + return (char) (x + 65); //Add Offset again, cast back to char and return + } + + @Override + public void setState(EnigmaStateBundle state) + { + 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 + public EnigmaStateBundle getState() + { + EnigmaStateBundle state = new EnigmaStateBundle(); + + state.setTypeEntryWheel(entryWheel.getIndex()); + + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); + + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); + + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); + + state.setTypeReflector(reflector.getIndex()); + state.setRotationReflector(reflector.getRotation()); + state.setRingSettingReflector(reflector.getRingSetting()); + state.setConfigurationReflector(reflector.getConfiguration()); + + return state; + } + + @Override + public void restoreState(BigInteger s, int protocol_version) + { + switch (protocol_version) + { + case 1: + 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); + s = removeDigit(s, 26); + + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); + this.reflector.setConfiguration(s); + break; + + default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); + } + + } + + @Override + public BigInteger getEncodedState(int protocol_version) { + 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); + 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, 12, 20); //Machine #12 TODO: Reorder? + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + + return s; + } +} 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 a87032c..1dc53dc 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 @@ -30,39 +30,42 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_K_Swiss_Airforce extends Enigma_K { - public Enigma_K_Swiss_Airforce() - { - super(); - machineType = "KSA"; - Log.d(MainActivity.APP_ID, "Created Enigma KSA"); - } + public Enigma_K_Swiss_Airforce() + { + super(); + machineType = "KSA"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 + 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() - { - 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 + @Override + public BigInteger getEncodedState(int protocol_version) + { + 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); - return s.toString(16); - } + 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 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + + return s; + } } 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 5996921..db585b2 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 @@ -30,40 +30,43 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_K_Swiss_Standard extends Enigma_K { - public Enigma_K_Swiss_Standard() - { - super(); - machineType = "KS"; - Log.d(MainActivity.APP_ID, "Created Enigma KS"); - } + public Enigma_K_Swiss_Standard() + { + super(); + machineType = "KS"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 + 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() - { - 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 + @Override + public BigInteger getEncodedState(int protocol_version) + { + 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); - return s.toString(16); - } + 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 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + + return s; + } } 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 3ce7d59..b705cde 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 @@ -30,69 +30,70 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_M3 extends Enigma_I { - public Enigma_M3() - { - super(); - machineType = "M3"; - Log.d(MainActivity.APP_ID, "Created Enigma M3"); - } + public Enigma_M3() + { + super(); + machineType = "M3"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 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 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); + @Override + protected void generateState() { + 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); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); + int rot1 = rand.nextInt(26); + int rot2 = rand.nextInt(26); + int rot3 = rand.nextInt(26); + int ring1 = rand.nextInt(26); + int ring2 = rand.nextInt(26); + int ring3 = rand.nextInt(26); - 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.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)); - } + this.plugboard = new Plugboard(); + plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); + } - @Override - public String stateToString() { - 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, 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 + @Override + public BigInteger getEncodedState(int protocol_version) { + 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, 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 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - return s.toString(16); - } + return s; + } } 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 d3e0c92..0534d5e 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 @@ -31,281 +31,290 @@ 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 ArrayList availableThinRotors; + private EntryWheel entryWheel; + private Rotor rotor1; + private Rotor rotor2; + private Rotor rotor3; - private Rotor rotor4; + private Rotor rotor4; - private Reflector reflector; + private Reflector reflector; - private Plugboard plugboard; + private Plugboard plugboard; - public Enigma_M4() - { - super(); - machineType = "M4"; - Log.d(MainActivity.APP_ID, "Created Enigma M4"); - } + public Enigma_M4() + { + super(); + machineType = "M4"; + Log.d(MainActivity.APP_ID, "Created Enigma M4"); + } - protected void addAvailableThinRotor(Rotor r) - { - if(availableThinRotors == null) availableThinRotors = new ArrayList<>(); - availableThinRotors.add(availableThinRotors.size(), r.setIndex(availableThinRotors.size())); - } + 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) + { + 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); - } + 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 + 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.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 + public void initialize() + { + Log.d(MainActivity.APP_ID, "Initialized"); + this.plugboard = new Plugboard(); + 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 - /** - * Set the enigma into the next mechanical state. - * This rotates the first rotor and eventually also the second/third. - * Also this method handles the anomaly in case it should happen. - */ - public void nextState() - { - //Rotate rotors - rotor1.rotate(); - //Eventually turn next rotor (usual turnOver or anomaly) - if (rotor1.isAtTurnoverPosition() || this.doAnomaly) - { - rotor2.rotate(); - //Set doAnomaly for next call of encryptChar - this.doAnomaly = rotor2.doubleTurnAnomaly(); - //Eventually rotate next rotor - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + @Override + /** + * Set the enigma into the next mechanical state. + * This rotates the first rotor and eventually also the second/third. + * Also this method handles the anomaly in case it should happen. + */ + public void nextState() + { + //Rotate rotors + rotor1.rotate(); + //Eventually turn next rotor (usual turnOver or anomaly) + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) + { + rotor2.rotate(); + //Set doAnomaly for next call of encryptChar + this.doAnomaly = rotor2.doubleTurnAnomaly(); + //Eventually rotate next rotor + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - @Override - protected void generateState() { - int r1, r2=-1, r3=-1; - int r4; - int ref; - r1 = rand.nextInt(8); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(8); - while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(8); - r4 = rand.nextInt(2); - ref = rand.nextInt(2); + @Override + protected void generateState() { + int r1, r2=-1, r3=-1; + int r4; + int ref; + r1 = rand.nextInt(8); + while(r2 == -1 || r2 == r1) r2 = rand.nextInt(8); + while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(8); + r4 = rand.nextInt(2); + ref = rand.nextInt(2); - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rot4 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ring4 = rand.nextInt(26); - int ringRef = rand.nextInt(26); + int rot1 = rand.nextInt(26); + int rot2 = rand.nextInt(26); + int rot3 = rand.nextInt(26); + int rot4 = rand.nextInt(26); + int rotRef = rand.nextInt(26); + int ring1 = rand.nextInt(26); + int ring2 = rand.nextInt(26); + int ring3 = rand.nextInt(26); + int ring4 = rand.nextInt(26); + int ringRef = rand.nextInt(26); - 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.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 = getReflector(ref, rotRef, ringRef); + this.reflector = getReflector(ref, rotRef, ringRef); - this.plugboard = new Plugboard(); - this.plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); - } + this.plugboard = new Plugboard(); + this.plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); + } - @Override - /** - * Substitute char k by sending the signal through the enigma. - * The signal passes the plugboard, the rotors and returns back after going through the - * reflector wheel. - * - * @param k input char - * @return substituted output char - */ - public char encryptChar(char k) - { - nextState(); //Rotate rotors - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //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()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + rotor4.getRotation() - rotor4.getRingSetting()); - x = rotor4.encryptForward(x); - x = rotor1.normalize(x - rotor4.getRotation() + rotor4.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor4.getRotation() - rotor4.getRingSetting()); - x = rotor4.encryptBackward(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - rotor4.getRotation() + rotor4.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - 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 - } + @Override + /** + * Substitute char k by sending the signal through the enigma. + * The signal passes the plugboard, the rotors and returns back after going through the + * reflector wheel. + * + * @param k input char + * @return substituted output char + */ + public char encryptChar(char k) + { + nextState(); //Rotate rotors + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //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()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + rotor4.getRotation() - rotor4.getRingSetting()); + x = rotor4.encryptForward(x); + x = rotor1.normalize(x - rotor4.getRotation() + rotor4.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor4.getRotation() - rotor4.getRingSetting()); + x = rotor4.encryptBackward(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - rotor4.getRotation() + rotor4.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + 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 + } - @Override - public void setState(EnigmaStateBundle state) - { - 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()); + @Override + public void setState(EnigmaStateBundle state) + { + 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()); - } + } - @Override - public EnigmaStateBundle getState() - { - EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getIndex()); + @Override + public EnigmaStateBundle getState() + { + EnigmaStateBundle state = new EnigmaStateBundle(); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); - state.setTypeRotor4(rotor4.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()); - state.setRotationRotor3(rotor3.getRotation()); - state.setRotationRotor4(rotor4.getRotation()); + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); + state.setRotationRotor4(rotor4.getRotation()); - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setRingSettingRotor4(rotor4.getRingSetting()); + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); + state.setRingSettingRotor4(rotor4.getRingSetting()); - state.setTypeReflector(reflector.getIndex()); + state.setTypeReflector(reflector.getIndex()); - state.setConfigurationPlugboard(plugboard.getConfiguration()); + state.setConfigurationPlugboard(plugboard.getConfiguration()); - return state; - } + return state; + } - @Override - public void restoreState(BigInteger s) - { - 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()); + @Override + public void restoreState(BigInteger s, int protocol_version) + { + switch (protocol_version) + { + case 1: + 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); - 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 rot4 = getValue(s, 26); - s = removeDigit(s,26); - int ring4 = 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); + 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 rot4 = getValue(s, 26); + s = removeDigit(s,26); + int ring4 = 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 = 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(s); - } + 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(s); + break; - @Override - public String stateToString() { - 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); - 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); + default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); + } + } - 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()); + @Override + public BigInteger getEncodedState(int protocol_version) { + 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); + 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, 2, 20); - return s.toString(16); - } + 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); + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + + return s; + } } 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 de370f5..8741319 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 @@ -30,191 +30,203 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; */ public class Enigma_R extends Enigma { - protected EntryWheel entryWheel; - protected Rotor rotor1; - protected Rotor rotor2; - protected Rotor rotor3; + protected EntryWheel entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; - protected Reflector reflector; + protected Reflector reflector; - public Enigma_R() - { - super(); - machineType = "R"; - Log.d(MainActivity.APP_ID, "Created Enigma R"); - } + public Enigma_R() + { + super(); + machineType = "R"; + Log.d(MainActivity.APP_ID, "Created Enigma 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 + 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 = getEntryWheel(0); - this.rotor1 = getRotor(0); - this.rotor2 = getRotor(1); - this.rotor3 = getRotor(2); - this.reflector = getReflector(0); - } + @Override + public void initialize() + { + 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) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + @Override + public void nextState() + { + rotor1.rotate(); + if (rotor1.isAtTurnoverPosition() || this.doAnomaly) + { + rotor2.rotate(); + this.doAnomaly = rotor2.doubleTurnAnomaly(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - @Override - protected void generateState() - { - int r1, r2=-1, r3; - r1 = rand.nextInt(3); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); - r3 = 3 - r1 - r2; + @Override + protected void generateState() + { + 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); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); + int rot1 = rand.nextInt(26); + int rot2 = rand.nextInt(26); + int rot3 = rand.nextInt(26); + int rotRef = rand.nextInt(26); + int ring1 = rand.nextInt(26); + int ring2 = rand.nextInt(26); + int ring3 = rand.nextInt(26); + int ringRef = rand.nextInt(26); - 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); - } + 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 char encryptChar(char k) { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - 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()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - 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); - return (char) (x + 65); //Add Offset again, cast back to char and return - } + @Override + public char encryptChar(char k) { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + 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()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + 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); + return (char) (x + 65); //Add Offset again, cast back to char and return + } - @Override - public void setState(EnigmaStateBundle state) - { - 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 void setState(EnigmaStateBundle state) + { + 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(); + @Override + public EnigmaStateBundle getState() { + EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getIndex()); + state.setTypeEntryWheel(entryWheel.getIndex()); - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); + state.setTypeRotor1(rotor1.getIndex()); + state.setTypeRotor2(rotor2.getIndex()); + state.setTypeRotor3(rotor3.getIndex()); - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); + state.setTypeReflector(reflector.getIndex()); + state.setRotationReflector(reflector.getRotation()); + state.setRingSettingReflector(reflector.getRingSetting()); - return state; - } + return state; + } - @Override - public void restoreState(BigInteger s) - { - 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()); + @Override + public void restoreState(BigInteger s, int protocol_version) + { + switch (protocol_version) + { + case 1: + 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); - s = removeDigit(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.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); - } + 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); + break; - @Override - public String stateToString() - { - BigInteger t = BigInteger.valueOf(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.getIndex(), availableRotors.size()); - t = addDigit(t, rotor2.getIndex(), availableRotors.size()); - t = addDigit(t, rotor1.getIndex(), availableRotors.size()); - t = addDigit(t, 10, 20); //Machine #10 + default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); + } - return t.toString(16); - } + } + + @Override + public BigInteger getEncodedState(int protocol_version) + { + 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, 10, 20); //Machine #10 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + + return s; + } } 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 a73b5f7..1059c7f 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 @@ -166,53 +166,64 @@ public class Enigma_T extends Enigma } @Override - public void restoreState(BigInteger s) + public void restoreState(BigInteger s, int protocol_version) { - 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()); + switch (protocol_version) + { + case 1: + 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); + + this.rotor1 = getRotor(r1, rot1, ring1); + this.rotor2 = getRotor(r2, rot2, ring2); + this.rotor3 = getRotor(r3, rot3, ring3); + this.reflector = getReflector(0, rotRef, ringRef); + break; + + default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); + } - 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() + public BigInteger getEncodedState(int protocol_version) { - BigInteger t = BigInteger.valueOf(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.getIndex(), availableRotors.size()); - t = addDigit(t, rotor2.getIndex(), availableRotors.size()); - t = addDigit(t, rotor1.getIndex(), availableRotors.size()); - t = addDigit(t, 11, 20); //Machine #11 + 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); - return t.toString(16); + s = addDigit(s, rotor3.getIndex(), availableRotors.size()); + s = addDigit(s, rotor2.getIndex(), availableRotors.size()); + s = addDigit(s, rotor1.getIndex(), availableRotors.size()); + + s = addDigit(s, 11, 20); //Machine #11 + s = addDigit(s, protocol_version, MainActivity.max_protocol_version); + + return s; } } 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 de4e454..5fc2248 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java @@ -87,7 +87,7 @@ public class Plugboard public static int[] stringToConfiguration(String in) { String pairs = trimString(new InputPreparer.RemoveIllegalCharacters().prepareString(in)); - int[] out = empty; + int[] out = Arrays.copyOf(empty, empty.length); //Check if in is too long or odd int l = pairs.length(); if(l>1 && (pairs.length() > 26 || pairs.length()/2 == (pairs.length()-1)/2)) diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java index 19dd470..bb6059f 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java @@ -3,6 +3,7 @@ package de.vanitasvitae.enigmandroid.enigma.rotors; import android.util.Log; import java.math.BigInteger; +import java.util.Arrays; import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.enigma.Enigma; @@ -31,310 +32,325 @@ import de.vanitasvitae.enigmandroid.enigma.Plugboard; */ public class Reflector { - protected int type; - protected String name; - protected int index; - protected String summary; - protected int[] connections; - protected int rotation; - protected int ringSetting; + protected int type; + protected String name; + protected int index; + protected String summary; + protected int[] connections; + protected int rotation; + protected int ringSetting; - /** - * This constructor is not accessible from outside this class file. - * Use the one of the createReflector* methods instead to create concrete Reflectors from - * outside this class file - * @param type name indicator of the reflector - * @param connections wiring of the reflector as Integer array - */ - protected Reflector(int type, String name, String summary, int[] connections) - { - this.type = type; - this.name = name; - this.summary = summary; - this.connections = connections; - } + /** + * This constructor is not accessible from outside this class file. + * Use the one of the createReflector* methods instead to create concrete Reflectors from + * outside this class file + * @param type name indicator of the reflector + * @param connections wiring of the reflector as Integer array + */ + protected Reflector(int type, String name, String summary, int[] connections) + { + this.type = type; + this.name = name; + this.summary = summary; + this.connections = connections; + } - public Reflector getInstance() - { - //noinspection ConstantConditions - return createReflector(this.type).setIndex(this.getIndex()); - } + public Reflector getInstance() + { + //noinspection ConstantConditions + return createReflector(this.type).setIndex(this.getIndex()); + } - public Reflector getInstance(int rotation, int ringSetting) - { - //noinspection ConstantConditions - return createReflector(this.type).setIndex(this.getIndex()) - .setRotation(rotation).setRingSetting(ringSetting); - } + public Reflector getInstance(int rotation, int ringSetting) + { + //noinspection ConstantConditions + return createReflector(this.type).setIndex(this.getIndex()) + .setRotation(rotation).setRingSetting(ringSetting); + } - public Reflector setIndex(int index) - { - this.index = index; - return this; - } + public Reflector setIndex(int index) + { + this.index = index; + return this; + } - public int getIndex() - { - return this.index; - } + public int getIndex() + { + return this.index; + } - public int getRotation() - { - return rotation; - } + public int getRotation() + { + return rotation; + } - public int getRingSetting() - { - return ringSetting; - } + public int getRingSetting() + { + return ringSetting; + } - public Reflector setRotation(int rotation) - { - this.rotation = rotation; - return this; - } + public Reflector setRotation(int rotation) + { + this.rotation = rotation; + return this; + } - public Reflector setRingSetting(int ringSetting) - { - this.ringSetting = ringSetting; - return this; - } + public Reflector setRingSetting(int ringSetting) + { + this.ringSetting = ringSetting; + return this; + } - public Reflector setConfiguration(int[] c) - { - this.connections = c; - return this; - } + public Reflector setConfiguration(int[] c) + { + this.connections = c; + return this; + } - public BigInteger setConfiguration(BigInteger b) - { - String s = ""; + 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(Plugboard.stringToConfiguration(s)); - return b; - } + 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(Plugboard.stringToConfiguration(s)); + return b; + } - public int[] getConfiguration() - { - return connections; - } + public int[] getConfiguration() + { + return connections; + } - /** - * Factory method to create reflectors. - * @param type name of the created reflector - * 1 -> ReflectorA - * 2 -> ReflectorB - * 3 -> ReflectorC - * 4 -> ReflectorThinB - * 5 -> ReflectorThinC - * 6 -> ReflectorEnigma_D_KD_G31 - * 7 -> Reflector_K - * 8 -> Reflector_T - * 9 -> Reflector_G312 - * 10 -> Reflector_G260 - * 11 -> Reflector_R - * default -> ReflectorB - * @return Reflector - */ - public static Reflector createReflector(int type) - { - switch (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(); + /** + * Factory method to create reflectors. + * @param type name of the created reflector + * 1 -> ReflectorA + * 2 -> ReflectorB + * 3 -> ReflectorC + * 4 -> ReflectorThinB + * 5 -> ReflectorThinC + * 6 -> ReflectorEnigma_D_KD_G31 + * 7 -> Reflector_K + * 8 -> Reflector_T + * 9 -> Reflector_G312 + * 10 -> Reflector_G260 + * 11 -> Reflector_R + * default -> ReflectorB + * @return Reflector + */ + public static Reflector createReflector(int type) + { + switch (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_G31(); + case 21: return new ReflectorEnigma_KD(); + 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 name "+type); - return null; - } - } + default: + Log.e(MainActivity.APP_ID," Tried to create Reflector of invalid name "+type); + return null; + } + } - /** - * 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 - * due to the electronic implementation of the historical enigma machine. - * @param input input signal - * @return encrypted (substituted) output - */ - public int encrypt(int input) - { - return this.connections[normalize(input)]; - } + /** + * 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 + * due to the electronic implementation of the historical enigma machine. + * @param input input signal + * @return encrypted (substituted) output + */ + public int encrypt(int input) + { + return this.connections[normalize(input)]; + } - /** - * Return the size (ie the number of wires/length of the connections array) of the reflector - * @return size - */ - private int getRotorSize() - { - return this.connections.length; - } + /** + * Return the size (ie the number of wires/length of the connections array) of the reflector + * @return size + */ + private int getRotorSize() + { + return this.connections.length; + } - /** - * Normalize the input. - * Normalizing means keeping the input via modulo in the range from 0 to n-1, where n is equal - * to the size of the reflector. This is necessary since java allows negative modulo values, - * which can break this implementation - * @param input input signal - * @return "normalized" input signal - */ - private int normalize(int input) - { - return (input + this.getRotorSize()) % this.getRotorSize(); - } + /** + * Normalize the input. + * Normalizing means keeping the input via modulo in the range from 0 to n-1, where n is equal + * to the size of the reflector. This is necessary since java allows negative modulo values, + * which can break this implementation + * @param input input signal + * @return "normalized" input signal + */ + private int normalize(int input) + { + return (input + this.getRotorSize()) % this.getRotorSize(); + } - /** - * Concrete implementation of ReflectorA - * Used in Enigma I - * AE BJ CM DZ FL GY HX IV KW NR OQ PU ST - */ - public static class Reflector_A extends Reflector - { - public Reflector_A() - { - 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}); - } - } + /** + * Concrete implementation of ReflectorA + * Used in Enigma I + * AE BJ CM DZ FL GY HX IV KW NR OQ PU ST + */ + public static class Reflector_A extends Reflector + { + public Reflector_A() + { + 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}); + } + } - /** - * Concrete implementation of ReflectorB - * Used in Enigma I, M3 - * AY BR CU DH EQ FS GL IP JX KN MO TZ VW - */ - public static class Reflector_B extends Reflector - { - public Reflector_B() - { - 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}); - } - } + /** + * Concrete implementation of ReflectorB + * Used in Enigma I, M3 + * AY BR CU DH EQ FS GL IP JX KN MO TZ VW + */ + public static class Reflector_B extends Reflector + { + public Reflector_B() + { + 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}); + } + } - /** - * Concrete implementation of ReflectorC - * Used in Enigma I, M3 - * AF BV CP DJ EI GO HY KR LZ MX NW QT SU - */ - public static class Reflector_C extends Reflector - { - public Reflector_C() - { - 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 ReflectorC + * Used in Enigma I, M3 + * AF BV CP DJ EI GO HY KR LZ MX NW QT SU + */ + public static class Reflector_C extends Reflector + { + public Reflector_C() + { + 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 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 - */ - public static class Reflector_Thin_B extends Reflector - { - public Reflector_Thin_B() - { - 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 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 + */ + public static class Reflector_Thin_B extends Reflector + { + public Reflector_Thin_B() + { + 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 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 - */ - public static class ReflectorThinC extends Reflector - { - public ReflectorThinC() - { - 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}); - } - } + /** + * 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 + */ + public static class ReflectorThinC extends Reflector + { + public ReflectorThinC() + { + 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 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(20, "Ref-D", "Default: IMETCGFRAYSQBZXWLHKDVUPOJN", defaultWiring_D_KD_G31); - } - } + /** + * Pluggable Reflector of the Enigma machine of name D and G31 + * 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_G31 extends Reflector + { + public static final int[] defaultWiring_D_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_G31() + { + super(20, "Ref-D", "Default: IMETCGFRAYSQBZXWLHKDVUPOJN", Arrays.copyOf(defaultWiring_D_G31,defaultWiring_D_G31.length)); + } + } - /** - * 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 - */ - public static class Reflector_G312 extends Reflector - { - public Reflector_G312() - { - 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}); - } - } + /** + * Pluggable Reflector as used in the Enigma of Type KD + * Standard wiring: KOTVPNLMJIAGHFBEWYXCZDQSRU + * Has additional ringSetting and can rotate + */ + public static class ReflectorEnigma_KD extends Reflector + { + public static final int[] defaultWiring_KD = {10,14,19,21,15,13,11,12,9,8,0,6,7,5,1,4,22,24,23,2,25,3,16,18,17,20}; + public ReflectorEnigma_KD() + { + super(21, "Ref-KD", "Default: KOTVPNLMJIAGHFBEWYXCZDQSRU", Arrays.copyOf(defaultWiring_KD, defaultWiring_KD.length)); + } + } - /** - * 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 - */ - public static class Reflector_K_G260 extends Reflector - { - public Reflector_K_G260() - { - 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 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 + */ + public static class Reflector_G312 extends Reflector + { + public Reflector_G312() + { + 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 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 - */ - public static class Reflector_R extends Reflector - { - public Reflector_R() - { - 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 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 + */ + public static class Reflector_K_G260 extends Reflector + { + public Reflector_K_G260() + { + 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 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}); - } - } + /** + * 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 + */ + public static class Reflector_R extends Reflector + { + public Reflector_R() + { + 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 5bb4730..0d2c47b 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 @@ -140,26 +140,30 @@ public abstract class Rotor case 51: return new Rotor_K_D_II(rotation, ringSetting); case 52: return new Rotor_K_D_III(rotation, ringSetting); - 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); + case 60: return new Rotor_KD_I(rotation, ringSetting); + case 61: return new Rotor_KD_II(rotation, ringSetting); + case 62: return new Rotor_KD_III(rotation, ringSetting); - 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); + case 70: return new Rotor_KSwiss_Standard_I(rotation, ringSetting); + case 71: return new Rotor_KSwiss_Standard_II(rotation, ringSetting); + case 72: return new Rotor_KSwiss_Standard_III(rotation, ringSetting); - 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); + case 80: return new Rotor_K_Swiss_Airforce_I(rotation, ringSetting); + case 81: return new Rotor_K_Swiss_Airforce_II(rotation, ringSetting); + case 82: return new Rotor_K_Swiss_Airforce_III(rotation, ringSetting); - 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); + case 90: return new Rotor_R_I(rotation, ringSetting); + case 91: return new Rotor_R_II(rotation, ringSetting); + case 92: return new Rotor_R_III(rotation, ringSetting); + + case 100: return new Rotor_T_I(rotation, ringSetting); + case 101: return new Rotor_T_II(rotation, ringSetting); + case 102: return new Rotor_T_III(rotation, ringSetting); + case 103: return new Rotor_T_IV(rotation, ringSetting); + case 104: return new Rotor_T_V(rotation, ringSetting); + case 105: return new Rotor_T_VI(rotation, ringSetting); + case 106: return new Rotor_T_VII(rotation, ringSetting); + case 107: return new Rotor_T_VIII(rotation, ringSetting); default: Log.e(MainActivity.APP_ID," Tried to create Rotor of invalid name "+type); return null; @@ -707,6 +711,54 @@ public abstract class Rotor } } + /** + * Rotor I as used in the Enigma Type KD + * VEZIOJCXKYDUNTWAPLQGBHSFMR + * Turnover TVZBFIMOR + */ + public static class Rotor_KD_I extends Rotor + { + public Rotor_KD_I(int rotation, int ringSetting) + { + super(60, "KD-I", "VEZIOJCXKYDUNTWAPLQGBHSFMR", + new Integer[]{21,4,25,8,14,9,2,23,10,24,3,20,13,19,22,0,15,11,16,6,1,7,18,5,12,17}, + new Integer[]{15,20,6,10,1,23,19,21,3,5,8,17,24,12,4,16,18,25,22,13,11,0,14,7,9,2}, + new Integer[]{19,21,25,1,5,8,12,14,17}, ringSetting, rotation); + } + } + + /** + * Rotor II as used in the Enigma Type KD + * HGRBSJZETDLVPMQYCXAOKINFUW + * Turnover TVZBFIMOR + */ + public static class Rotor_KD_II extends Rotor + { + public Rotor_KD_II(int rotation, int ringSetting) + { + super(61, "KD-II", "HGRBSJZETDLVPMQYCXAOKINFUW", + new Integer[]{7,6,17,1,18,9,25,4,19,3,11,21,15,12,16,24,2,23,0,14,10,8,13,5,20,22}, + new Integer[]{18,3,16,9,7,23,1,0,21,5,20,10,13,22,19,12,14,2,4,8,24,11,25,17,15,6}, + new Integer[]{19,21,25,1,5,8,12,14,17}, ringSetting, rotation); + } + } + + /** + * Rotor III as used in the Enigma Type KD + * NWLHXGRBYOJSAZDVTPKFQMEUIC + * Turnover TVZBFIMOR + */ + public static class Rotor_KD_III extends Rotor + { + public Rotor_KD_III(int rotation, int ringSetting) + { + super(62, "KD-II", "NWLHXGRBYOJSAZDVTPKFQMEUIC", + new Integer[]{13,22,11,7,23,6,17,1,24,14,9,18,0,25,3,21,19,15,10,5,16,12,4,20,8,2}, + new Integer[]{12,7,25,14,22,19,5,3,24,10,18,2,21,0,9,17,20,6,11,16,23,15,1,4,8,13}, + new Integer[]{19,21,25,1,5,8,12,14,17}, 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 @@ -716,7 +768,7 @@ public abstract class Rotor { public Rotor_KSwiss_Standard_I(int rotation, int ringSetting) { - super(60, "KS-I", "PEZUOHXSCVFMTBGLRINQJWAYDK", + super(70, "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); @@ -732,7 +784,7 @@ public abstract class Rotor { public Rotor_KSwiss_Standard_II(int rotation, int ringSetting) { - super(61, "KS-II", "ZOUESYDKFWPCIQXHMVBLGNJRAT", + super(71, "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); @@ -748,7 +800,7 @@ public abstract class Rotor { public Rotor_KSwiss_Standard_III(int rotation, int ringSetting) { - super(62, "KS-III", "EHRVXGAOBQUSIMZFLYNWKTPDJC", + super(72, "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); @@ -764,7 +816,7 @@ public abstract class Rotor { public Rotor_K_Swiss_Airforce_I(int rotation, int ringSetting) { - super(70, "KSA-I", "PEZUOHXSCVFMTBGLRINQJWAYDK", + super(80, "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); @@ -779,7 +831,7 @@ public abstract class Rotor { public Rotor_K_Swiss_Airforce_II(int rotation, int ringSetting) { - super(71, "KSA-II", "ZOUESYDKFWPCIQXHMVBLGNJRAT", + super(81, "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); @@ -794,7 +846,7 @@ public abstract class Rotor { public Rotor_K_Swiss_Airforce_III(int rotation, int ringSetting) { - super(72, "KSA-III", "EHRVXGAOBQUSIMZFLYNWKTPDJC", + super(82, "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); @@ -810,7 +862,7 @@ public abstract class Rotor { public Rotor_R_I(int rotation, int ringSetting) { - super(80, "R-I", "JGDQOXUSCAMIFRVTPNEWKBLZYH", + super(90, "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); @@ -826,7 +878,7 @@ public abstract class Rotor { public Rotor_R_II(int rotation, int ringSetting) { - super(81, "R-II", "NTZPSFBOKMWRCJDIVLAEYUXHGQ", + super(91, "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); @@ -842,7 +894,7 @@ public abstract class Rotor { public Rotor_R_III(int rotation, int ringSetting) { - super(82, "R-III", "JVIUBHTCDYAKEQZPOSGXNRMWFL", + super(92, "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); @@ -858,7 +910,7 @@ public abstract class Rotor { public Rotor_T_I(int rotation, int ringSetting) { - super(90, "T-I", "KPTYUELOCVGRFQDANJMBSWHZXI", + super(100, "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); @@ -874,7 +926,7 @@ public abstract class Rotor { public Rotor_T_II(int rotation, int ringSetting) { - super(91, "T-II", "UPHZLWEQMTDJXCAKSOIGVBYFNR", + super(101, "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); @@ -889,7 +941,7 @@ public abstract class Rotor public static class Rotor_T_III extends Rotor { public Rotor_T_III(int rotation, int ringSetting) { - super(92, "T-III", "QUDLYRFEKONVZAXWHMGPJBSICT", + super(102, "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); @@ -905,7 +957,7 @@ public abstract class Rotor { public Rotor_T_IV(int rotation, int ringSetting) { - super(93, "T-IV", "CIWTBKXNRESPFLYDAGVHQUOJZM", + super(103, "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); @@ -921,7 +973,7 @@ public abstract class Rotor { public Rotor_T_V(int rotation, int ringSetting) { - super(94, "T-V", "UAXGISNJBVERDYLFZWTPCKOHMQ", + super(104, "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); @@ -937,7 +989,7 @@ public abstract class Rotor { public Rotor_T_VI(int rotation, int ringSetting) { - super(95, "T-VI", "XFUZGALVHCNYSEWQTDMRBKPIOJ", + super(105, "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); @@ -953,7 +1005,7 @@ public abstract class Rotor { public Rotor_T_VII(int rotation, int ringSetting) { - super(96, "T-VII", "BJVFTXPLNAYOZIKWGDQERUCHSM", + super(106, "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); @@ -969,7 +1021,7 @@ public abstract class Rotor { public Rotor_T_VIII(int rotation, int ringSetting) { - super(97, "T-VIII", "YMTPNZHWKODAJXELUQVGCBISFR", + super(107, "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); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java index 86d8e52..037c478 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java @@ -1,5 +1,7 @@ package de.vanitasvitae.enigmandroid.layout; +import android.text.Editable; +import android.text.TextWatcher; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner; @@ -34,139 +36,157 @@ import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer; */ public abstract class LayoutContainer { - protected EditText inputView; - protected EditText outputView; + protected EditText inputView; + protected EditText outputView; - protected EditTextAdapter input; - protected EditTextAdapter output; + protected EditTextAdapter input; + protected EditTextAdapter output; - protected InputPreparer inputPreparer; - protected MainActivity main; + protected InputPreparer inputPreparer; + protected MainActivity main; - public abstract Enigma getEnigma(); - protected abstract void initializeLayout(); - public abstract void resetLayout(); - public abstract void setLayoutState(EnigmaStateBundle state); - public abstract void syncStateFromLayoutToEnigma(); - public void syncStateFromEnigmaToLayout() - { - this.setLayoutState(getEnigma().getState()); - } - public abstract void showRingSettingsDialog(); + public abstract Enigma getEnigma(); + protected abstract void assembleLayout(); + public abstract void resetLayout(); + public abstract void setLayoutState(EnigmaStateBundle state); + public abstract void syncStateFromLayoutToEnigma(); + public void syncStateFromEnigmaToLayout() + { + this.setLayoutState(getEnigma().getState()); + } + public abstract void showRingSettingsDialog(); - public LayoutContainer() - { - main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); - this.inputView = (EditText) main.findViewById(R.id.input); - this.outputView = (EditText) main.findViewById(R.id.output); - input = EditTextAdapter.createEditTextAdapter(inputView, main.getPrefMessageFormatting()); - output = EditTextAdapter.createEditTextAdapter(outputView, main.getPrefMessageFormatting()); + public LayoutContainer() + { + main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); + setEnigmaLayout(); + this.inputView = (EditText) main.findViewById(R.id.input); + this.outputView = (EditText) main.findViewById(R.id.output); + input = EditTextAdapter.createEditTextAdapter(inputView, + SettingsActivity.SettingsSingleton.getInstance().getPrefMessageFormatting()); + output = EditTextAdapter.createEditTextAdapter(outputView, + SettingsActivity.SettingsSingleton.getInstance().getPrefMessageFormatting()); inputPreparer = InputPreparer.createInputPreparer(); - initializeLayout(); - } + assembleLayout(); + finishLayout(); + } - public void doCrypto() - { - if(inputView.getText().length()!=0) - { - syncStateFromLayoutToEnigma(); - String message = inputView.getText().toString(); - message = inputPreparer.prepareString(message); - input.setText(message); - output.setText(getEnigma().encryptString(message)); - setLayoutState(getEnigma().getState()); - } - } + public void doCrypto() + { + if(inputView.getText().length()!=0) + { + syncStateFromLayoutToEnigma(); + String message = inputView.getText().toString(); + message = inputPreparer.prepareString(message); + input.setText(message); + output.setText(getEnigma().encryptString(message)); + setLayoutState(getEnigma().getState()); + } + } - public EditTextAdapter getInput() - { - return this.input; - } + public EditTextAdapter getInput() + { + return this.input; + } - public EditTextAdapter getOutput() - { - return this.output; - } + public EditTextAdapter getOutput() + { + return this.output; + } public static LayoutContainer createLayoutContainer() { return createLayoutContainer(SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType()); } - public static LayoutContainer createLayoutContainer(String enigmaType) - { - switch (enigmaType) { - case "I": - return new LayoutContainer_I(); - case "M3": - return new LayoutContainer_M3(); - case "M4": - return new LayoutContainer_M4(); - case "D": - return new LayoutContainer_D(); - case "K": - return new LayoutContainer_K(); - case "KS": - return new LayoutContainer_K_Swiss(); - case "KSA": - return new LayoutContainer_K_Swiss_Airforce(); - case "T": - return new LayoutContainer_T(); - case "R": - return new LayoutContainer_R(); - case "G31": - return new LayoutContainer_G31(); - case "G312": - return new LayoutContainer_G312(); - case "G260": - return new LayoutContainer_G260(); - default: - return new LayoutContainer_I(); - } - } + public static LayoutContainer createLayoutContainer(String enigmaType) + { + switch (enigmaType) { + case "I": + return new LayoutContainer_I(); + case "M3": + return new LayoutContainer_M3(); + case "M4": + return new LayoutContainer_M4(); + case "D": + return new LayoutContainer_D(); + case "K": + return new LayoutContainer_K(); + case "KS": + return new LayoutContainer_K_Swiss(); + case "KSA": + return new LayoutContainer_K_Swiss_Airforce(); + case "T": + return new LayoutContainer_T(); + case "R": + return new LayoutContainer_R(); + case "G31": + return new LayoutContainer_G31(); + case "G312": + return new LayoutContainer_G312(); + case "G260": + return new LayoutContainer_G260(); + case "KD": + return new LayoutContainer_KD(); + default: + return new LayoutContainer_I(); + } + } - /** - * Add ArrayAdapter, contents and layouts to Spinner - * @param view Spinner - * @param resourceID ID of the referenced array (eg. R.array.rotor_1_8) - */ - protected void prepareSpinnerAdapter(Spinner view, int resourceID) { - MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); + /** + * Add ArrayAdapter, contents and layouts to Spinner + * @param view Spinner + * @param resourceID ID of the referenced array (eg. R.array.rotor_1_8) + */ + protected void prepareSpinnerAdapter(Spinner view, int resourceID) { + MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); - ArrayAdapter adapter = ArrayAdapter.createFromResource(main, resourceID, - android.R.layout.simple_spinner_item); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - view.setAdapter(adapter); - } + ArrayAdapter adapter = ArrayAdapter.createFromResource(main, resourceID, + android.R.layout.simple_spinner_item); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + view.setAdapter(adapter); + } - /** - * Add ArrayAdapter, contents and layouts to Spinner - * @param view Spinner - * @param array Character array - */ - protected void prepareSpinnerAdapter(Spinner view, Character[] array) - { - MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); - ArrayAdapter adapter = new ArrayAdapter<>(main.getApplicationContext(), - android.R.layout.simple_spinner_item, array); - adapter.setDropDownViewResource( - android.R.layout.simple_spinner_dropdown_item); - view.setAdapter(adapter); - } + /** + * Add ArrayAdapter, contents and layouts to Spinner + * @param view Spinner + * @param array Character array + */ + protected void prepareSpinnerAdapter(Spinner view, Character[] array) + { + MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); + ArrayAdapter adapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, array); + adapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + view.setAdapter(adapter); + } - public void setInputPreparer(InputPreparer inputPreparer) - { - this.inputPreparer = inputPreparer; - } + public void setInputPreparer(InputPreparer inputPreparer) + { + this.inputPreparer = inputPreparer; + } - public void setEditTextAdapter(String type) - { - String in = input.getText(); - String out = output.getText(); - input = EditTextAdapter.createEditTextAdapter(inputView, type); - input.setText(in); - output = EditTextAdapter.createEditTextAdapter(outputView, type); - output.setText(out); - } + public void setEditTextAdapter(String type) + { + String in = input.getText(); + String out = output.getText(); + input = EditTextAdapter.createEditTextAdapter(inputView, type); + input.setText(in); + output = EditTextAdapter.createEditTextAdapter(outputView, type); + output.setText(out); + } + + protected void setMainActivityLayout() + { + setEnigmaLayout(); + } + + abstract protected void setEnigmaLayout(); + + private void finishLayout() + { + //TODO + } } 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 d82018d..2ffb0bd 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 @@ -4,6 +4,7 @@ import android.view.View; import android.widget.Button; import android.widget.Spinner; +import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_D; @@ -40,13 +41,18 @@ public class LayoutContainer_D extends LayoutContainer public LayoutContainer_D() { super(); - main.setContentView(R.layout.activity_main_d); main.setTitle("D - EnigmAndroid"); this.resetLayout(); } @Override - protected void initializeLayout() + protected void setEnigmaLayout() + { + MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_d); + } + + @Override + protected void assembleLayout() { this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position); this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position); 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 0497e3f..8c46664 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,5 +1,6 @@ package de.vanitasvitae.enigmandroid.layout; +import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma_G260; /** @@ -24,18 +25,18 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_G260; */ public class LayoutContainer_G260 extends LayoutContainer_G31 { - public LayoutContainer_G260() - { - super(); - main.setTitle("G260 - EnigmAndroid"); - this.resetLayout(); - } + public LayoutContainer_G260() + { + super(); + main.setTitle("G260 - EnigmAndroid"); + this.resetLayout(); + } - @Override - public void resetLayout() { - enigma = new Enigma_G260(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } + @Override + public void resetLayout() { + enigma = new Enigma_G260(); + setLayoutState(enigma.getState()); + output.setText(""); + input.setText(""); + } } 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 f42f4e7..a1c49ea 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 @@ -2,6 +2,7 @@ package de.vanitasvitae.enigmandroid.layout; import android.widget.Spinner; +import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; @@ -29,90 +30,95 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_G31; */ public class LayoutContainer_G31 extends LayoutContainer { - protected Enigma enigma; + protected Enigma enigma; - protected Spinner rotor1View; - protected Spinner rotor2View; - protected Spinner rotor3View; + protected Spinner rotor1View; + protected Spinner rotor2View; + protected Spinner rotor3View; - protected Spinner rotor1PositionView; - protected Spinner rotor2PositionView; - protected Spinner rotor3PositionView; - protected Spinner reflectorPositionView; + protected Spinner rotor1PositionView; + protected Spinner rotor2PositionView; + protected Spinner rotor3PositionView; + protected Spinner reflectorPositionView; - public LayoutContainer_G31() - { - super(); - main.setContentView(R.layout.activity_main_g_k_r_t); - main.setTitle("G31 - EnigmAndroid"); - this.resetLayout(); - } + public LayoutContainer_G31() + { + super(); + main.setTitle("G31 - EnigmAndroid"); + this.resetLayout(); + } - @Override - public Enigma getEnigma() { - return this.enigma; - } + @Override + protected void setEnigmaLayout() + { + MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_g_k_r_t); + } - @Override - protected void initializeLayout() { - this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); - this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); - this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); - this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position); - this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position); - this.rotor3PositionView = (Spinner) main.findViewById(R.id.rotor3position); - this.reflectorPositionView = (Spinner) main.findViewById(R.id.reflector_position); + @Override + public Enigma getEnigma() { + return this.enigma; + } - Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} + @Override + protected void assembleLayout() { + this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); + this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); + this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); + this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position); + this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position); + this.rotor3PositionView = (Spinner) main.findViewById(R.id.rotor3position); + this.reflectorPositionView = (Spinner) main.findViewById(R.id.reflector_position); - prepareSpinnerAdapter(rotor1View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor2View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor3View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); - prepareSpinnerAdapter(reflectorPositionView, rotorPositionArray); - } + Character[] rotorPositionArray = new Character[26]; + for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} - @Override - public void resetLayout() { - enigma = new Enigma_G31(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } + prepareSpinnerAdapter(rotor1View, R.array.rotors_1_3); + prepareSpinnerAdapter(rotor2View, R.array.rotors_1_3); + prepareSpinnerAdapter(rotor3View, R.array.rotors_1_3); + prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); + prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); + prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); + prepareSpinnerAdapter(reflectorPositionView, rotorPositionArray); + } - @Override - public void setLayoutState(EnigmaStateBundle state) - { - 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()); - this.reflectorPositionView.setSelection(state.getRotationReflector()); - } + @Override + public void resetLayout() { + enigma = new Enigma_G31(); + setLayoutState(enigma.getState()); + output.setText(""); + input.setText(""); + } - @Override - public void syncStateFromLayoutToEnigma() - { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); - state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); - state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); - state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); - state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); - getEnigma().setState(state); - } + @Override + public void setLayoutState(EnigmaStateBundle state) + { + 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()); + this.reflectorPositionView.setSelection(state.getRotationReflector()); + } - @Override - public void showRingSettingsDialog() - { - new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). - createRingSettingsDialog(getEnigma().getState()); - } + @Override + public void syncStateFromLayoutToEnigma() + { + EnigmaStateBundle state = getEnigma().getState(); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); + state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); + state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); + state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); + state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); + getEnigma().setState(state); + } + + @Override + public void showRingSettingsDialog() + { + new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). + createRingSettingsDialog(getEnigma().getState()); + } } 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 e52f9c9..46f86d3 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 @@ -4,6 +4,7 @@ import android.view.View; import android.widget.Button; import android.widget.Spinner; +import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_I; @@ -43,13 +44,18 @@ public class LayoutContainer_I extends LayoutContainer public LayoutContainer_I() { super(); - main.setContentView(R.layout.activity_main_i_m3); main.setTitle("I - EnigmAndroid"); this.resetLayout(); } @Override - protected void initializeLayout() + protected void setEnigmaLayout() + { + MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_i_m3); + } + + @Override + protected void assembleLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); 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 eb0278a..f318de4 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 @@ -2,6 +2,7 @@ package de.vanitasvitae.enigmandroid.layout; import android.widget.Spinner; +import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; @@ -43,18 +44,23 @@ public class LayoutContainer_K extends LayoutContainer public LayoutContainer_K() { super(); - main.setContentView(R.layout.activity_main_g_k_r_t); main.setTitle("K - EnigmAndroid"); this.resetLayout(); } + @Override + protected void setEnigmaLayout() + { + MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_g_k_r_t); + } + @Override public Enigma getEnigma() { return this.enigma; } @Override - protected void initializeLayout() { + protected void assembleLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_KD.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_KD.java new file mode 100644 index 0000000..b095811 --- /dev/null +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_KD.java @@ -0,0 +1,135 @@ +package de.vanitasvitae.enigmandroid.layout; + +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.Spinner; + +import de.vanitasvitae.enigmandroid.MainActivity; +import de.vanitasvitae.enigmandroid.R; +import de.vanitasvitae.enigmandroid.enigma.Enigma; +import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; +import de.vanitasvitae.enigmandroid.enigma.Enigma_KD; + +/** + * LayoutContainer for the Enigma Model K + * This class contains the layout and controls the layout elements such as spinners and stuff + * Copyright (C) 2015 Paul Schaub + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae + */ +public class LayoutContainer_KD extends LayoutContainer +{ + protected Enigma enigma; + + protected Spinner rotor1View; + protected Spinner rotor2View; + protected Spinner rotor3View; + + protected Spinner rotor1PositionView; + protected Spinner rotor2PositionView; + protected Spinner rotor3PositionView; + protected Spinner reflectorPositionView; + + public LayoutContainer_KD() + { + super(); + main.setTitle("KD - EnigmAndroid"); + this.resetLayout(); + } + + @Override + protected void setEnigmaLayout() + { + MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_kd); + } + + @Override + public Enigma getEnigma() { + return this.enigma; + } + + @Override + protected void assembleLayout() { + this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); + this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); + this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); + this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position); + this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position); + this.rotor3PositionView = (Spinner) main.findViewById(R.id.rotor3position); + this.reflectorPositionView = (Spinner) main.findViewById(R.id.reflector_position); + Button reflectorWiring = (Button) main.findViewById(R.id.button_reflector); + reflectorWiring.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + new PluggableDialogBuilder(getEnigma().getState()).showDialogReflector(); + } + }); + + Character[] rotorPositionArray = new Character[26]; + for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} + + prepareSpinnerAdapter(rotor1View, R.array.rotors_1_3); + prepareSpinnerAdapter(rotor2View, R.array.rotors_1_3); + prepareSpinnerAdapter(rotor3View, R.array.rotors_1_3); + + prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); + prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); + prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); + prepareSpinnerAdapter(reflectorPositionView, rotorPositionArray); + } + + @Override + public void resetLayout() { + enigma = new Enigma_KD(); + setLayoutState(enigma.getState()); + output.setText(""); + input.setText(""); + } + + @Override + public void setLayoutState(EnigmaStateBundle state) + { + 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()); + this.reflectorPositionView.setSelection(state.getRotationReflector()); + } + + @Override + public void syncStateFromLayoutToEnigma() + { + EnigmaStateBundle state = getEnigma().getState(); + state.setTypeRotor1(rotor1View.getSelectedItemPosition()); + state.setTypeRotor2(rotor2View.getSelectedItemPosition()); + state.setTypeRotor3(rotor3View.getSelectedItemPosition()); + state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); + state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); + state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); + state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); + getEnigma().setState(state); + } + + @Override + public void showRingSettingsDialog() + { + new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). + createRingSettingsDialog(getEnigma().getState()); + } +} 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 8c9be62..25502b5 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 @@ -40,7 +40,7 @@ public class LayoutContainer_M3 extends LayoutContainer_I } @Override - protected void initializeLayout() + protected void assembleLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); 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 5d75292..ea70ae2 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 @@ -4,6 +4,7 @@ import android.view.View; import android.widget.Button; import android.widget.Spinner; +import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_M4; @@ -45,11 +46,15 @@ public class LayoutContainer_M4 extends LayoutContainer public LayoutContainer_M4() { super(); - main.setContentView(R.layout.activity_main_m4); main.setTitle("M4 - EnigmAndroid"); this.resetLayout(); } + @Override + protected void setEnigmaLayout() + { + MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_m4); + } public Enigma_M4 getEnigma() @@ -58,7 +63,7 @@ public class LayoutContainer_M4 extends LayoutContainer } @Override - protected void initializeLayout() { + protected void assembleLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); @@ -69,7 +74,7 @@ public class LayoutContainer_M4 extends LayoutContainer this.rotor4PositionView = (Spinner) main.findViewById(R.id.thin_rotor_position); this.reflectorView = (Spinner) main.findViewById(R.id.reflector); Button setPlugboardButton = (Button) main.findViewById(R.id.button_plugboard); - setPlugboardButton.setOnClickListener(new View.OnClickListener() { + if(setPlugboardButton != null) setPlugboardButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new PluggableDialogBuilder(getEnigma().getState()).showDialogPlugboard(); 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 03fcc5b..96ffc6e 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 @@ -2,6 +2,7 @@ package de.vanitasvitae.enigmandroid.layout; import android.widget.Spinner; +import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; @@ -43,18 +44,23 @@ public class LayoutContainer_R extends LayoutContainer public LayoutContainer_R() { super(); - main.setContentView(R.layout.activity_main_g_k_r_t); main.setTitle("R - EnigmAndroid"); this.resetLayout(); } + @Override + protected void setEnigmaLayout() + { + MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_g_k_r_t); + } + @Override public Enigma getEnigma() { return this.enigma; } @Override - protected void initializeLayout() { + protected void assembleLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); 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 b389d7e..ca91e3d 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 @@ -2,6 +2,7 @@ package de.vanitasvitae.enigmandroid.layout; import android.widget.Spinner; +import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; @@ -43,7 +44,6 @@ public class LayoutContainer_T extends LayoutContainer public LayoutContainer_T() { super(); - main.setContentView(R.layout.activity_main_g_k_r_t); main.setTitle("T - EnigmAndroid"); this.resetLayout(); } @@ -66,13 +66,20 @@ public class LayoutContainer_T extends LayoutContainer } } + @Override + protected void setEnigmaLayout() + { + MainActivity.ActivitySingleton.getInstance().getActivity().setContentView( + R.layout.activity_main_g_k_r_t); + } + @Override public Enigma getEnigma() { return this.enigma; } @Override - protected void initializeLayout() { + protected void assembleLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); 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 20ba903..cadc9af 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java @@ -74,7 +74,7 @@ public class PassphraseDialogBuilder { AlertDialog.Builder builder = new AlertDialog.Builder(main); - builder.setTitle(R.string.hint_passphrase); + builder.setTitle(R.string.hint_configuration); Dialog d = builder.setView(passphraseDialogView) .setCancelable(true) .setPositiveButton(R.string.dialog_positive, new DialogInterface.OnClickListener() @@ -89,7 +89,7 @@ public class PassphraseDialogBuilder } else { - main.createStateFromSeed(pass); + main.applyStateFromSeed(pass); String message = String.format(main.getResources().getString( R.string.dialog_passphrase_set), " \'"+pass+"\'"); Toast.makeText(main, message, Toast.LENGTH_LONG).show(); diff --git a/app/src/main/res/layout/activity_main_kd.xml b/app/src/main/res/layout/activity_main_kd.xml new file mode 100755 index 0000000..d2ecde5 --- /dev/null +++ b/app/src/main/res/layout/activity_main_kd.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +