Skip to content

Rijndael-->Aes #4456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ static void Encrypt( XmlDocument^ Doc, String^ ElementToEncrypt, RSA^ Alg, Strin
// and use it to encrypt the XmlElement with the
// a new random symmetric key.
//////////////////////////////////////////////////
// Create a 256 bit Rijndael key.
RijndaelManaged^ sessionKey = gcnew RijndaelManaged;
// Create a 256 bit Aes key.
Aes^ sessionKey = Aes::Create();
sessionKey->KeySize = 256;
EncryptedXml^ eXml = gcnew EncryptedXml;
array<Byte>^encryptedElement = eXml->EncryptData( elementToEncrypt, sessionKey, false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void Encrypt( XmlDocument^ Doc, String^ ElementToEncrypt, SymmetricAlgori
encryptionMethod = EncryptedXml::XmlEncDESUrl;
}
else
if ( dynamic_cast<Rijndael^>(Alg) )
if ( dynamic_cast<Aes^>(Alg) )
{
switch ( Alg->KeySize )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void Encrypt( XmlDocument^ Doc, String^ ElementToEncrypt, SymmetricAlgori
encryptionMethod = EncryptedXml::XmlEncDESUrl;
}
else
if ( dynamic_cast<Rijndael^>(Alg) )
if ( dynamic_cast<Aes^>(Alg) )
{
switch ( Alg->KeySize )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace System::ComponentModel;
using namespace System::Security::Cryptography;

// <Snippet1>
void EncryptData( String^ inName, String^ outName, array<Byte>^rijnKey, array<Byte>^rijnIV )
void EncryptData( String^ inName, String^ outName, array<Byte>^aesKey, array<Byte>^aesIV )
{

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

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

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

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

//Read from the input file, then encrypt and write to the output file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ namespace CryptographySample

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

// Create a new RSA algorithm and display its key values.
RSACryptoServiceProvider^ rsaCSP =
gcnew RSACryptoServiceProvider(384);
ShowKeys(rsaCSP->LegalKeySizes, rsaCSP->ToString());
Console::WriteLine("RSACryptoServiceProvider KeySize ="
RSA^ rsa = RSA::Create();
ShowKeys(rsa->LegalKeySizes, rsa->ToString());
Console::WriteLine("RSA KeySize ="
" {0}",
rsaCSP->KeySize);
rsa->KeySize);

Console::WriteLine("This sample completed successfully; "
"press Enter to exit.");
Expand Down Expand Up @@ -96,17 +94,17 @@ int main()
// Interval between key size bits: 64
//
// KeySizes retrieved from the
// System.Security.Cryptography.RijndaelManaged object.
// System.Security.Cryptography.Aes object.
// Minimum key size bits: 128
// Maximum key size bits: 256
// Interval between key size bits: 64
// rijn.blocksize:128
// aes.blocksize:128
//
// KeySizes retrieved from the
// System.Security.Cryptography.RSACryptoServiceProvider object.
// Minimum key size bits: 384
// System.Security.Cryptography.RSA object.
// Minimum key size bits: 512
// Maximum key size bits: 16384
// Interval between key size bits: 8
// RSACryptoServiceProvider KeySize = 384
// Interval between key size bits: 64
// RSA KeySize = 2048
// This sample completed successfully; press Enter to exit.
//</Snippet1>
//</Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main()
RSA->ImportParameters( RSAKeyInfo );

//Create a new instance of the Aes class.
Aes^ aes = gcnew Aes;
Aes^ aes = Aes::Create();

//Encrypt the symmetric key and IV.
EncryptedSymmetricKey = RSA->Encrypt( aes->Key, false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ static void Configure(ServiceHost serviceHost)
}
public class CertificateSecurityStateEncoder : SecurityStateEncoder
{
RSACryptoServiceProvider rsaCryptoServiceProvider;
RSA rsa;
CookieContainerSerializer serializer;
RijndaelManaged aesAlg;
Aes aesAlg;

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

rsaCryptoServiceProvider = protectionCertificate.PrivateKey as RSACryptoServiceProvider;
if (rsaCryptoServiceProvider == null)
rsa = protectionCertificate.GetRSAPrivateKey();
if (rsa == null)
{
throw new NotSupportedException("protectionCertificate must have a private key of type RSACryptoServiceProvider.");
throw new NotSupportedException("protectionCertificate must have a private key of type RSA.");
}

serializer = new CookieContainerSerializer();

// The symmetric key algorithm used to protect the cookie.
aesAlg = new RijndaelManaged();
aesAlg = Aes.Create();
}

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

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

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

/// <summary>
/// Creates a new cookie container and auto-generate a symmetric key protected
/// with the RSA key.
/// </summary>
/// <param name="rsaKey">The RSA key to protect the generated symmetric key.</param>
/// <param name="aesAlg">The symmetric key algorithm to use.</param>
public CookieContainer(RSACryptoServiceProvider rsaKey, RijndaelManaged aesAlg)
public CookieContainer(RSA rsaKey, Aes aesAlg)
{
this.aesAlg = aesAlg;
this.iv = aesAlg.IV;

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

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

// Create an encryptor based on the symmetric key which can be used to encrypt SCT cookie blob.
this.encryptor = aesAlg.CreateEncryptor(symmetricKey, iv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, string Encr
// a new random symmetric key.
//////////////////////////////////////////////////

// Create a 256 bit Rijndael key.
RijndaelManaged sessionKey = new RijndaelManaged();
// Create a 256 bit Aes key.
Aes sessionKey = Aes.Create();
sessionKey.KeySize = 256;

EncryptedXml eXml = new EncryptedXml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, st
// a new random symmetric key.
//////////////////////////////////////////////////

// Create a 256 bit Rijndael key.
RijndaelManaged sessionKey = new RijndaelManaged();
// Create a 256 bit Aes key.
Aes sessionKey = Aes.Create();
sessionKey.KeySize = 256;

EncryptedXml eXml = new EncryptedXml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, st
// a new random symmetric key.
//////////////////////////////////////////////////

// Create a 256 bit Rijndael key.
RijndaelManaged sessionKey = new RijndaelManaged();
// Create a 256 bit Aes key.
Aes sessionKey = Aes.Create();
sessionKey.KeySize = 256;

EncryptedXml eXml = new EncryptedXml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, st
// a new random symmetric key.
//////////////////////////////////////////////////

// Create a 256 bit Rijndael key.
RijndaelManaged sessionKey = new RijndaelManaged();
// Create a 256 bit Aes key.
Aes sessionKey = Aes.Create();
sessionKey.KeySize = 256;

EncryptedXml eXml = new EncryptedXml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, st
// a new random symmetric key.
//////////////////////////////////////////////////

// Create a 256 bit Rijndael key.
RijndaelManaged sessionKey = new RijndaelManaged();
// Create a 256 bit Aes key.
Aes sessionKey = Aes.Create();
sessionKey.KeySize = 256;

EncryptedXml eXml = new EncryptedXml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, SymmetricAl
{
encryptionMethod = EncryptedXml.XmlEncDESUrl;
}
else if (Alg is Rijndael)
else if (Alg is Aes)
{
switch (Alg.KeySize)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, SymmetricAl
{
encryptionMethod = EncryptedXml.XmlEncDESUrl;
}
else if (Alg is Rijndael)
else if (Alg is Aes)
{
switch (Alg.KeySize)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ static void Main(string[] args)
}

// Encrypt the file using the public key from the certificate.
EncryptFile(originalFile, (RSACryptoServiceProvider)cert.PublicKey.Key);
EncryptFile(originalFile, (RSA)cert.PublicKey.Key);

// Decrypt the file using the private key from the certificate.
DecryptFile(encryptedFile, (RSACryptoServiceProvider)cert.PrivateKey);
DecryptFile(encryptedFile, cert.GetRSAPrivateKey());

//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", File.ReadAllText(originalFile));
Expand Down Expand Up @@ -80,9 +80,9 @@ private static X509Certificate2 GetCertificateFromStore(string certName)

// <Snippet3>
// Encrypt a file using a public key.
private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPublicKey)
private static void EncryptFile(string inFile, RSA rsaPublicKey)
{
using (Aes aes = new Aes())
using (Aes aes = Aes.Create())
{
// Create instance of Aes for
// symetric encryption of the data.
Expand Down Expand Up @@ -133,7 +133,6 @@ private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPubli
// a time, you can save memory
// and accommodate large files.
int count = 0;
int offset = 0;

// blockSizeBytes can be any arbitrary size.
int blockSizeBytes = aes.BlockSize / 8;
Expand All @@ -144,8 +143,7 @@ private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPubli
{
do
{
count = inFs.Read(data, offset, blockSizeBytes);
offset += count;
count = inFs.Read(data, 0, blockSizeBytes);
outStreamEncrypted.Write(data, 0, count);
bytesRead += count;
}
Expand All @@ -165,12 +163,12 @@ private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPubli

// <Snippet4>
// Decrypt a file using a private key.
private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPrivateKey)
private static void DecryptFile(string inFile, RSA rsaPrivateKey)
{

// Create instance of Aes for
// symetric decryption of the data.
using (Aes aes = new Aes())
using (Aes aes = Aes.Create())
{
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
Expand Down Expand Up @@ -221,9 +219,9 @@ private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPriva
inFs.Read(IV, 0, lenIV);
Directory.CreateDirectory(decrFolder);
//<Snippet10>
// Use RSACryptoServiceProvider
// Use RSA
// to decrypt the Aes key.
byte[] KeyDecrypted = rsaPrivateKey.Decrypt(KeyEncrypted, false);
byte[] KeyDecrypted = rsaPrivateKey.Decrypt(KeyEncrypted, RSAEncryptionPadding.Pkcs1);

// Decrypt the key.
using (ICryptoTransform transform = aes.CreateDecryptor(KeyDecrypted, IV))
Expand All @@ -238,7 +236,6 @@ private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPriva
{

int count = 0;
int offset = 0;

int blockSizeBytes = aes.BlockSize / 8;
byte[] data = new byte[blockSizeBytes];
Expand All @@ -254,8 +251,7 @@ private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPriva
{
do
{
count = inFs.Read(data, offset, blockSizeBytes);
offset += count;
count = inFs.Read(data, 0, blockSizeBytes);
outStreamDecrypted.Write(data, 0, count);
}
while (count > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class Sample
{
// <Snippet1>
private static void EncryptData(String inName, String outName, byte[] rijnKey, byte[] rijnIV)
private static void EncryptData(String inName, String outName, byte[] aesKey, byte[] aesIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
Expand All @@ -19,8 +19,8 @@ private static void EncryptData(String inName, String outName, byte[] rijnKey, b
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.

SymmetricAlgorithm rijn = SymmetricAlgorithm.Create(); //Creates the default implementation, which is RijndaelManaged.
CryptoStream encStream = new CryptoStream(fout, rijn.CreateEncryptor(rijnKey, rijnIV), CryptoStreamMode.Write);
Aes aes = Aes.Create();
CryptoStream encStream = new CryptoStream(fout, aes.CreateEncryptor(aesKey, aesIV), CryptoStreamMode.Write);

Console.WriteLine("Encrypting...");

Expand Down
Loading