2019-01-04 20:40:11 +01:00
|
|
|
/**
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
Started new Iteration of MainActivity as Main2, moved getters and setters of preferences to SettingsActivity
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
Translated some strings, commented some methods in MainActivity
New Icon!
Added comment and removed unused import, peanuts if you ask me
Added Script to automatically render icon in all dimensions
Reformated code and moved Plugboard, EntryWheel, Reflector, Rotor to new package parts
Cleaned repository
Merge branch 'development'
2017-05-05 16:49:30 +02:00
|
|
|
package de.vanitasvitae.enigmandroid.enigma.parts;
|
2015-02-04 20:51:31 +01:00
|
|
|
|
2015-10-04 22:51:35 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.util.Arrays;
|
2015-09-30 14:25:49 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2015-10-04 22:51:35 +02:00
|
|
|
import de.vanitasvitae.enigmandroid.MainActivity;
|
Started new Iteration of MainActivity as Main2, moved getters and setters of preferences to SettingsActivity
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
Translated some strings, commented some methods in MainActivity
New Icon!
Added comment and removed unused import, peanuts if you ask me
Added Script to automatically render icon in all dimensions
Reformated code and moved Plugboard, EntryWheel, Reflector, Rotor to new package parts
Cleaned repository
Merge branch 'development'
2017-05-05 16:49:30 +02:00
|
|
|
import de.vanitasvitae.enigmandroid.enigma.Enigma;
|
2015-09-30 14:25:49 +02:00
|
|
|
import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer;
|
|
|
|
|
2015-08-15 03:53:02 +02:00
|
|
|
/**
|
2015-08-16 02:59:56 +02:00
|
|
|
* Plugboard of the enigma
|
|
|
|
* Copyright (C) 2015 Paul Schaub
|
2015-02-04 20:51:31 +01:00
|
|
|
*/
|
|
|
|
public class Plugboard
|
|
|
|
{
|
2015-10-04 22:51:35 +02:00
|
|
|
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};
|
2015-09-15 02:06:47 +02:00
|
|
|
private int[] plugs;
|
2015-02-18 21:51:40 +01:00
|
|
|
|
|
|
|
public Plugboard()
|
|
|
|
{
|
2015-10-09 00:57:54 +02:00
|
|
|
plugs = Arrays.copyOf(empty, empty.length);
|
2015-08-15 03:53:02 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 02:06:47 +02:00
|
|
|
public Plugboard(int[] conf)
|
2015-08-15 03:53:02 +02:00
|
|
|
{
|
2015-09-15 02:06:47 +02:00
|
|
|
this.plugs = conf;
|
2015-02-18 21:51:40 +01:00
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
|
2015-09-15 02:06:47 +02:00
|
|
|
public void setConfiguration(int[] conf)
|
2015-02-18 21:51:40 +01:00
|
|
|
{
|
2015-09-15 02:06:47 +02:00
|
|
|
this.plugs = conf;
|
2015-02-18 21:51:40 +01:00
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
|
Started new Iteration of MainActivity as Main2, moved getters and setters of preferences to SettingsActivity
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
Translated some strings, commented some methods in MainActivity
New Icon!
Added comment and removed unused import, peanuts if you ask me
Added Script to automatically render icon in all dimensions
Reformated code and moved Plugboard, EntryWheel, Reflector, Rotor to new package parts
Cleaned repository
Merge branch 'development'
2017-05-05 16:49:30 +02:00
|
|
|
@SuppressWarnings("UnusedReturnValue")
|
2015-10-04 22:51:35 +02:00
|
|
|
public BigInteger setConfiguration(BigInteger b)
|
|
|
|
{
|
|
|
|
String s = "";
|
|
|
|
|
|
|
|
int x;
|
2015-10-09 00:57:54 +02:00
|
|
|
while((x = Enigma.getValue(b, 27)) != 26 && b.compareTo(BigInteger.ZERO) > 1)
|
2015-10-04 22:51:35 +02:00
|
|
|
{
|
|
|
|
s = ((char) (x+65))+s;
|
|
|
|
b = Enigma.removeDigit(b, 27);
|
|
|
|
}
|
|
|
|
this.setConfiguration(stringToConfiguration(s));
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2015-09-15 02:06:47 +02:00
|
|
|
public int[] getConfiguration()
|
2015-02-18 21:51:40 +01:00
|
|
|
{
|
2015-09-15 02:06:47 +02:00
|
|
|
return this.plugs;
|
2015-02-18 21:51:40 +01:00
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
|
2015-08-15 03:53:02 +02:00
|
|
|
/**
|
|
|
|
* Encrypt input via plugboard connections
|
2015-09-10 02:56:48 +02:00
|
|
|
* @param input input symbol to encryptString
|
2015-08-15 03:53:02 +02:00
|
|
|
* @return encrypted symbol
|
|
|
|
*/
|
|
|
|
public int encrypt(int input)
|
2015-02-04 20:51:31 +01:00
|
|
|
{
|
2015-09-15 02:06:47 +02:00
|
|
|
return plugs[(input+plugs.length)%plugs.length];
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|
2015-09-30 14:25:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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));
|
Started new Iteration of MainActivity as Main2, moved getters and setters of preferences to SettingsActivity
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
Translated some strings, commented some methods in MainActivity
New Icon!
Added comment and removed unused import, peanuts if you ask me
Added Script to automatically render icon in all dimensions
Reformated code and moved Plugboard, EntryWheel, Reflector, Rotor to new package parts
Cleaned repository
Merge branch 'development'
2017-05-05 16:49:30 +02:00
|
|
|
int[] out = Arrays.copyOf(empty, empty.length);
|
2015-09-30 14:25:49 +02:00
|
|
|
//Check if in is too long or odd
|
2015-10-04 22:51:35 +02:00
|
|
|
int l = pairs.length();
|
|
|
|
if(l>1 && (pairs.length() > 26 || pairs.length()/2 == (pairs.length()-1)/2))
|
2015-09-30 14:25:49 +02:00
|
|
|
{
|
|
|
|
//Odd length. remove last char. Information loss!
|
|
|
|
pairs = pairs.substring(0,pairs.length()-1);
|
|
|
|
}
|
|
|
|
//Modify out
|
|
|
|
for(int i=0; i<pairs.length()/2; i++)
|
|
|
|
{
|
|
|
|
int a = (int) (pairs.toCharArray()[i*2])-65;
|
|
|
|
int b = (int) (pairs.toCharArray()[(i*2)+1])-65;
|
|
|
|
out[a] = b;
|
|
|
|
out[b] = a;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all duplicate chars x from the String except the first appearance of x.
|
|
|
|
* String MUST be in upper case!
|
|
|
|
* @param in String
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
private static String trimString(String in)
|
|
|
|
{
|
|
|
|
in = in.toUpperCase();
|
|
|
|
for(int i=0; i<26; i++)
|
|
|
|
{
|
|
|
|
char x = (char)(i+65);
|
|
|
|
int index = in.indexOf(""+x);
|
|
|
|
if(index != in.lastIndexOf(""+x))
|
|
|
|
{
|
|
|
|
//Remove all duplicates of x
|
|
|
|
in = in.substring(0, index) + in.substring(index+1).replace(""+x,"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a configuration from a seeded PRNG.
|
|
|
|
* Not all connections have to be done.
|
|
|
|
* @param rand Seeded PRNG
|
|
|
|
* @return configuration
|
|
|
|
*/
|
|
|
|
public static int[] seedToPlugboardConfiguration(Random rand)
|
|
|
|
{
|
|
|
|
int connectionCount = rand.nextInt(14); //0..13
|
|
|
|
int[] out = empty;
|
|
|
|
for(int i=0; i<connectionCount; i++)
|
|
|
|
{
|
|
|
|
int rA = rand.nextInt(26);
|
|
|
|
while(out[rA] != rA) rA = (rA+1)%26;
|
|
|
|
int rB = rand.nextInt(26);
|
|
|
|
while(out[rB] != rB) rB = (rB+1)%26;
|
|
|
|
out[rA] = rB;
|
|
|
|
out[rB] = rA;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a configuration with full set of connections from a seeded PRNG
|
|
|
|
* @param rand Seeded PRNG
|
|
|
|
* @return configuration
|
|
|
|
*/
|
|
|
|
public static int[] seedToReflectorConfiguration(Random rand)
|
|
|
|
{
|
2015-10-04 22:51:35 +02:00
|
|
|
int[] out = Arrays.copyOf(empty,empty.length);
|
|
|
|
for (int i=0; i<out.length/2; i++) {
|
2015-09-30 14:25:49 +02:00
|
|
|
int rA = rand.nextInt(26);
|
2015-10-04 22:51:35 +02:00
|
|
|
while (out[rA] != rA) rA = (rA + 27) % 26;
|
2015-09-30 14:25:49 +02:00
|
|
|
int rB = rand.nextInt(26);
|
2015-10-04 22:51:35 +02:00
|
|
|
while (out[rB] != rB || rA == rB) rB = (rB + 27) % 26;
|
2015-09-30 14:25:49 +02:00
|
|
|
out[rA] = rB;
|
|
|
|
out[rB] = rA;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a configurations array back to a String representation
|
|
|
|
* @param c array
|
|
|
|
* @return String representation
|
|
|
|
*/
|
Started new Iteration of MainActivity as Main2, moved getters and setters of preferences to SettingsActivity
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
Translated some strings, commented some methods in MainActivity
New Icon!
Added comment and removed unused import, peanuts if you ask me
Added Script to automatically render icon in all dimensions
Reformated code and moved Plugboard, EntryWheel, Reflector, Rotor to new package parts
Cleaned repository
Merge branch 'development'
2017-05-05 16:49:30 +02:00
|
|
|
private static String configurationToString(int[] c)
|
2015-09-30 14:25:49 +02:00
|
|
|
{
|
|
|
|
String out = "";
|
|
|
|
for(int i=0; i<c.length; i++) // c.length = 26 (mostly)
|
|
|
|
{
|
|
|
|
if(c[i] != i && out.indexOf((char)(c[i])+65)==-1)
|
|
|
|
{
|
|
|
|
out += (char) (i+65);
|
|
|
|
out += (char) (c[i]+65);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2015-10-01 00:39:17 +02:00
|
|
|
|
2015-10-04 22:51:35 +02:00
|
|
|
public static BigInteger configurationToBigInteger(int[] a)
|
2015-10-01 00:39:17 +02:00
|
|
|
{
|
|
|
|
String s = configurationToString(a);
|
2015-10-04 22:51:35 +02:00
|
|
|
BigInteger l = BigInteger.ZERO;
|
|
|
|
l = Enigma.addDigit(l,26,27);
|
2015-10-01 00:39:17 +02:00
|
|
|
for(char c : s.toCharArray())
|
|
|
|
{
|
|
|
|
int i = (int) (c);
|
|
|
|
i-=65;
|
2015-10-04 22:51:35 +02:00
|
|
|
l = Enigma.addDigit(l,i,27);
|
2015-10-01 00:39:17 +02:00
|
|
|
}
|
2015-10-04 22:51:35 +02:00
|
|
|
Log.d(MainActivity.APP_ID, "Save configuration plugs: "+l.toString());
|
2015-10-01 00:39:17 +02:00
|
|
|
return l;
|
|
|
|
}
|
2015-10-04 22:51:35 +02:00
|
|
|
|
|
|
|
public static int[] bigIntegerToConfiguration(BigInteger b)
|
|
|
|
{
|
|
|
|
String s = "";
|
|
|
|
int x;
|
2015-10-09 00:57:54 +02:00
|
|
|
while((x = Enigma.getValue(b, 27)) != 26 && b.compareTo(BigInteger.ZERO) > 1)
|
2015-10-04 22:51:35 +02:00
|
|
|
{
|
|
|
|
s = ((char) (x+65))+s;
|
|
|
|
b = Enigma.removeDigit(b, 27);
|
|
|
|
}
|
|
|
|
Log.d(MainActivity.APP_ID, "Restored: "+s);
|
|
|
|
return stringToConfiguration(s);
|
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|