public class FlowDocEncryptor : IDisposable
{
private bool _disposed;
private readonly SHA256 _sha256;
private readonly Rijndael _rijndael;
public FlowDocEncryptor(string key)
{
_sha256 = SHA256.Create();
_rijndael = Rijndael.Create();
_rijndael.BlockSize = 256;
_rijndael.IV = ***;
_rijndael.Key = ***;
}
public void EncryptToFile(string path, FlowDocument flowDoc, List<string> files, List<string> filenames)
{
if (File.Exists(path))
{
try { File.Delete(path); }
catch (Exception) { /* error handling */ }
}
FileStream fstream = null;
CryptoStream cstream = null;
try
{
fstream = File.Open(path, FileMode.CreateNew, FileAccess.Write);
cstream = new CryptoStream(fstream, _rijndael.CreateEncryptor(_rijndael.Key, _rijndael.IV), CryptoStreamMode.Write);
string gesamt = "";
string xaml = XamlWriter.Save(flowDoc);
string trenn = "+*+++~##EEAD by Chriss000xx.sssd";
gesamt = xaml + trenn;
for (int i = 0; i < files.Count; i++)
{
gesamt = gesamt + "-#+++:"+Encoding.Unicode.GetString(File.ReadAllBytes(files[i]))+"filename::++"+filenames[i];
}
byte[] data = Encoding.Unicode.GetBytes(gesamt);
cstream.Write(data, 0, data.Length);
cstream.Flush();
fstream.Flush();
}
catch (Exception)
{
}
finally
{
try
{
if (cstream != null)
{
cstream.Close();
cstream.Dispose();
}
if (fstream != null)
{
fstream.Close();
fstream.Dispose();
}
}
catch { }
}
}
bool timer = false;
int wait = 5;
Button button;
private void timer1_Tick(object sender, EventArgs e)
{
if (timer == true)
{
button.IsEnabled = false;
button.Width = 859;
button.Content = "Passwort falsch! Noch " + wait + " Sekunden warten";
if (wait == 0)
{
timer = false;
wait = 5;
button.IsEnabled = true;
button.Content = "Container wählen";
button.Width = 100;
}
wait--;
}
}
List<string> filenames = new List<string>();
List<byte[]> filedata = new List<byte[]>();
public List<string> names()
{
return filenames;
}
public List<byte[]> data()
{
return filedata;
}
public FlowDocument DecryptFromFile(string path, Button error)
{
System.Windows.Forms.Timer time = new System.Windows.Forms.Timer();
time.Interval = 1000;
time.Tick += new System.EventHandler(this.timer1_Tick);
button = error;
time.Start();
if (!File.Exists(path))
throw new FileNotFoundException();
CryptoStream cstream = null;
MemoryStream mstream = null;
StreamReader reader = null;
try
{
byte[] data = File.ReadAllBytes(path);
mstream = new MemoryStream(data, true);
cstream = new CryptoStream(mstream, _rijndael.CreateDecryptor(_rijndael.Key, _rijndael.IV), CryptoStreamMode.Read);
reader = new StreamReader(cstream, Encoding.Unicode);
string gesamt = reader.ReadToEnd();
string[] trenn = new string[1]{"+*+++~##EEAD by Chriss000xx.sssd"};
string[] trenn2 = new string[1] { "-#+++:" };
string[] trenn3 = new string[1] { "filename::++" };
string xaml = gesamt.Split(trenn,StringSplitOptions.None)[0];
try
{
string rest = gesamt.Split(trenn, StringSplitOptions.None)[1];
try
{
for (int i = 0; i < rest.Split(trenn2, StringSplitOptions.RemoveEmptyEntries).Length; i++)
{
filedata.Add(Encoding.Unicode.GetBytes(rest.Split(trenn2, StringSplitOptions.None)[i].Split(trenn3, StringSplitOptions.None)[0]));
filenames.Add(rest.Split(trenn2, StringSplitOptions.None)[i].Split(trenn3, StringSplitOptions.None)[1]);
}
}
catch { }
}
catch (Exception e)
{
filenames.Add(e.Message);
filedata.Add(new byte[1]{0});
string rest = gesamt.Split(trenn, StringSplitOptions.None)[1];
filenames.Add(rest.Split(trenn2, StringSplitOptions.RemoveEmptyEntries).Length.ToString());
filedata.Add(new byte[1] { 0 });
}
return XamlReader.Parse(xaml) as FlowDocument;
}
catch (Exception)
{
return new FlowDocument();
}
finally
{
try
{
if (cstream != null)
{
cstream.Close();
cstream.Dispose();
}
if (mstream != null)
{
mstream.Close();
mstream.Dispose();
}
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
catch
{
timer = true;
button.IsEnabled = false;
button.Width = 859;
button.Content = "Passwort falsch! Noch " + wait + " Sekunden warten";
}
}
}
public static byte[] keyhash(string key)
{
MD5 md5 = MD5.Create();
SHA256 sha = SHA256.Create();
byte[] data = ***;
return data;
}
public static byte[] ivhash(string key)
{
MD5 md5 = MD5.Create();
SHA256 sha = SHA256.Create();
byte[] data = ***;
return data;
}
public void Dispose()
{
if (_disposed) return;
_rijndael.Clear();
_rijndael.Dispose();
_sha256.Clear();
_sha256.Dispose();
_disposed = true;
}
}