/** * 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 3 of the License, or * 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, see . */ package de.vanitasvitae.enigmandroid.enigma.parts; import android.util.Log; import java.math.BigInteger; import java.util.Arrays; import java.util.Random; import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer; /** * Plugboard of the enigma * Copyright (C) 2015 Paul Schaub */ public class Plugboard { private static final int[] empty = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}; private int[] plugs; public Plugboard() { plugs = Arrays.copyOf(empty, empty.length); } public Plugboard(int[] conf) { this.plugs = conf; } public void setConfiguration(int[] conf) { this.plugs = conf; } @SuppressWarnings("UnusedReturnValue") 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); } this.setConfiguration(stringToConfiguration(s)); return b; } public int[] getConfiguration() { return this.plugs; } /** * Encrypt input via plugboard connections * @param input input symbol to encryptString * @return encrypted symbol */ public int encrypt(int input) { return plugs[(input+plugs.length)%plugs.length]; } /** * Interpret a String of Pairs as a configuration for the plugboard * Any char with an even index is connected to the successor. * in must not contain duplicates! * @param in String representation of pairs (eg. AXBHCS...) * @return connections as int[] */ public static int[] stringToConfiguration(String in) { String pairs = trimString(new InputPreparer.RemoveIllegalCharacters().prepareString(in)); 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)) { //Odd length. remove last char. Information loss! pairs = pairs.substring(0,pairs.length()-1); } //Modify out for(int i=0; i 1) { s = ((char) (x+65))+s; b = Enigma.removeDigit(b, 27); } Log.d(MainActivity.APP_ID, "Restored: "+s); return stringToConfiguration(s); } }