Repository navigation
Expand file tree
/
Copy pathRotor.java
More file actions
137 lines (118 loc) · 3.37 KB
/
Copy pathRotor.java
File metadata and controls
137 lines (118 loc) · 3.37 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.meowdoleon.enigma;
/**
* Classe définissant un rotor
*
* @author Alex Lebeau
* @version 1.0, 11/30/20
*/
public class Rotor
{
private int[] range1 = new int[26];
private int[] range2 = new int[26];
private int id;
private int ordreDirection;
private int tempPermutationR1;
private int tempPermutationR2;
private char directionCrypto;
public Rotor(int[] r1, int[] r2, int paramID)
{
this.range1 = r1;
this.range2 = r2;
this.id = paramID;
}
public int getId()
{
return id;
}
public int[] getRange1()
{
return range1;
}
public int[] getRange2()
{
return range2;
}
public int getOrdreDirection()
{
return ordreDirection;
}
public void setOrdreDirection(int ordreDirection)
{
this.ordreDirection = ordreDirection;
}
public char getDirectionCrypto()
{
return directionCrypto;
}
public void setDirectionCrypto(char directionCrypto)
{
this.directionCrypto = directionCrypto;
}
/**
* Fonction qui tourne le rotor dans la direction et d'un nombre d'unité indiqué.
*
* @param direction 'G' pour gauche, 'D' pour droite.
* @param nbUnite Entier positif.
* @return
* @throws java.lang.IllegalArgumentException
*/
public Rotor tourneRot(char direction, int nbUnite) throws IllegalArgumentException
{
if(direction != 'D' && direction != 'G')
{
throw new IllegalArgumentException("Mauvais caractère entré pour la direction");
}
if(nbUnite < 0) // 0 fonctionne, même s'il est inutile de le faire.
{
throw new IllegalArgumentException("Un entier positif doit être entré pour le nombre d'unité.");
}
if(direction == 'D' && nbUnite != 0)
{
for(int i = 0; i < nbUnite; i++)
{
tempPermutationR1 = range1[25];
tempPermutationR2 = range2[25];
for(int j = 25; j >= 0; j--)
{
if(j != 0)
{
range1[j] = range1[j - 1];
range2[j] = range2[j - 1];
}
else
{
range1[0] = tempPermutationR1;
range2[0] = tempPermutationR2;
}
}
}
return this;
}
else if(direction == 'G' && nbUnite != 0)
{
for(int i = 0; i < nbUnite; i++)
{
tempPermutationR1 = range1[0];
tempPermutationR2 = range2[0];
for(int j = 0; j < 26; j++)
{
if(j != 25)
{
range1[j] = range1[j + 1];
range2[j] = range2[j + 1];
}
else
{
range1[25] = tempPermutationR1;
range2[25] = tempPermutationR2;
}
}
}
return this;
}
else
{
return this;
}
}
}