@@ -17,6 +17,15 @@ public class Crypto
1717 // This is divided by 8 later to get the equivalent number of bytes.
1818 private const int KeySize = 256 ;
1919
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+
2029 // Determines the number of iterations used during password generation.
2130 private const int DerivationIterations = 1000 ;
2231
@@ -60,8 +69,8 @@ public static string Encrypt(string plainText, string passPhrase)
6069 public static byte [ ] Encrypt ( byte [ ] plainBytes , string passPhrase )
6170 {
6271 // 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 ) ;
6574
6675 // Prepare store for encrypted bytes.
6776 byte [ ] encryptedBytes ;
@@ -70,9 +79,9 @@ public static byte[] Encrypt(byte[] plainBytes, string passPhrase)
7079 {
7180 byte [ ] keyBytes = password . GetBytes ( KeySize / 8 ) ;
7281
73- using ( RijndaelManaged symmetricKey = new RijndaelManaged ( ) )
82+ using ( AesManaged symmetricKey = new AesManaged ( ) )
7483 {
75- symmetricKey . BlockSize = 256 ;
84+ symmetricKey . BlockSize = BlockSize ;
7685 symmetricKey . Mode = CipherMode . CBC ;
7786 symmetricKey . Padding = PaddingMode . PKCS7 ;
7887
@@ -144,14 +153,22 @@ public static string Decrypt(string encryptedText, string passPhrase)
144153
145154 public static byte [ ] Decrypt ( byte [ ] encryptedBytesWithSaltAndIv , string passPhrase )
146155 {
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 ( ) ;
155172
156173 // Prepare store for decrypted string and bytes read.
157174 byte [ ] plainTextBytes ;
@@ -161,9 +178,9 @@ public static byte[] Decrypt(byte[] encryptedBytesWithSaltAndIv, string passPhra
161178 {
162179 byte [ ] keyBytes = password . GetBytes ( KeySize / 8 ) ;
163180
164- using ( RijndaelManaged symmetricKey = new RijndaelManaged ( ) )
181+ using ( AesManaged symmetricKey = new AesManaged ( ) )
165182 {
166- symmetricKey . BlockSize = 256 ;
183+ symmetricKey . BlockSize = BlockSize ;
167184 symmetricKey . Mode = CipherMode . CBC ;
168185 symmetricKey . Padding = PaddingMode . PKCS7 ;
169186
@@ -187,14 +204,9 @@ public static byte[] Decrypt(byte[] encryptedBytesWithSaltAndIv, string passPhra
187204 return plainTextBytes . Take ( decryptedByteCount ) . ToArray ( ) ;
188205 }
189206
190- private static byte [ ] Generate256BitsOfRandomEntropy ( )
207+ private static byte [ ] GenerateRandomEntropy ( int bitCount )
191208 {
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 ) ;
198210
199211 return randomBytes ;
200212 }
0 commit comments