package beast;
import java.lang.reflect.MalformedParameterizedTypeException;
/**
* Represents a dynamic rotor capable of simple substitution in an enigma machine.
* A rotor has 40 positions.
* @author Colter LeSage
*/
public class Rotor {
private static final String string2 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ .?!";
/**
* One of many standard rotors ready to use right out of the box.
*/
public static final Rotor ROTOR_I = new Rotor(1,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. !?");
/**
* One of many standard rotors ready to use right out of the box.
*/
public static final Rotor ROTOR_II = new Rotor(8,"0P9OL8IK7UJM6YHN5TGB4?RFV3!EDC2WSX1QAZ. ");
/**
* One of many standard rotors ready to use right out of the box.
*/
public static final Rotor ROTOR_III = new Rotor(13,"QWERTPOIUY1029384756ASDFLKJHG. !?ZXCMNBV");
/**
* One of many standard rotors ready to use right out of the box.
*/
public static final Rotor ROTOR_IV = new Rotor(6,"ISX1QJM6YHN5TGAZ. K7U?RFV3!EDC2WB0P9OL84");
/**
* One of many standard rotors ready to use right out of the box.
*/
public static final Rotor ROTOR_V = new Rotor(2,"WA2Q1GV6PL. N8UHX40OK?M9B7YESZ3TFC5RDIJ!");
private String string1;
private int rotations;
private int frequency;
/**
* @param i determines how often the rotor rotates. Variety is desired for more robust encryptions.
* @param a a String containing all letters of the alphabet, the numbers from 0 to 9, a space, a period, an exclamation point, and a question mark
*/
public Rotor(int i,String a) throws MalformedParameterizedTypeException{
string1 = a.toUpperCase();rotations = 0;frequency = i;
for(char x = 'A'; x < 'Z' + 1; x++){
if(string1.indexOf(x) < 0)
throw new MalformedParameterizedTypeException();
}
for(char x = '0'; x < '9' + 1; x++){
if(string1.indexOf(x) < 0)
throw new MalformedParameterizedTypeException();
}
if(string1.indexOf('.') < 0)throw new MalformedParameterizedTypeException();
if(string1.indexOf(' ') < 0)throw new MalformedParameterizedTypeException();
if(string1.indexOf('?') < 0)throw new MalformedParameterizedTypeException();
if(string1.indexOf('!') < 0)throw new MalformedParameterizedTypeException();
if(frequency < 1)throw new MalformedParameterizedTypeException();
}
/**
* Positions the rotor at a desired location
* @param i an int typically between 0 an 39
*/
public void position(int i){
for(int x = 0; x < rotations; x++)rotate(-1);
for(int x = 0; x < i; x++)rotate(1);
}
/**
* Sends a char through the substitution algorhythm
* @param c the char to be converted
* @param direction 1 or -1 depending on whether moving toward or away from the inverter
* @return an encrypted char
*/
public char chug(char c, int direction){
if(direction == 1)
return string2.charAt(string1.indexOf(c));
else
return string1.charAt(string2.indexOf(c));
}
/**
* rotates the rotor once
*/
public void rotate(){
rotate(1);
}
private void rotate(int direction){
if(direction == 1){
string1.replace('Z', '~');
string2.replace('Z', '~');
for(char x = 'A'; x < 'Z'; x++){
string1.replace(x, (char)(x + 1));
string2.replace(x, (char)(x + 1));
}
string1.replace('~', 'A');
string2.replace('~', 'A');
rotations += 1;
}
else if(direction == -1){
string1.replace('A', '~');
string2.replace('A', '~');
for(char x = 'B'; x <= 'Z'; x++){
string1.replace(x, (char)(x - 1));
string2.replace(x, (char)(x - 1));
}
string1.replace('~', 'Z');
string2.replace('~', 'Z');
rotations -= 1;
}
if(rotations > 39) rotations = 0;
if(rotations < 0) rotations = 39;
}
/**
* Get the position of the rotor
* @return the current position of the rotor (0 to 39)
*/
public int getOrientation(){
return rotations;
}
/**
* Get the frequency at which the rotor turns
* @return the frequency at which the rotor turns
*/
public int getFrequency(){
return frequency;
}
}
package beast;
import java.lang.reflect.MalformedParameterizedTypeException;
/**
* Represents digitially a stationary inverter in an enigma machine.
* An inverter has has two positions.
* @author Colter LeSage
*/
public class Inverter {
/**
* A standard inverter ready to use right out of the box.
*/
public static final Inverter STANDARD_INVERTER = new Inverter("QPWOE?IRUTYLA KSJDHFGZM.XNCBV1!029384756");
private String key;
private int orientation;
/**
* @param s a String containing all letters of the alphabet, the numbers from 0 to 9, a space, a period, an exclamation point, and a question mark
*/
public Inverter(String s){
key = s.toUpperCase();
for(char x = 'A'; x < 'Z' + 1; x++){
if(key.indexOf(x) < 0)
throw new MalformedParameterizedTypeException();
}
for(char x = '0'; x < '9' + 1; x++){
if(key.indexOf(x) < 0)
throw new MalformedParameterizedTypeException();
}
if(key.indexOf('.') < 0)throw new MalformedParameterizedTypeException();
if(key.indexOf(' ') < 0)throw new MalformedParameterizedTypeException();
if(key.indexOf('?') < 0)throw new MalformedParameterizedTypeException();
if(key.indexOf('!') < 0)throw new MalformedParameterizedTypeException();
orientation = 1;
}
/**
* "flips" the inverter between it's two states; 1 and -1
*/
public void flip(){
orientation = -orientation;
StringBuffer firstHalf = new StringBuffer(key.substring(0, 20));
key = (firstHalf.reverse()).toString() + key.substring(20, 40);
}
/**
* @return returns either 1 or -1 based on the inverter's position
*/
public int getOrientation(){
return orientation;
}
/**
* @param c char to be sent through the inverter
* @return the char that the input char is paired with
*/
public char chug(char c){
return key.charAt(39 - key.indexOf(c));
}
}