-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathARC4CryptoTransform.cs
238 lines (209 loc) · 9.11 KB
/
ARC4CryptoTransform.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
namespace System.Security.Cryptography
{
/// <summary>
/// Performs cryptographic transformation of data using the <see cref = "ARC4CryptoProvider" /> algorithm.
/// This class could not be inherited.
/// </summary>
public sealed class ARC4CryptoTransform : ICryptoTransform
{
private bool _disposed = false;
private CryptoProvider _initialState;
private CryptoProvider _currentState;
/// <summary>
/// Current internal state of the algorithm <see cref = "ARC4" />.
/// </summary>
/// <exception cref="ObjectDisposedException">
/// Thrown if current instance of <see cref="ARC4CryptoTransform"/> is disposed.
/// </exception>
internal string State =>
_disposed
? throw new ObjectDisposedException(nameof(ARC4CryptoTransform),
InternalTools.GetResourceString("ObjectDisposed_Generic"))
: _currentState.ToString();
/// <inheritdoc cref="ICryptoTransform.InputBlockSize"/>
public int InputBlockSize => 1;
/// <inheritdoc cref="ICryptoTransform.OutputBlockSize"/>
public int OutputBlockSize => 1;
/// <inheritdoc cref="ICryptoTransform.CanTransformMultipleBlocks"/>
public bool CanTransformMultipleBlocks => true;
/// <inheritdoc cref="ICryptoTransform.CanReuseTransform"/>
public bool CanReuseTransform => true;
/// <summary>
/// Initializes a new instance <see cref = "ARC4CryptoTransform" />, using the specified parameters.
/// </summary>
/// <param name = "key">
/// The secret key to be used for the symmetric algorithm.
/// </param>
public ARC4CryptoTransform(byte[] key, int skip = 0, bool plus = false)
{
_initialState = CryptoProvider.Create(key, null, skip, plus);
_initialState.Update(key);
_currentState = (ARC4CryptoProvider) _initialState.Clone();
}
/// <summary>
/// Initializes a new instance <see cref = "ARC4CryptoTransform" />
/// using the specified <paramref name="key"/> and <paramref name="iv"/>.
/// </summary>
/// <param name = "key">
/// The secret key to be used for the symmetric algorithm.
/// </param>
/// <param name = "iv">
/// Initialization vector.
/// </param>
public ARC4CryptoTransform(byte[] key, byte[] iv, int skip = 0, bool plus = false)
{
_initialState = CryptoProvider.Create(key, iv, skip, plus);
_initialState.Update(key);
_currentState = (ARC4CryptoProvider)_initialState.Clone();
}
/// <summary>
/// Initializes a new instance <see cref = "ARC4CryptoTransform" />
/// using the specified <paramref name="key"/> and <paramref name="iv"/>.
/// </summary>
/// <param name = "key">
/// The secret key to be used for the symmetric algorithm.
/// </param>
/// <param name = "seed">
/// A signed 32-bit integer value used as the initial state of the <see cref="ARC4"/> algorithm.
/// </param>
public ARC4CryptoTransform(byte[] key, int seed, int skip = 0, bool plus = false)
: this(key, BitConverter.GetBytes(seed))
{
}
/// <summary>
/// Initializes a new instance <see cref = "ARC4CryptoTransform" />
/// using the specified <paramref name="key"/> and <paramref name="iv"/>.
/// </summary>
/// <param name = "key">
/// The secret key to be used for the symmetric algorithm.
/// </param>
/// <param name = "seed">
/// An unsigned 32-bit integer value used as the initial state of the <see cref="ARC4"/> algorithm.
/// </param>
public ARC4CryptoTransform(byte[] key, uint seed, int skip = 0, bool plus = false)
: this(key, BitConverter.GetBytes(seed))
{
}
/// <inheritdoc cref="ICryptoTransform.TransformBlock"/>
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount,
byte[] outputBuffer, int outputOffset)
{
if (_disposed)
throw new ObjectDisposedException(nameof(ARC4CryptoTransform),
InternalTools.GetResourceString("ObjectDisposed_Generic"));
int count = _currentState.Cipher(inputBuffer, inputOffset, inputCount,
outputBuffer, outputOffset);
return count;
}
/// <inheritdoc cref="ICryptoTransform.TransformFinalBlock"/>
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
if (_disposed)
throw new ObjectDisposedException(nameof(ARC4CryptoTransform),
InternalTools.GetResourceString("ObjectDisposed_Generic"));
byte[] outputBuffer = _currentState.Cipher(inputBuffer, inputOffset, inputCount);
Reset();
//throw new CryptographicException()
return outputBuffer;
}
/// <summary>
/// Reset the <see cref = "ARC4CryptoTransform" /> instance state.
/// </summary>
/// <exception cref="ObjectDisposedException">
/// Thrown if current instance of <see cref="ARC4CryptoTransform"/> is disposed.
/// </exception>
public void Reset()
{
if (_disposed)
throw new ObjectDisposedException(nameof(ARC4CryptoTransform),
InternalTools.GetResourceString("ObjectDisposed_Generic"));
_currentState.Dispose();
_currentState = (ARC4CryptoProvider)_initialState.Clone();
}
/// <summary>
/// Reset the <see cref = "ARC4CryptoTransform" /> instance state
/// using the specified <paramref name="key"/> and <paramref name="iv"/>.
/// </summary>
/// <param name = "key">
/// The secret key to be used for the <see cref="ARC4"/> algorithm.
/// </param>
/// <param name = "iv">
/// An initialization vector
/// used as the initial state of the ARC4 algorithm.
/// </param>
/// <exception cref="ObjectDisposedException">
/// Thrown if current instance of <see cref="ARC4CryptoTransform"/> is disposed.
/// </exception>
public void Reset(byte[] key, byte[] iv)
{
if (_disposed)
throw new ObjectDisposedException(nameof(ARC4CryptoTransform),
InternalTools.GetResourceString("ObjectDisposed_Generic"));
_currentState.Dispose();
_currentState = new ARC4CryptoProvider(iv);
_currentState.Update(key);
}
/// <summary>
/// Reset the instance <see cref = "ARC4CryptoTransform" />
/// using the specified <paramref name="key"/> and <paramref name="seed"/>.
/// </summary>
/// <param name = "key">
/// The secret key to be used for the <see cref="ARC4"/> algorithm.
/// </param>
/// <param name = "seed">
/// A signed 32-bit integer value used as the initial state of the <see cref="ARC4"/> algorithm.
/// </param>
/// <exception cref="ObjectDisposedException">
/// Thrown if current instance of <see cref="ARC4CryptoTransform"/> is disposed.
/// </exception>
public void Reset(byte[] key, int seed)
{
Reset(key, BitConverter.GetBytes(seed));
}
/// <summary>
/// Reset the instance <see cref = "ARC4CryptoTransform" />
/// using the specified <paramref name="key"/> and <paramref name="seed"/>.
/// </summary>
/// <param name = "key">
/// The secret key to be used for the <see cref="ARC4"/> algorithm.
/// </param>
/// <param name = "seed">
/// An unsigned 32-bit integer value used as the initial state of the <see cref="ARC4"/> algorithm.
/// </param>
/// <exception cref="ObjectDisposedException">
/// Thrown if current instance of <see cref="ARC4CryptoTransform"/> is disposed.
/// </exception>
public void Reset(byte[] key, uint seed)
{
Reset(key, BitConverter.GetBytes(seed));
}
/// <summary>
/// <inheritdoc cref="object.Finalize"/>.
/// </summary>
~ARC4CryptoTransform()
{
Dispose(false);
}
// Dispose current instance.
private void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
_initialState?.Dispose();
_currentState?.Dispose();
}
_initialState = null;
_currentState = null;
_disposed = true;
}
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
if (_disposed) return;
Dispose(true);
GC.SuppressFinalize(this);
}
}
}