67 lines
2.1 KiB
Java
67 lines
2.1 KiB
Java
/**
|
|
* Enigma Class with method for en-/decoding of characters.
|
|
* Constructs the Enigma.
|
|
*/
|
|
public class Enigma {
|
|
|
|
/**
|
|
* Patchboard of the Enigma.
|
|
*/
|
|
private PatchBoard patchboard;
|
|
|
|
/**
|
|
* Reflector of the Engima.
|
|
*/
|
|
private Reflector reflector;
|
|
|
|
/**
|
|
* Array of variable mount of Enigma's rotors.
|
|
*/
|
|
private Rotor[] rotor;
|
|
|
|
/**
|
|
* Creates the Enigma with patchboard, reflector and all the rotors.
|
|
* Arguments are committed by the Shell.
|
|
* @param tickPos tick-position-array for all rotors
|
|
* @param initialPos initial-position-array
|
|
* @param permu permutation-array-array
|
|
* @param patchboardpermu Permutation for the patchboard
|
|
* @param reflectorpermu Permutation for the reflector
|
|
*/
|
|
Enigma(final int[] tickPos, final int[] initialPos, final int[][] permu
|
|
, final int[] patchboardpermu, final int[] reflectorpermu) {
|
|
this.patchboard = new PatchBoard(patchboardpermu);
|
|
this.reflector = new Reflector(reflectorpermu);
|
|
rotor = new Rotor[tickPos.length];
|
|
for (int i = 0; i < tickPos.length; i++) {
|
|
rotor[i] = new Rotor(tickPos[i],
|
|
initialPos[i], permu[i]);
|
|
if (0 < i) {
|
|
this.rotor[i - 1].setNext(rotor[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method for en-/decoding of a Character. Calls one by one the patchboard
|
|
* , all the rotors, the Reflector, back again all the rotors and finally
|
|
* again the patchboard like in the Enigma, used by the Germans in WW2.
|
|
* @param symbol int-value of a Character
|
|
* @return en-/decoded value of a Character
|
|
*/
|
|
public final int encode(final int symbol) {
|
|
int i = symbol;
|
|
i = patchboard.encode(i);
|
|
for (int j = 0; j < rotor.length; j++) {
|
|
i = rotor[j].encode(i);
|
|
}
|
|
i = reflector.encode(i);
|
|
for (int k = rotor.length - 1; k >= 0; k--) {
|
|
i = this.rotor[k].decode(i);
|
|
}
|
|
i = patchboard.decode(i);
|
|
rotor[0].tick();
|
|
return i;
|
|
}
|
|
|
|
} |