@@ -17,6 +17,15 @@ public class Crypto
17
17
// This is divided by 8 later to get the equivalent number of bytes.
18
18
private const int KeySize = 256 ;
19
19
20
+ // The AES specification states that the block size must be 128.
21
+ private const int BlockSize = 128 ;
22
+
23
+ // Initialisation vector size.
24
+ private const int IvSize = 128 ;
25
+
26
+ // Salt size.
27
+ private const int SaltSize = 256 ;
28
+
20
29
// Determines the number of iterations used during password generation.
21
30
private const int DerivationIterations = 1000 ;
22
31
@@ -60,8 +69,8 @@ public static string Encrypt(string plainText, string passPhrase)
60
69
public static byte [ ] Encrypt ( byte [ ] plainBytes , string passPhrase )
61
70
{
62
71
// Bytes for salt and initialisation vector are generated randomly each time.
63
- byte [ ] saltBytes = Generate256BitsOfRandomEntropy ( ) ;
64
- byte [ ] ivBytes = Generate256BitsOfRandomEntropy ( ) ;
72
+ byte [ ] saltBytes = GenerateRandomEntropy ( SaltSize ) ;
73
+ byte [ ] ivBytes = GenerateRandomEntropy ( IvSize ) ;
65
74
66
75
// Prepare store for encrypted bytes.
67
76
byte [ ] encryptedBytes ;
@@ -70,9 +79,9 @@ public static byte[] Encrypt(byte[] plainBytes, string passPhrase)
70
79
{
71
80
byte [ ] keyBytes = password . GetBytes ( KeySize / 8 ) ;
72
81
73
- using ( RijndaelManaged symmetricKey = new RijndaelManaged ( ) )
82
+ using ( AesManaged symmetricKey = new AesManaged ( ) )
74
83
{
75
- symmetricKey . BlockSize = 256 ;
84
+ symmetricKey . BlockSize = BlockSize ;
76
85
symmetricKey . Mode = CipherMode . CBC ;
77
86
symmetricKey . Padding = PaddingMode . PKCS7 ;
78
87
@@ -144,14 +153,22 @@ public static string Decrypt(string encryptedText, string passPhrase)
144
153
145
154
public static byte [ ] Decrypt ( byte [ ] encryptedBytesWithSaltAndIv , string passPhrase )
146
155
{
147
- // Get the salt bytes by extracting the first 32 bytes.
148
- byte [ ] saltBytes = encryptedBytesWithSaltAndIv . Take ( KeySize / 8 ) . ToArray ( ) ;
149
-
150
- // Get the initialisation vector bytes by extracting the next 32 bytes after the salt.
151
- byte [ ] ivBytes = encryptedBytesWithSaltAndIv . Skip ( KeySize / 8 ) . Take ( KeySize / 8 ) . ToArray ( ) ;
152
-
153
- // Get the actual encrypted bytes by removing the first 64 bytes.
154
- byte [ ] encryptedBytes = encryptedBytesWithSaltAndIv . Skip ( ( KeySize / 8 ) * 2 ) . Take ( encryptedBytesWithSaltAndIv . Length - ( ( KeySize / 8 ) * 2 ) ) . ToArray ( ) ;
156
+ // Get the salt bytes by extracting the first (SaltSize / 8) bytes.
157
+ byte [ ] saltBytes = encryptedBytesWithSaltAndIv
158
+ . Take ( SaltSize / 8 )
159
+ . ToArray ( ) ;
160
+
161
+ // Get the initialisation vector bytes by extracting the next (IvSize / 8) bytes after the salt.
162
+ byte [ ] ivBytes = encryptedBytesWithSaltAndIv
163
+ . Skip ( SaltSize / 8 )
164
+ . Take ( IvSize / 8 )
165
+ . ToArray ( ) ;
166
+
167
+ // Get the actual encrypted bytes by removing the salt and iv bytes.
168
+ byte [ ] encryptedBytes = encryptedBytesWithSaltAndIv
169
+ . Skip ( ( SaltSize / 8 ) + ( IvSize / 8 ) )
170
+ . Take ( encryptedBytesWithSaltAndIv . Length - ( ( SaltSize / 8 ) + ( IvSize / 8 ) ) )
171
+ . ToArray ( ) ;
155
172
156
173
// Prepare store for decrypted string and bytes read.
157
174
byte [ ] plainTextBytes ;
@@ -161,9 +178,9 @@ public static byte[] Decrypt(byte[] encryptedBytesWithSaltAndIv, string passPhra
161
178
{
162
179
byte [ ] keyBytes = password . GetBytes ( KeySize / 8 ) ;
163
180
164
- using ( RijndaelManaged symmetricKey = new RijndaelManaged ( ) )
181
+ using ( AesManaged symmetricKey = new AesManaged ( ) )
165
182
{
166
- symmetricKey . BlockSize = 256 ;
183
+ symmetricKey . BlockSize = BlockSize ;
167
184
symmetricKey . Mode = CipherMode . CBC ;
168
185
symmetricKey . Padding = PaddingMode . PKCS7 ;
169
186
@@ -187,14 +204,9 @@ public static byte[] Decrypt(byte[] encryptedBytesWithSaltAndIv, string passPhra
187
204
return plainTextBytes . Take ( decryptedByteCount ) . ToArray ( ) ;
188
205
}
189
206
190
- private static byte [ ] Generate256BitsOfRandomEntropy ( )
207
+ private static byte [ ] GenerateRandomEntropy ( int bitCount )
191
208
{
192
- byte [ ] randomBytes = new byte [ 32 ] ;
193
-
194
- using ( RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider ( ) )
195
- {
196
- rngCsp . GetBytes ( randomBytes ) ;
197
- }
209
+ byte [ ] randomBytes = CryptoUtilities . GenerateRandomBytes ( bitCount / 8 ) ;
198
210
199
211
return randomBytes ;
200
212
}
0 commit comments