-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnigmaMachine.java
55 lines (39 loc) · 1.55 KB
/
EnigmaMachine.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class EnigmaMachine {
private Rotor lrotor;
private Rotor mrotor;
private Rotor rrotor;
private Reflector reflector;
private Plugboard plugboard;
// CONSTRUCTOR
public EnigmaMachine(Rotor lrotor, Rotor mrotor, Rotor rrotor, Reflector reflector, Plugboard plugboard) {
this.lrotor = lrotor;
this.mrotor = mrotor;
this.rrotor = rrotor;
this.reflector = reflector;
this.plugboard = plugboard;
} // END OF CONSTRUCTOR
public char getEncodedChar(char c) {
int characterIndex = EnigmaConstants.getCharIndex(c);
incrementRotors(lrotor, mrotor, rrotor);
characterIndex = plugboard.getEncodedChar(characterIndex);
characterIndex = rrotor.getEncodedChar(0, characterIndex);
characterIndex = mrotor.getEncodedChar(0, characterIndex);
characterIndex = lrotor.getEncodedChar(0, characterIndex);
characterIndex = reflector.getEncodedChar(characterIndex);
characterIndex = lrotor.getEncodedChar(1, characterIndex);
characterIndex = mrotor.getEncodedChar(1, characterIndex);
characterIndex = rrotor.getEncodedChar(1, characterIndex);
characterIndex = plugboard.getEncodedChar(characterIndex);
c = EnigmaConstants.PLAINTEXT.charAt(characterIndex);
return c;
}
public void incrementRotors(Rotor lrotor, Rotor mrotor, Rotor rrotor) {
if(rrotor.getWheelPosition() == rrotor.getNotchPosition()) {
if(mrotor.getWheelPosition() == mrotor.getNotchPosition()) {
lrotor.incrementWheelPosition();
}
mrotor.incrementWheelPosition();
}
rrotor.incrementWheelPosition();
}
} // END OF EnigmaMachine CLASS