Skip to content
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 @@ -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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,16 @@ Class Program
' <Snippet3>
' Encrypt a file using a public key.
Private Shared Sub EncryptFile(ByVal inFile As String, ByVal rsaPublicKey As RSACryptoServiceProvider)
Dim aesManaged As New AesManaged()
Dim aes As New Aes()
Try
' 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
Dim transform As ICryptoTransform = aesManaged.CreateEncryptor()
aes.KeySize = 256
aes.Mode = CipherMode.CBC
Dim transform As ICryptoTransform = aes.CreateEncryptor()
Try
Dim keyFormatter As New RSAPKCS1KeyExchangeFormatter(rsaPublicKey)
Dim keyEncrypted As Byte() = keyFormatter.CreateKeyExchange(aesManaged.Key, aesManaged.GetType())
Dim keyEncrypted As Byte() = keyFormatter.CreateKeyExchange(aes.Key, aes.GetType())

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

Dim lKey As Integer = keyEncrypted.Length
LenK = BitConverter.GetBytes(lKey)
Dim lIV As Integer = aesManaged.IV.Length
Dim lIV As Integer = aesM.IV.Length
LenIV = BitConverter.GetBytes(lIV)

' Write the following to the FileStream
Expand All @@ -117,7 +116,7 @@ Class Program
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 @@ -131,7 +130,7 @@ Class Program
Dim offset As Integer = 0

' blockSizeBytes can be any arbitrary size.
Dim blockSizeBytes As Integer = aesManaged.BlockSize / 8
Dim blockSizeBytes As Integer = aes.BlockSize / 8
Dim data(blockSizeBytes) As Byte
Dim bytesRead As Integer = 0

Expand Down Expand Up @@ -160,7 +159,7 @@ Class Program
transform.Dispose()
End Try
Finally
aesManaged.Dispose()
aes.Dispose()
End Try

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

' Create instance of AesManaged for
' Create instance of Aes for
' symetric decryption of the data.
Dim aesManaged As New AesManaged()
Dim aes As New Aes()
Try
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 Down Expand Up @@ -211,7 +209,7 @@ Class Program
Dim lenC As Integer = (CType(inFs.Length, Integer) - startC)

' Create the byte arrays for
' the encrypted Rijndael key,
' the encrypted AES key,
' the IV, and the cipher text.
Dim KeyEncrypted() As Byte = New Byte(lengthK - 1) {}
Dim IV() As Byte = New Byte(lengthIV - 1) {}
Expand All @@ -226,11 +224,11 @@ Class Program
Directory.CreateDirectory(decrFolder)
'<Snippet10>
' Use RSACryptoServiceProvider
' to decrypt the Rijndael key.
' to decrypt the AES key.
Dim KeyDecrypted As Byte() = rsaPrivateKey.Decrypt(KeyEncrypted, False)

' Decrypt the key.
Dim transform As ICryptoTransform = aesManaged.CreateDecryptor(KeyDecrypted, IV)
Dim transform As ICryptoTransform = aes.CreateDecryptor(KeyDecrypted, IV)
'</Snippet10>
' Decrypt the cipher text from
' from the FileSteam of the encrypted
Expand All @@ -246,7 +244,7 @@ Class Program
Dim count As Integer = 0
Dim offset As Integer = 0

Dim blockSizeBytes As Integer = aesManaged.BlockSize / 8
Dim blockSizeBytes As Integer = aes.BlockSize / 8
Dim data(blockSizeBytes) As Byte

' By decrypting a chunk a time,
Expand Down Expand Up @@ -280,7 +278,7 @@ Class Program
End Try

Finally
aesManaged.Dispose()
aes.Dispose()
End Try


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Class RSACSPSample
'Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo)

'Create a new instance of the RijndaelManaged class.
Dim RM As New RijndaelManaged()
'Create a new instance of the Aes class.
Dim aes As 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