Skip to content

Merge master into live #4459

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 10 commits into from
Jul 1, 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 @@ -46,7 +46,7 @@ int main()
//</SNIPPET4>

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

// Try to decrypt, thus showing it can be round-tripped.
TripleDES^ decAlg = TripleDES::Create();
Aes^ decAlg = Aes::Create();
decAlg->Key = k2->GetBytes( 16 );
decAlg->IV = encAlg->IV;
MemoryStream^ decryptionStreamBacking = gcnew MemoryStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ int main()
//Import key parameters into RSA.
RSA->ImportParameters( RSAKeyInfo );

//Create a new instance of the RijndaelManaged class.
RijndaelManaged^ RM = gcnew RijndaelManaged;
//Create a new instance of the Aes class.
Aes^ aes = gcnew Aes;

//Encrypt the symmetric key and IV.
EncryptedSymmetricKey = RSA->Encrypt( RM->Key, false );
EncryptedSymmetricIV = RSA->Encrypt( RM->IV, false );
Console::WriteLine( "RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider." );
EncryptedSymmetricKey = RSA->Encrypt( aes->Key, false );
EncryptedSymmetricIV = RSA->Encrypt( aes->IV, false );
Console::WriteLine( "Aes Key and IV have been encrypted with RSACryptoServiceProvider." );
}
catch ( CryptographicException^ e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main()
String^ XmlFileName = "xmlsig.xml";

// Generate a signing key.
HMACSHA1^ Key = gcnew HMACSHA1;
HMACSHA256^ Key = gcnew HMACSHA256;
Console::WriteLine( "Signing: {0}", resourceToSign );

// Sign the detached resourceand save the signature in an XML file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int main()
{

// Generate a signing key.
HMACSHA1^ Key = gcnew HMACSHA1;
HMACSHA256^ Key = gcnew HMACSHA256;

// Create an XML file to sign.
CreateSomeXml( "Example.xml" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void Main(string[] passwordargs)
Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1, salt1);
//</SNIPPET4>
// Encrypt the data.
TripleDES encAlg = TripleDES.Create();
Aes encAlg = Aes.Create();
encAlg.Key = k1.GetBytes(16);
MemoryStream encryptionStream = new MemoryStream();
CryptoStream encrypt = new CryptoStream(encryptionStream,
Expand All @@ -64,7 +64,7 @@ public static void Main(string[] passwordargs)
k1.Reset();

// Try to decrypt, thus showing it can be round-tripped.
TripleDES decAlg = TripleDES.Create();
Aes decAlg = Aes.Create();
decAlg.Key = k2.GetBytes(16);
decAlg.IV = encAlg.IV;
MemoryStream decryptionStreamBacking = new MemoryStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,16 @@ private static X509Certificate2 GetCertificateFromStore(string certName)
// Encrypt a file using a public key.
private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPublicKey)
{
using (AesManaged aesManaged = new AesManaged())
using (Aes aes = new Aes())
{
// Create instance of AesManaged for
// Create instance of Aes for
// symetric encryption of the data.
aesManaged.KeySize = 256;
aesManaged.BlockSize = 128;
aesManaged.Mode = CipherMode.CBC;
using (ICryptoTransform transform = aesManaged.CreateEncryptor())
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
using (ICryptoTransform transform = aes.CreateEncryptor())
{
RSAPKCS1KeyExchangeFormatter keyFormatter = new RSAPKCS1KeyExchangeFormatter(rsaPublicKey);
byte[] keyEncrypted = keyFormatter.CreateKeyExchange(aesManaged.Key, aesManaged.GetType());
byte[] keyEncrypted = keyFormatter.CreateKeyExchange(aes.Key, aes.GetType());

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

int lKey = keyEncrypted.Length;
LenK = BitConverter.GetBytes(lKey);
int lIV = aesManaged.IV.Length;
int lIV = aes.IV.Length;
LenIV = BitConverter.GetBytes(lIV);

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

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

// blockSizeBytes can be any arbitrary size.
int blockSizeBytes = aesManaged.BlockSize / 8;
int blockSizeBytes = aes.BlockSize / 8;
byte[] data = new byte[blockSizeBytes];
int bytesRead = 0;

Expand Down Expand Up @@ -169,13 +168,12 @@ private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPubli
private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPrivateKey)
{

// Create instance of AesManaged for
// Create instance of Aes for
// symetric decryption of the data.
using (AesManaged aesManaged = new AesManaged())
using (Aes aes = new Aes())
{
aesManaged.KeySize = 256;
aesManaged.BlockSize = 128;
aesManaged.Mode = CipherMode.CBC;
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;

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

// Determine the start postition of
// the ciphter text (startC)
// Determine the start position of
// the cipher text (startC)
// and its length(lenC).
int startC = lenK + lenIV + 8;
int lenC = (int)inFs.Length - startC;

// Create the byte arrays for
// the encrypted AesManaged key,
// the encrypted Aes key,
// the IV, and the cipher text.
byte[] KeyEncrypted = new byte[lenK];
byte[] IV = new byte[lenIV];
Expand All @@ -224,11 +222,11 @@ private static void DecryptFile(string inFile, RSACryptoServiceProvider rsaPriva
Directory.CreateDirectory(decrFolder);
//<Snippet10>
// Use RSACryptoServiceProvider
// to decrypt the AesManaged key.
// to decrypt the Aes key.
byte[] KeyDecrypted = rsaPrivateKey.Decrypt(KeyEncrypted, false);

// Decrypt the key.
using (ICryptoTransform transform = aesManaged.CreateDecryptor(KeyDecrypted, IV))
using (ICryptoTransform transform = aes.CreateDecryptor(KeyDecrypted, IV))
{
//</Snippet10>

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

int blockSizeBytes = aesManaged.BlockSize / 8;
int blockSizeBytes = aes.BlockSize / 8;
byte[] data = new byte[blockSizeBytes];

// By decrypting a chunk a time,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ static void Main()
//Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo);

//Create a new instance of the RijndaelManaged class.
RijndaelManaged RM = new RijndaelManaged();
//Create a new instance of the Aes class.
Aes aes = new Aes();

//Encrypt the symmetric key and IV.
EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);
EncryptedSymmetricKey = RSA.Encrypt(aes.Key, false);
EncryptedSymmetricIV = RSA.Encrypt(aes.IV, false);

Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider.");
Console.WriteLine("Aes Key and IV have been encrypted with RSACryptoServiceProvider.");
}
//Catch and display a CryptographicException
//to the console.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void Main(string[] args)
string XmlFileName = "xmlsig.xml";

// Generate a signing key.
HMACSHA1 Key = new HMACSHA1();
HMACSHA256 Key = new HMACSHA256();

Console.WriteLine("Signing: {0}", resourceToSign);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void Main(String[] args)
{

// Generate a signing key.
HMACSHA1 Key = new HMACSHA1();
HMACSHA256 Key = new HMACSHA256();

// Create an XML file to sign.
CreateSomeXml("Example.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ private string EncodePassword(string password)
Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
break;
case MembershipPasswordFormat.Hashed:
HMACSHA1 hash = new HMACSHA1();
HMACSHA256 hash = new HMACSHA256();
hash.Key = HexToByte(machineKey.ValidationKey);
encodedPassword =
Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ private string EncodePassword(string password)
Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
break;
case MembershipPasswordFormat.Hashed:
HMACSHA1 hash = new HMACSHA1();
HMACSHA256 hash = new HMACSHA256();
hash.Key = HexToByte(machineKey.ValidationKey);
encodedPassword =
Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ private string EncodePassword(string password)
Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
break;
case MembershipPasswordFormat.Hashed:
HMACSHA1 hash = new HMACSHA1();
HMACSHA256 hash = new HMACSHA256();
hash.Key = HexToByte(machineKey.ValidationKey);
encodedPassword =
Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ private string EncodePassword(string password)
Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
break;
case MembershipPasswordFormat.Hashed:
HMACSHA1 hash = new HMACSHA1();
HMACSHA256 hash = new HMACSHA256();
hash.Key = HexToByte(machineKey.ValidationKey);
encodedPassword =
Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ private string EncodePassword(string password)
Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
break;
case MembershipPasswordFormat.Hashed:
HMACSHA1 hash = new HMACSHA1();
HMACSHA256 hash = new HMACSHA256();
hash.Key = HexToByte(machineKey.ValidationKey);
encodedPassword =
Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Public Class rfc2898test
Dim k2 As New Rfc2898DeriveBytes(pwd1, salt1)
'</SNIPPET4>
' Encrypt the data.
Dim encAlg As TripleDES = TripleDES.Create()
Dim encAlg As Aes = Aes.Create()
encAlg.Key = k1.GetBytes(16)
Dim encryptionStream As New MemoryStream()
Dim encrypt As New CryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write)
Expand All @@ -51,7 +51,7 @@ Public Class rfc2898test
k1.Reset()

' Try to decrypt, thus showing it can be round-tripped.
Dim decAlg As TripleDES = TripleDES.Create()
Dim decAlg As Aes = Aes.Create()
decAlg.Key = k2.GetBytes(16)
decAlg.IV = encAlg.IV
Dim decryptionStreamBacking As New MemoryStream()
Expand Down
Loading