// VerschluesselteDateien.cpp: Hauptprojektdatei.

#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace System::Security::Cryptography;

int main(array<System::String ^> ^args)
{
   {
      // verschlüsselt in Datei schreiben
      FileStream^ fs = gcnew FileStream("ImNebel.enc", FileMode::Create);
      array<Byte>^ schluessel = Convert::FromBase64String("ABkwoRR32mKKNNt9430rk4R88JDlM9wQ");
      array<Byte>^ iv = Convert::FromBase64String("Zr348FUS83lF");
      TripleDES^ tdes = TripleDES::Create();

      CryptoStream^ cs = gcnew CryptoStream(fs, 
                                            tdes->CreateEncryptor(schluessel, iv), 
                                            CryptoStreamMode::Write);
      StreamWriter^ sw = gcnew StreamWriter(cs);

      sw->WriteLine(" Seltsam, im Nebel zu wandern!");
      sw->WriteLine(" Einsam ist jeder Busch und Stein,");
      sw->WriteLine(" Kein Baum sieht den andern,");
      sw->WriteLine(" Jeder ist allein.");
      sw->WriteLine(" ...");
      sw->WriteLine("                        Hermann Hesse");

      sw->Close();
   }

   {
      // aus komprimierter Datei lesen
      FileStream^ fs = gcnew FileStream("ImNebel.enc", FileMode::Open);
      array<Byte>^ schluessel = Convert::FromBase64String("ABkwoRR32mKKNNt9430rk4R88JDlM9wQ");
      array<Byte>^ iv = Convert::FromBase64String("Zr348FUS83lF");
      TripleDES^ tdes = TripleDES::Create();

      CryptoStream^ cs = gcnew CryptoStream(fs, 
                                            tdes->CreateDecryptor(schluessel, iv), 
                                            CryptoStreamMode::Read);

      StreamReader^ sr = gcnew StreamReader(cs);

      String^ zeile;

      while( (zeile = sr->ReadLine()) != nullptr)
         Console::WriteLine(zeile);

      sr->Close();
   }

   Console::ReadLine();
   return 0;
}