-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathRijndael.cs
201 lines (174 loc) · 7.56 KB
/
Rijndael.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Security.Cryptography;
using Moserware.AesIllustrated.Rounds;
using Moserware.AesIllustrated.Transforms;
namespace Moserware.AesIllustrated
{
/// <summary>
/// Main interface to the Rijndael algorithm.
/// </summary>
public class Rijndael
{
// The individual steps
private int _BlockSizeInBits = 128;
private FinalRound _FinalRound;
private InitialRound _InitialRound;
private IntermediateRound _IntermediateRound;
private KeySchedule _KeySchedule;
private Settings _Settings;
public Rijndael()
: this(ByteUtilities.GetCryptographicallyRandomBytes(256/Constants.BitsPerByte))
{
}
public Rijndael(byte[] key)
{
Rekey(key);
}
public Rijndael(byte[] key, int blockSizeInBits)
{
_BlockSizeInBits = blockSizeInBits;
Rekey(key);
}
/// <summary>
/// Cipher key (must be a multiple of 32 bits and at least 128 bits).
/// </summary>
public byte[] Key
{
get { return _KeySchedule.Key; }
set
{
if (value == null)
{
// not calling it "value" since it likely will come from a static function
throw new ArgumentNullException("key");
}
_KeySchedule.Key = value;
_Settings = new Settings(value.Length*Constants.BitsPerByte, BlockSize);
}
}
/// <summary>
/// Block size in bits.
/// </summary>
public int BlockSize
{
get { return _BlockSizeInBits; }
set
{
int blockByteCount = BlockSize/Constants.BitsPerByte;
if (((BlockSize%Constants.BitsPerByte) != 0) || ((blockByteCount%Constants.StateRows) != 0))
{
throw new ArgumentException("Block size must be at least 128 bits and a valid multiple of 32 bits",
"value");
}
_BlockSizeInBits = value;
_Settings = new Settings(Key.Length*Constants.BitsPerByte, value);
}
}
private void Rekey(byte[] key)
{
_KeySchedule = new KeySchedule(key, BlockSize/Constants.BitsPerByte);
_Settings = new Settings(key.Length*Constants.BitsPerByte, BlockSize);
_InitialRound = new InitialRound(_Settings, _KeySchedule);
_IntermediateRound = new IntermediateRound(_Settings, _KeySchedule);
_FinalRound = new FinalRound(_Settings, _KeySchedule);
}
/// <summary>
/// Encrypts a single block.
/// </summary>
/// <param name="input">Block to encrypt.</param>
/// <returns>Encrypted block</returns>
public byte[] Encrypt(byte[] input)
{
State state = new State(input);
if (Debugging.IsEnabled)
{
Debugging.Trace("Encrypting these plaintext bytes:");
ByteUtilities.WriteBytes(input);
Debugging.Trace("");
Debugging.Trace("Initial state:");
Debugging.Trace(state.ToString());
Debugging.Trace("");
}
_InitialRound.Apply(state, 0);
int totalRounds = _Settings.Rounds;
for (int round = 1; round < totalRounds; round++)
{
_IntermediateRound.Apply(state, round);
}
_FinalRound.Apply(state, totalRounds);
byte[] encryptedBytes = state.ToByteArray();
if(Debugging.IsEnabled)
{
Debugging.Trace("Encryption resulted in this ciphertext:");
ByteUtilities.WriteBytes(encryptedBytes);
}
return encryptedBytes;
}
/// <summary>
/// Encrypts a single block.
/// </summary>
/// <param name="input">Block to encrypt.</param>
/// <param name="key">Cipher key</param>
/// <returns>Encrypted block</returns>
public static byte[] Encrypt(byte[] input, byte[] key)
{
return (new Rijndael(key, input.Length * Constants.BitsPerByte)).Encrypt(input);
}
/// <summary>
/// Creates an encryption transform that can be used by the .NET cryptography APIs.
/// </summary>
/// <param name="cipherMode">The mode if the cipher to use.</param>
/// <param name="initializationVector">An initialization vector (if used by the <paramref name="cipherMode"/>, or <see langword="null"/> if not used).</param>
/// <param name="feedbackSizeInBits">Size of the feedback register in bits for feedback modes.</param>
/// <param name="paddingMode">The style of padding to apply to the last block.</param>
/// <returns>An encryption transform that can be used by the .NET cryptography APIs.</returns>
public ICryptoTransform CreateEncryptor(CipherMode cipherMode, byte[] initializationVector,
int feedbackSizeInBits, PaddingMode paddingMode)
{
return RijndaelEncryptionTransformFactory.Create(this, cipherMode, initializationVector, feedbackSizeInBits,
paddingMode);
}
/// <summary>
/// Decrypts a single block.
/// </summary>
/// <param name="input">Block to decrypt.</param>
/// <returns>The decrypted block.</returns>
public byte[] Decrypt(byte[] input)
{
// Decryption does everything in reverse
State state = new State(input);
int totalRounds = _Settings.Rounds;
_FinalRound.Inverse(state, totalRounds);
for (int round = (totalRounds - 1); round > 0; round--)
{
_IntermediateRound.Inverse(state, round);
}
_InitialRound.Inverse(state, 0);
return state.ToByteArray();
}
/// <summary>
/// Decrypts a single block.
/// </summary>
/// <param name="input">Block to decrypt.</param>
/// <param name="key">Cipher key</param>
/// <returns>The decrypted block.</returns>
public static byte[] Decrypt(byte[] input, byte[] key)
{
return (new Rijndael(key, input.Length * Constants.BitsPerByte)).Decrypt(input);
}
/// <summary>
/// Creates a decryption transform that can be used by the .NET cryptography APIs.
/// </summary>
/// <param name="cipherMode">The mode if the cipher to use.</param>
/// <param name="initializationVector">An initialization vector (if used by the <paramref name="cipherMode"/>, or <see langword="null"/> if not used).</param>
/// <param name="feedbackSizeInBits">Size of the feedback register in bits for feedback modes.</param>
/// <param name="paddingMode">The style of padding to apply to the last block.</param>
/// <returns>An decryption transform that can be used by the .NET cryptography APIs.</returns>
public ICryptoTransform CreateDecryptor(CipherMode cipherMode, byte[] initializationVector,
int feedbackSizeInBits, PaddingMode paddingMode)
{
return RijndaelDecryptionTransformFactory.Create(this, cipherMode, initializationVector, feedbackSizeInBits,
paddingMode);
}
}
}