Skip to content

Commit 75eb938

Browse files
authored
replace rijndael etc (#4415)
* replace rijndael etc * restore snippet tags * address feedback * AesManaged-->Aes
1 parent e46ead7 commit 75eb938

File tree

8 files changed

+59
-63
lines changed
  • samples/snippets
    • cpp
      • VS_Snippets_CLR_System/system.Security.Cryptography.RSACryptoServiceProvider.Encrypt/CPP
      • VS_Snippets_CLR/rfc28981/CPP
    • csharp
      • VS_Snippets_CLR_System/system.Security.Cryptography.RSACryptoServiceProvider.Encrypt/CS
      • VS_Snippets_CLR
    • visualbasic
      • VS_Snippets_CLR_System/system.Security.Cryptography.RSACryptoServiceProvider.Encrypt/VB
      • VS_Snippets_CLR

8 files changed

+59
-63
lines changed

samples/snippets/cpp/VS_Snippets_CLR/rfc28981/CPP/rfc28981.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int main()
4646
//</SNIPPET4>
4747

4848
// Encrypt the data.
49-
TripleDES^ encAlg = TripleDES::Create();
49+
Aes^ encAlg = Aes::Create();
5050
encAlg->Key = k1->GetBytes( 16 );
5151
MemoryStream^ encryptionStream = gcnew MemoryStream;
5252
CryptoStream^ encrypt = gcnew CryptoStream( encryptionStream,encAlg->CreateEncryptor(),CryptoStreamMode::Write );
@@ -60,7 +60,7 @@ int main()
6060
k1->Reset();
6161

6262
// Try to decrypt, thus showing it can be round-tripped.
63-
TripleDES^ decAlg = TripleDES::Create();
63+
Aes^ decAlg = Aes::Create();
6464
decAlg->Key = k2->GetBytes( 16 );
6565
decAlg->IV = encAlg->IV;
6666
MemoryStream^ decryptionStreamBacking = gcnew MemoryStream;

samples/snippets/cpp/VS_Snippets_CLR_System/system.Security.Cryptography.RSACryptoServiceProvider.Encrypt/CPP/sample.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ int main()
3131
//Import key parameters into RSA.
3232
RSA->ImportParameters( RSAKeyInfo );
3333

34-
//Create a new instance of the RijndaelManaged class.
35-
RijndaelManaged^ RM = gcnew RijndaelManaged;
34+
//Create a new instance of the Aes class.
35+
Aes^ aes = gcnew Aes;
3636

3737
//Encrypt the symmetric key and IV.
38-
EncryptedSymmetricKey = RSA->Encrypt( RM->Key, false );
39-
EncryptedSymmetricIV = RSA->Encrypt( RM->IV, false );
40-
Console::WriteLine( "RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider." );
38+
EncryptedSymmetricKey = RSA->Encrypt( aes->Key, false );
39+
EncryptedSymmetricIV = RSA->Encrypt( aes->IV, false );
40+
Console::WriteLine( "Aes Key and IV have been encrypted with RSACryptoServiceProvider." );
4141
}
4242
catch ( CryptographicException^ e )
4343
{

samples/snippets/csharp/VS_Snippets_CLR/rfc28981/CS/rfc28981.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void Main(string[] passwordargs)
4848
Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1, salt1);
4949
//</SNIPPET4>
5050
// Encrypt the data.
51-
TripleDES encAlg = TripleDES.Create();
51+
Aes encAlg = Aes.Create();
5252
encAlg.Key = k1.GetBytes(16);
5353
MemoryStream encryptionStream = new MemoryStream();
5454
CryptoStream encrypt = new CryptoStream(encryptionStream,
@@ -64,7 +64,7 @@ public static void Main(string[] passwordargs)
6464
k1.Reset();
6565

6666
// Try to decrypt, thus showing it can be round-tripped.
67-
TripleDES decAlg = TripleDES.Create();
67+
Aes decAlg = Aes.Create();
6868
decAlg.Key = k2.GetBytes(16);
6969
decAlg.IV = encAlg.IV;
7070
MemoryStream decryptionStreamBacking = new MemoryStream();

samples/snippets/csharp/VS_Snippets_CLR/x509certificate2/cs/program.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,16 @@ private static X509Certificate2 GetCertificateFromStore(string certName)
8282
// Encrypt a file using a public key.
8383
private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPublicKey)
8484
{
85-
using (AesManaged aesManaged = new AesManaged())
85+
using (Aes aes = new Aes())
8686
{
87-
// Create instance of AesManaged for
87+
// Create instance of Aes for
8888
// symetric encryption of the data.
89-
aesManaged.KeySize = 256;
90-
aesManaged.BlockSize = 128;
91-
aesManaged.Mode = CipherMode.CBC;
92-
using (ICryptoTransform transform = aesManaged.CreateEncryptor())
89+
aes.KeySize = 256;
90+
aes.Mode = CipherMode.CBC;
91+
using (ICryptoTransform transform = aes.CreateEncryptor())
9392
{
9493
RSAPKCS1KeyExchangeFormatter keyFormatter = new RSAPKCS1KeyExchangeFormatter(rsaPublicKey);
95-
byte[] keyEncrypted = keyFormatter.CreateKeyExchange(aesManaged.Key, aesManaged.GetType());
94+
byte[] keyEncrypted = keyFormatter.CreateKeyExchange(aes.Key, aes.GetType());
9695

9796
// Create byte arrays to contain
9897
// the length values of the key and IV.
@@ -101,7 +100,7 @@ private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPubli
101100

102101
int lKey = keyEncrypted.Length;
103102
LenK = BitConverter.GetBytes(lKey);
104-
int lIV = aesManaged.IV.Length;
103+
int lIV = aes.IV.Length;
105104
LenIV = BitConverter.GetBytes(lIV);
106105

107106
// Write the following to the FileStream
@@ -123,7 +122,7 @@ private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPubli
123122
outFs.Write(LenK, 0, 4);
124123
outFs.Write(LenIV, 0, 4);
125124
outFs.Write(keyEncrypted, 0, lKey);
126-
outFs.Write(aesManaged.IV, 0, lIV);
125+
outFs.Write(aes.IV, 0, lIV);
127126

128127
// Now write the cipher text using
129128
// a CryptoStream for encrypting.
@@ -137,7 +136,7 @@ private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPubli
137136
int offset = 0;
138137

139138
// blockSizeBytes can be any arbitrary size.
140-
int blockSizeBytes = aesManaged.BlockSize / 8;
139+
int blockSizeBytes = aes.BlockSize / 8;
141140
byte[] data = new byte[blockSizeBytes];
142141
int bytesRead = 0;
143142

@@ -169,13 +168,12 @@ private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPubli
169168
private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPrivateKey)
170169
{
171170

172-
// Create instance of AesManaged for
171+
// Create instance of Aes for
173172
// symetric decryption of the data.
174-
using (AesManaged aesManaged = new AesManaged())
173+
using (Aes aes = new Aes())
175174
{
176-
aesManaged.KeySize = 256;
177-
aesManaged.BlockSize = 128;
178-
aesManaged.Mode = CipherMode.CBC;
175+
aes.KeySize = 256;
176+
aes.Mode = CipherMode.CBC;
179177

180178
// Create byte arrays to get the length of
181179
// the encrypted key and IV.
@@ -202,14 +200,14 @@ private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPriva
202200
int lenK = BitConverter.ToInt32(LenK, 0);
203201
int lenIV = BitConverter.ToInt32(LenIV, 0);
204202

205-
// Determine the start postition of
206-
// the ciphter text (startC)
203+
// Determine the start position of
204+
// the cipher text (startC)
207205
// and its length(lenC).
208206
int startC = lenK + lenIV + 8;
209207
int lenC = (int)inFs.Length - startC;
210208

211209
// Create the byte arrays for
212-
// the encrypted AesManaged key,
210+
// the encrypted Aes key,
213211
// the IV, and the cipher text.
214212
byte[] KeyEncrypted = new byte[lenK];
215213
byte[] IV = new byte[lenIV];
@@ -224,11 +222,11 @@ private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPriva
224222
Directory.CreateDirectory(decrFolder);
225223
//<Snippet10>
226224
// Use RSACryptoServiceProvider
227-
// to decrypt the AesManaged key.
225+
// to decrypt the Aes key.
228226
byte[] KeyDecrypted = rsaPrivateKey.Decrypt(KeyEncrypted, false);
229227

230228
// Decrypt the key.
231-
using (ICryptoTransform transform = aesManaged.CreateDecryptor(KeyDecrypted, IV))
229+
using (ICryptoTransform transform = aes.CreateDecryptor(KeyDecrypted, IV))
232230
{
233231
//</Snippet10>
234232

@@ -242,7 +240,7 @@ private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPriva
242240
int count = 0;
243241
int offset = 0;
244242

245-
int blockSizeBytes = aesManaged.BlockSize / 8;
243+
int blockSizeBytes = aes.BlockSize / 8;
246244
byte[] data = new byte[blockSizeBytes];
247245

248246
// By decrypting a chunk a time,

samples/snippets/csharp/VS_Snippets_CLR_System/system.Security.Cryptography.RSACryptoServiceProvider.Encrypt/CS/sample.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ static void Main()
3737
//Import key parameters into RSA.
3838
RSA.ImportParameters(RSAKeyInfo);
3939

40-
//Create a new instance of the RijndaelManaged class.
41-
RijndaelManaged RM = new RijndaelManaged();
40+
//Create a new instance of the Aes class.
41+
Aes aes = new Aes();
4242

4343
//Encrypt the symmetric key and IV.
44-
EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
45-
EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);
44+
EncryptedSymmetricKey = RSA.Encrypt(aes.Key, false);
45+
EncryptedSymmetricIV = RSA.Encrypt(aes.IV, false);
4646

47-
Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider.");
47+
Console.WriteLine("Aes Key and IV have been encrypted with RSACryptoServiceProvider.");
4848
}
4949
//Catch and display a CryptographicException
5050
//to the console.

samples/snippets/visualbasic/VS_Snippets_CLR/rfc28981/vb/rfc28981.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Public Class rfc2898test
3838
Dim k2 As New Rfc2898DeriveBytes(pwd1, salt1)
3939
'</SNIPPET4>
4040
' Encrypt the data.
41-
Dim encAlg As TripleDES = TripleDES.Create()
41+
Dim encAlg As Aes = Aes.Create()
4242
encAlg.Key = k1.GetBytes(16)
4343
Dim encryptionStream As New MemoryStream()
4444
Dim encrypt As New CryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write)
@@ -51,7 +51,7 @@ Public Class rfc2898test
5151
k1.Reset()
5252

5353
' Try to decrypt, thus showing it can be round-tripped.
54-
Dim decAlg As TripleDES = TripleDES.Create()
54+
Dim decAlg As Aes = Aes.Create()
5555
decAlg.Key = k2.GetBytes(16)
5656
decAlg.IV = encAlg.IV
5757
Dim decryptionStreamBacking As New MemoryStream()

samples/snippets/visualbasic/VS_Snippets_CLR/x509certificate2/vb/program.vb

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,16 @@ Class Program
7777
' <Snippet3>
7878
' Encrypt a file using a public key.
7979
Private Shared Sub EncryptFile(ByVal inFile As String, ByVal rsaPublicKey As RSACryptoServiceProvider)
80-
Dim aesManaged As New AesManaged()
80+
Dim aes As New Aes()
8181
Try
82-
' Create instance of AesManaged for
82+
' Create instance of Aes for
8383
' symetric encryption of the data.
84-
aesManaged.KeySize = 256
85-
aesManaged.BlockSize = 128
86-
aesManaged.Mode = CipherMode.CBC
87-
Dim transform As ICryptoTransform = aesManaged.CreateEncryptor()
84+
aes.KeySize = 256
85+
aes.Mode = CipherMode.CBC
86+
Dim transform As ICryptoTransform = aes.CreateEncryptor()
8887
Try
8988
Dim keyFormatter As New RSAPKCS1KeyExchangeFormatter(rsaPublicKey)
90-
Dim keyEncrypted As Byte() = keyFormatter.CreateKeyExchange(aesManaged.Key, aesManaged.GetType())
89+
Dim keyEncrypted As Byte() = keyFormatter.CreateKeyExchange(aes.Key, aes.GetType())
9190

9291
' Create byte arrays to contain
9392
' the length values of the key and IV.
@@ -96,7 +95,7 @@ Class Program
9695

9796
Dim lKey As Integer = keyEncrypted.Length
9897
LenK = BitConverter.GetBytes(lKey)
99-
Dim lIV As Integer = aesManaged.IV.Length
98+
Dim lIV As Integer = aesM.IV.Length
10099
LenIV = BitConverter.GetBytes(lIV)
101100

102101
' Write the following to the FileStream
@@ -117,7 +116,7 @@ Class Program
117116
outFs.Write(LenK, 0, 4)
118117
outFs.Write(LenIV, 0, 4)
119118
outFs.Write(keyEncrypted, 0, lKey)
120-
outFs.Write(aesManaged.IV, 0, lIV)
119+
outFs.Write(aes.IV, 0, lIV)
121120

122121
' Now write the cipher text using
123122
' a CryptoStream for encrypting.
@@ -131,7 +130,7 @@ Class Program
131130
Dim offset As Integer = 0
132131

133132
' blockSizeBytes can be any arbitrary size.
134-
Dim blockSizeBytes As Integer = aesManaged.BlockSize / 8
133+
Dim blockSizeBytes As Integer = aes.BlockSize / 8
135134
Dim data(blockSizeBytes) As Byte
136135
Dim bytesRead As Integer = 0
137136

@@ -160,7 +159,7 @@ Class Program
160159
transform.Dispose()
161160
End Try
162161
Finally
163-
aesManaged.Dispose()
162+
aes.Dispose()
164163
End Try
165164

166165
End Sub
@@ -171,13 +170,12 @@ Class Program
171170
' Decrypt a file using a private key.
172171
Private Shared Sub DecryptFile(ByVal inFile As String, ByVal rsaPrivateKey As RSACryptoServiceProvider)
173172

174-
' Create instance of AesManaged for
173+
' Create instance of Aes for
175174
' symetric decryption of the data.
176-
Dim aesManaged As New AesManaged()
175+
Dim aes As New Aes()
177176
Try
178-
aesManaged.KeySize = 256
179-
aesManaged.BlockSize = 128
180-
aesManaged.Mode = CipherMode.CBC
177+
aes.KeySize = 256
178+
aes.Mode = CipherMode.CBC
181179

182180
' Create byte arrays to get the length of
183181
' the encrypted key and IV.
@@ -211,7 +209,7 @@ Class Program
211209
Dim lenC As Integer = (CType(inFs.Length, Integer) - startC)
212210

213211
' Create the byte arrays for
214-
' the encrypted Rijndael key,
212+
' the encrypted AES key,
215213
' the IV, and the cipher text.
216214
Dim KeyEncrypted() As Byte = New Byte(lengthK - 1) {}
217215
Dim IV() As Byte = New Byte(lengthIV - 1) {}
@@ -226,11 +224,11 @@ Class Program
226224
Directory.CreateDirectory(decrFolder)
227225
'<Snippet10>
228226
' Use RSACryptoServiceProvider
229-
' to decrypt the Rijndael key.
227+
' to decrypt the AES key.
230228
Dim KeyDecrypted As Byte() = rsaPrivateKey.Decrypt(KeyEncrypted, False)
231229

232230
' Decrypt the key.
233-
Dim transform As ICryptoTransform = aesManaged.CreateDecryptor(KeyDecrypted, IV)
231+
Dim transform As ICryptoTransform = aes.CreateDecryptor(KeyDecrypted, IV)
234232
'</Snippet10>
235233
' Decrypt the cipher text from
236234
' from the FileSteam of the encrypted
@@ -246,7 +244,7 @@ Class Program
246244
Dim count As Integer = 0
247245
Dim offset As Integer = 0
248246

249-
Dim blockSizeBytes As Integer = aesManaged.BlockSize / 8
247+
Dim blockSizeBytes As Integer = aes.BlockSize / 8
250248
Dim data(blockSizeBytes) As Byte
251249

252250
' By decrypting a chunk a time,
@@ -280,7 +278,7 @@ Class Program
280278
End Try
281279

282280
Finally
283-
aesManaged.Dispose()
281+
aes.Dispose()
284282
End Try
285283

286284

samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Security.Cryptography.RSACryptoServiceProvider.Encrypt/VB/sample.vb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ Class RSACSPSample
2727
'Import key parameters into RSA.
2828
RSA.ImportParameters(RSAKeyInfo)
2929

30-
'Create a new instance of the RijndaelManaged class.
31-
Dim RM As New RijndaelManaged()
30+
'Create a new instance of the Aes class.
31+
Dim aes As New Aes()
3232

3333
'Encrypt the symmetric key and IV.
34-
EncryptedSymmetricKey = RSA.Encrypt(RM.Key, False)
35-
EncryptedSymmetricIV = RSA.Encrypt(RM.IV, False)
34+
EncryptedSymmetricKey = RSA.Encrypt(aes.Key, False)
35+
EncryptedSymmetricIV = RSA.Encrypt(aes.IV, False)
3636

37-
Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider.")
37+
Console.WriteLine("Aes Key and IV have been encrypted with RSACryptoServiceProvider.")
3838

3939
'Catch and display a CryptographicException
4040
'to the console.

0 commit comments

Comments
 (0)