Skip to content

Commit 5a7858a

Browse files
authored
Rijndael-->Aes (#4456)
1 parent 35a18cb commit 5a7858a

File tree

35 files changed

+142
-351
lines changed

35 files changed

+142
-351
lines changed

samples/snippets/cpp/VS_Snippets_CLR/Cryptography.XML.XMLEncImbedKey/CPP/sample.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ static void Encrypt( XmlDocument^ Doc, String^ ElementToEncrypt, RSA^ Alg, Strin
4040
// and use it to encrypt the XmlElement with the
4141
// a new random symmetric key.
4242
//////////////////////////////////////////////////
43-
// Create a 256 bit Rijndael key.
44-
RijndaelManaged^ sessionKey = gcnew RijndaelManaged;
43+
// Create a 256 bit Aes key.
44+
Aes^ sessionKey = Aes::Create();
4545
sessionKey->KeySize = 256;
4646
EncryptedXml^ eXml = gcnew EncryptedXml;
4747
array<Byte>^encryptedElement = eXml->EncryptData( elementToEncrypt, sessionKey, false );

samples/snippets/cpp/VS_Snippets_CLR/Cryptography.XML.XMLEncMapKey/CPP/Cryptography.XML.XMLEncMapKey.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void Encrypt( XmlDocument^ Doc, String^ ElementToEncrypt, SymmetricAlgori
6565
encryptionMethod = EncryptedXml::XmlEncDESUrl;
6666
}
6767
else
68-
if ( dynamic_cast<Rijndael^>(Alg) )
68+
if ( dynamic_cast<Aes^>(Alg) )
6969
{
7070
switch ( Alg->KeySize )
7171
{

samples/snippets/cpp/VS_Snippets_CLR/Cryptography.XML.XMLEncMinimalDecryptData/CPP/Cryptography.XML.XMLEncMinimalDecryptData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void Encrypt( XmlDocument^ Doc, String^ ElementToEncrypt, SymmetricAlgori
6565
encryptionMethod = EncryptedXml::XmlEncDESUrl;
6666
}
6767
else
68-
if ( dynamic_cast<Rijndael^>(Alg) )
68+
if ( dynamic_cast<Aes^>(Alg) )
6969
{
7070
switch ( Alg->KeySize )
7171
{

samples/snippets/cpp/VS_Snippets_CLR_Classic/classic CryptoStream Example/CPP/source.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using namespace System::ComponentModel;
88
using namespace System::Security::Cryptography;
99

1010
// <Snippet1>
11-
void EncryptData( String^ inName, String^ outName, array<Byte>^rijnKey, array<Byte>^rijnIV )
11+
void EncryptData( String^ inName, String^ outName, array<Byte>^aesKey, array<Byte>^aesIV )
1212
{
1313

1414
//Create the file streams to handle the input and output files.
@@ -24,9 +24,9 @@ void EncryptData( String^ inName, String^ outName, array<Byte>^rijnKey, array<By
2424

2525
int len; //This is the number of bytes to be written at a time.
2626

27-
SymmetricAlgorithm^ rijn = SymmetricAlgorithm::Create(); //Creates the default implementation, which is RijndaelManaged.
27+
Aes^ aes = Aes::Create();
2828

29-
CryptoStream^ encStream = gcnew CryptoStream( fout,rijn->CreateEncryptor( rijnKey, rijnIV ),CryptoStreamMode::Write );
29+
CryptoStream^ encStream = gcnew CryptoStream( fout,aes->CreateEncryptor( aesKey, aesIV ),CryptoStreamMode::Write );
3030
Console::WriteLine( "Encrypting..." );
3131

3232
//Read from the input file, then encrypt and write to the output file.

samples/snippets/cpp/VS_Snippets_CLR_System/system.Security.Cryptography.KeySizes/CPP/members.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,17 @@ namespace CryptographySample
2727

2828
// Create a new symmetric algorithm and display its
2929
// key values.
30-
SymmetricAlgorithm^ symAlg =
31-
SymmetricAlgorithm::Create();
32-
ShowKeys(symAlg->LegalKeySizes, symAlg->ToString());
33-
Console::WriteLine("rijn.blocksize:{0}",
34-
symAlg->BlockSize);
30+
Aes^ aes = Aes::Create();
31+
ShowKeys(aes->LegalKeySizes, aes->ToString());
32+
Console::WriteLine("aes.blocksize:{0}",
33+
aes->BlockSize);
3534

3635
// Create a new RSA algorithm and display its key values.
37-
RSACryptoServiceProvider^ rsaCSP =
38-
gcnew RSACryptoServiceProvider(384);
39-
ShowKeys(rsaCSP->LegalKeySizes, rsaCSP->ToString());
40-
Console::WriteLine("RSACryptoServiceProvider KeySize ="
36+
RSA^ rsa = RSA::Create();
37+
ShowKeys(rsa->LegalKeySizes, rsa->ToString());
38+
Console::WriteLine("RSA KeySize ="
4139
" {0}",
42-
rsaCSP->KeySize);
40+
rsa->KeySize);
4341

4442
Console::WriteLine("This sample completed successfully; "
4543
"press Enter to exit.");
@@ -96,17 +94,17 @@ int main()
9694
// Interval between key size bits: 64
9795
//
9896
// KeySizes retrieved from the
99-
// System.Security.Cryptography.RijndaelManaged object.
97+
// System.Security.Cryptography.Aes object.
10098
// Minimum key size bits: 128
10199
// Maximum key size bits: 256
102100
// Interval between key size bits: 64
103-
// rijn.blocksize:128
101+
// aes.blocksize:128
104102
//
105103
// KeySizes retrieved from the
106-
// System.Security.Cryptography.RSACryptoServiceProvider object.
107-
// Minimum key size bits: 384
104+
// System.Security.Cryptography.RSA object.
105+
// Minimum key size bits: 512
108106
// Maximum key size bits: 16384
109-
// Interval between key size bits: 8
110-
// RSACryptoServiceProvider KeySize = 384
107+
// Interval between key size bits: 64
108+
// RSA KeySize = 2048
111109
// This sample completed successfully; press Enter to exit.
112-
//</Snippet1>
110+
//</Snippet1>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int main()
3232
RSA->ImportParameters( RSAKeyInfo );
3333

3434
//Create a new instance of the Aes class.
35-
Aes^ aes = gcnew Aes;
35+
Aes^ aes = Aes::Create();
3636

3737
//Encrypt the symmetric key and IV.
3838
EncryptedSymmetricKey = RSA->Encrypt( aes->Key, false );

samples/snippets/csharp/VS_Snippets_CFX/s_ue_secureconversationservicecredential/cs/source.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ static void Configure(ServiceHost serviceHost)
7676
}
7777
public class CertificateSecurityStateEncoder : SecurityStateEncoder
7878
{
79-
RSACryptoServiceProvider rsaCryptoServiceProvider;
79+
RSA rsa;
8080
CookieContainerSerializer serializer;
81-
RijndaelManaged aesAlg;
81+
Aes aesAlg;
8282

8383
public CertificateSecurityStateEncoder(X509Certificate2 protectionCertificate)
8484
{
@@ -92,24 +92,24 @@ public CertificateSecurityStateEncoder(X509Certificate2 protectionCertificate)
9292
throw new ArgumentException("protectionCertificate does not contain the private key which is required for performing encypt / decrypt operations.");
9393
}
9494

95-
rsaCryptoServiceProvider = protectionCertificate.PrivateKey as RSACryptoServiceProvider;
96-
if (rsaCryptoServiceProvider == null)
95+
rsa = protectionCertificate.GetRSAPrivateKey();
96+
if (rsa == null)
9797
{
98-
throw new NotSupportedException("protectionCertificate must have a private key of type RSACryptoServiceProvider.");
98+
throw new NotSupportedException("protectionCertificate must have a private key of type RSA.");
9999
}
100100

101101
serializer = new CookieContainerSerializer();
102102

103103
// The symmetric key algorithm used to protect the cookie.
104-
aesAlg = new RijndaelManaged();
104+
aesAlg = Aes.Create();
105105
}
106106

107107
protected override byte[] EncodeSecurityState(byte[] data)
108108
{
109109
// Create a new cookie container that will protect the WCF cookie.
110110
// Possible improvement: use a caching scheme so that a new cookie container
111111
// need not be created each time to improve performance.
112-
CookieContainer cookieContainer = new CookieContainer(rsaCryptoServiceProvider, aesAlg);
112+
CookieContainer cookieContainer = new CookieContainer(rsa, aesAlg);
113113

114114
// Encrypt the cookie from WCF with our own scheme so that any of the backend services
115115
// can decrypt it.
@@ -123,7 +123,7 @@ protected override byte[] DecodeSecurityState(byte[] data)
123123
{
124124
// Possible improvement: use a caching scheme so that a new cookie container
125125
// need not be created each time to improve performance.
126-
CookieContainer cookieContainer = serializer.Deserialize(rsaCryptoServiceProvider, aesAlg, data);
126+
CookieContainer cookieContainer = serializer.Deserialize(rsa, aesAlg, data);
127127

128128
// Decrypt the cookie and return it to WCF so that WCF can use the cookie to
129129
// perform its own cryptographic operations.
@@ -139,7 +139,7 @@ class CookieContainerSerializer
139139
/// <param name="aesAlg">The symmetric key algorithm to use to decrypt the cookie block.</param>
140140
/// <param name="data">The byte array to deserialize.</param>
141141
/// <returns>The deserialized cookie container instance.</returns>
142-
public CookieContainer Deserialize(RSACryptoServiceProvider rsaKey, RijndaelManaged aesAlg, byte[] data)
142+
public CookieContainer Deserialize(RSA rsaKey, Aes aesAlg, byte[] data)
143143
{
144144
CookieContainer cookieContainer = new CookieContainer(rsaKey, aesAlg);
145145
// Length of the IV according to the AES algorithm (in bytes).
@@ -221,23 +221,23 @@ class CookieContainer
221221
byte[] encryptedCookie;
222222
ICryptoTransform encryptor;
223223
ICryptoTransform decryptor;
224-
RijndaelManaged aesAlg;
225-
RSACryptoServiceProvider protectionRsaKey;
224+
Aes aesAlg;
225+
RSA protectionRsaKey;
226226

227227
/// <summary>
228228
/// Creates a new cookie container and auto-generate a symmetric key protected
229229
/// with the RSA key.
230230
/// </summary>
231231
/// <param name="rsaKey">The RSA key to protect the generated symmetric key.</param>
232232
/// <param name="aesAlg">The symmetric key algorithm to use.</param>
233-
public CookieContainer(RSACryptoServiceProvider rsaKey, RijndaelManaged aesAlg)
233+
public CookieContainer(RSA rsaKey, Aes aesAlg)
234234
{
235235
this.aesAlg = aesAlg;
236236
this.iv = aesAlg.IV;
237237

238238
// Use the RSA key in the X509Certificate to protect the symmetric key.
239239
this.protectionRsaKey = rsaKey;
240-
this.encryptedSymmetricKey = protectionRsaKey.Encrypt(aesAlg.Key, true);
240+
this.encryptedSymmetricKey = protectionRsaKey.Encrypt(aesAlg.Key, RSAEncryptionPadding.OaepSHA1);
241241

242242
// Create the enryptor and decryptor that will perform the actual
243243
// cryptographic operations.
@@ -266,7 +266,7 @@ public void CreateCryptoTransformers()
266266
{
267267
// Only a service configured with the right X509 certificate
268268
// can decrypt the symmetric key.
269-
byte[] symmetricKey = protectionRsaKey.Decrypt(encryptedSymmetricKey, true);
269+
byte[] symmetricKey = protectionRsaKey.Decrypt(encryptedSymmetricKey, RSAEncryptionPadding.OaepSHA1);
270270

271271
// Create an encryptor based on the symmetric key which can be used to encrypt SCT cookie blob.
272272
this.encryptor = aesAlg.CreateEncryptor(symmetricKey, iv);

samples/snippets/csharp/VS_Snippets_CLR/Cryptography.XML.DataReference/cs/sample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, string Encr
9696
// a new random symmetric key.
9797
//////////////////////////////////////////////////
9898

99-
// Create a 256 bit Rijndael key.
100-
RijndaelManaged sessionKey = new RijndaelManaged();
99+
// Create a 256 bit Aes key.
100+
Aes sessionKey = Aes.Create();
101101
sessionKey.KeySize = 256;
102102

103103
EncryptedXml eXml = new EncryptedXml();

samples/snippets/csharp/VS_Snippets_CLR/Cryptography.XML.EncryptedData/cs/sample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, st
7878
// a new random symmetric key.
7979
//////////////////////////////////////////////////
8080

81-
// Create a 256 bit Rijndael key.
82-
RijndaelManaged sessionKey = new RijndaelManaged();
81+
// Create a 256 bit Aes key.
82+
Aes sessionKey = Aes.Create();
8383
sessionKey.KeySize = 256;
8484

8585
EncryptedXml eXml = new EncryptedXml();

samples/snippets/csharp/VS_Snippets_CLR/Cryptography.XML.EncryptedKey/cs/example.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, st
7878
// a new random symmetric key.
7979
//////////////////////////////////////////////////
8080

81-
// Create a 256 bit Rijndael key.
82-
RijndaelManaged sessionKey = new RijndaelManaged();
81+
// Create a 256 bit Aes key.
82+
Aes sessionKey = Aes.Create();
8383
sessionKey.KeySize = 256;
8484

8585
EncryptedXml eXml = new EncryptedXml();

0 commit comments

Comments
 (0)