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
@@ -1,119 +1,146 @@

// <SNIPPET1>
using namespace System;
using namespace System::IO;
using namespace System::Security::Cryptography;
using namespace System::Text;
using namespace System::IO;
void EncryptTextToFile( String^ Data, String^ FileName, array<Byte>^Key, array<Byte>^IV )

void EncryptTextToFile(String^ text, String^ path, array<Byte>^ key, array<Byte>^ iv);
String^ DecryptTextFromFile(String^ path, array<Byte>^ key, array<Byte>^ iv);

int main()
{
try
{

// Create or open the specified file.
FileStream^ fStream = File::Open( FileName, FileMode::OpenOrCreate );

// Create a new TripleDES object.
TripleDES^ tripleDESalg = TripleDES::Create();

// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream^ cStream = gcnew CryptoStream( fStream,tripleDESalg->CreateEncryptor( Key, IV ),CryptoStreamMode::Write );

// Create a StreamWriter using the CryptoStream.
StreamWriter^ sWriter = gcnew StreamWriter( cStream );

// Write the data to the stream
// to encrypt it.
sWriter->WriteLine( Data );

// Close the streams and
// close the file.
sWriter->Close();
cStream->Close();
fStream->Close();
}
catch ( CryptographicException^ e )
{
Console::WriteLine( "A Cryptographic error occurred: {0}", e->Message );
}
catch ( UnauthorizedAccessException^ e )
{
Console::WriteLine( "A file access error occurred: {0}", e->Message );
}
try
{
array<Byte>^ key;
array<Byte>^ iv;

// Create a new TripleDES object to generate a random key
// and initialization vector (IV).
{
TripleDES^ tripleDes;

try
{
tripleDes = TripleDES::Create();
key = tripleDes->Key;
iv = tripleDes->IV;
}
finally
{
delete tripleDes;
}
}

// Create a string to encrypt.
String^ original = "Here is some data to encrypt.";
// The name/path of the file to write.
String^ filename = "CText.enc";

// Encrypt the string to a file.
EncryptTextToFile(original, filename, key, iv);

// Decrypt the file back to a string.
String^ decrypted = DecryptTextFromFile(filename, key, iv);

// Display the decrypted string to the console.
Console::WriteLine(decrypted);
}
catch (Exception^ e)
{
Console::WriteLine(e->Message);
}
}

String^ DecryptTextFromFile( String^ FileName, array<Byte>^Key, array<Byte>^IV )
void EncryptTextToFile(String^ text, String^ path, array<Byte>^ key, array<Byte>^ iv)
{
try
{

// Create or open the specified file.
FileStream^ fStream = File::Open( FileName, FileMode::OpenOrCreate );

// Create a new TripleDES object.
TripleDES^ tripleDESalg = TripleDES::Create();

// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream^ cStream = gcnew CryptoStream( fStream,tripleDESalg->CreateDecryptor( Key, IV ),CryptoStreamMode::Read );

// Create a StreamReader using the CryptoStream.
StreamReader^ sReader = gcnew StreamReader( cStream );

// Read the data from the stream
// to decrypt it.
String^ val = sReader->ReadLine();

// Close the streams and
// close the file.
sReader->Close();
cStream->Close();
fStream->Close();

// Return the string.
return val;
}
catch ( CryptographicException^ e )
{
Console::WriteLine( "A Cryptographic error occurred: {0}", e->Message );
return nullptr;
}
catch ( UnauthorizedAccessException^ e )
{
Console::WriteLine( "A file access error occurred: {0}", e->Message );
return nullptr;
}
FileStream^ fStream = nullptr;
TripleDES^ tripleDes = nullptr;
ICryptoTransform^ encryptor = nullptr;
CryptoStream^ cStream = nullptr;

try
{
// Create or open the specified file.
fStream = File::Open(path, FileMode::Create);
// Create a new TripleDES object.
tripleDes = TripleDES::Create();
// Create a TripleDES encryptor from the key and IV
encryptor = tripleDes->CreateEncryptor(key, iv);
// Create a CryptoStream using the FileStream and encryptor
cStream = gcnew CryptoStream(fStream, encryptor, CryptoStreamMode::Write);

// Convert the provided string to a byte array.
array<Byte>^ toEncrypt = Encoding::UTF8->GetBytes(text);

// Write the byte array to the crypto stream.
cStream->Write(toEncrypt, 0, toEncrypt->Length);
}
catch (CryptographicException^ e)
{
Console::WriteLine("A Cryptographic error occurred: {0}", e->Message);
throw;
}
finally
{
if (cStream != nullptr)
delete cStream;

if (encryptor != nullptr)
delete encryptor;

if (tripleDes != nullptr)
delete tripleDes;

if (fStream != nullptr)
delete fStream;
}
}

int main()
String^ DecryptTextFromFile(String^ path, array<Byte>^ key, array<Byte>^ iv)
{
try
{

// Create a new TripleDES object to generate a key
// and an initialization vector (IV).
TripleDES^ TripleDESalg = TripleDES::Create();

// Create a string to encrypt.
String^ sData = "Here is some data to encrypt.";
String^ FileName = "CText.txt";

// Encrypt text to a file using the file name, key, and IV.
EncryptTextToFile( sData, FileName, TripleDESalg->Key, TripleDESalg->IV );

// Decrypt the text from a file using the file name, key, and IV.
String^ Final = DecryptTextFromFile( FileName, TripleDESalg->Key, TripleDESalg->IV );

// Display the decrypted string to the console.
Console::WriteLine( Final );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
FileStream^ fStream = nullptr;
TripleDES^ tripleDes = nullptr;
ICryptoTransform^ decryptor = nullptr;
CryptoStream^ cStream = nullptr;
StreamReader^ reader = nullptr;

try
{
// Open the specified file
fStream = File::OpenRead(path);
// Create a new TripleDES object.
tripleDes = TripleDES::Create();
// Create a TripleDES decryptor from the key and IV
decryptor = tripleDes->CreateDecryptor(key, iv);
// Create a CryptoStream using the FileStream and decryptor
cStream = gcnew CryptoStream(fStream, decryptor, CryptoStreamMode::Read);
// Create a StreamReader to turn the bytes back into text
reader = gcnew StreamReader(cStream, Encoding::UTF8);

// Read back all of the text from the StreamReader, which receives
// the decrypted bytes from the CryptoStream, which receives the
// encrypted bytes from the FileStream.
return reader->ReadToEnd();
}
catch (CryptographicException^ e)
{
Console::WriteLine("A Cryptographic error occurred: {0}", e->Message);
throw;
}
finally
{
if (cStream != nullptr)
delete cStream;

if (decryptor != nullptr)
delete decryptor;

if (tripleDes != nullptr)
delete tripleDes;

if (fStream != nullptr)
delete fStream;
}
}

// </SNIPPET1>
Loading