-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathSubstitutionBox.cs
42 lines (36 loc) · 1.1 KB
/
SubstitutionBox.cs
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
namespace Moserware.AesIllustrated
{
/// <summary>
/// Provides a non-linear substitution for bytes.
/// </summary>
internal static class SubstitutionBox
{
private static readonly byte[] _InvSBox;
private static readonly byte[] _SBox;
static SubstitutionBox()
{
_SBox = CalculateSBox(out _InvSBox);
}
private static byte[] CalculateSBox(out byte[] invSBox)
{
byte[] result = new byte[256];
invSBox = new byte[256];
// We use an int since a byte would cause this to loop forever due to overflow
for (int i = 0; i < 256; i++)
{
byte currentByte = (byte) i;
result[i] = FiniteFieldMath.F(FiniteFieldMath.G(currentByte));
invSBox[result[i]] = currentByte;
}
return result;
}
public static byte Value(int offset)
{
return _SBox[offset];
}
public static byte Inverse(int offset)
{
return _InvSBox[offset];
}
}
}