Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
address feedback
  • Loading branch information
tdykstra committed Jun 29, 2020
commit 813acfaed12f84bda7ee1b662923304ee73fc45d
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 AesManaged class.
AesManaged^ AM = gcnew AesManaged;
//Create a new instance of the Aes class.
Aes^ aes = gcnew Aes;

//Encrypt the symmetric key and IV.
EncryptedSymmetricKey = RSA->Encrypt( AM->Key, false );
EncryptedSymmetricIV = RSA->Encrypt( AM->IV, false );
Console::WriteLine( "AesManaged 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 @@ -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 @@ -6,7 +6,7 @@
public class Sample
{
// <Snippet1>
private static void EncryptData(String inName, String outName, byte[] aesKey, byte[] aesIV)
private static void EncryptData(String inName, String outName, byte[] tdesKey, byte[] tdesIV)
{
//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[] aesKey, by
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.

AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, aes.CreateEncryptor(aesKey, aesIV), CryptoStreamMode.Write);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ static void Main()
RSA.ImportParameters(RSAKeyInfo);

//Create a new instance of the AesManaged class.
AesManaged AM = new AesManaged();
Aes aes = new Aes();

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

Console.WriteLine("AesManaged 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 @@ -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 @@ -230,7 +228,7 @@ Class Program
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 AesManaged class.
Dim AM As New AesManaged()
'Create a new instance of the Aes class.
Dim aes As New Aes()

'Encrypt the symmetric key and IV.
EncryptedSymmetricKey = RSA.Encrypt(AM.Key, False)
EncryptedSymmetricIV = RSA.Encrypt(AM.IV, False)
EncryptedSymmetricKey = RSA.Encrypt(aes.Key, False)
EncryptedSymmetricIV = RSA.Encrypt(aes.IV, False)

Console.WriteLine("AesManaged 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