forked from sauternic/One-Time-Pad-V2.0.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_OnTimePadVers.cs
148 lines (113 loc) · 5.33 KB
/
part_OnTimePadVers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Security.Cryptography;
namespace OnTimePad_WPF
{
public partial class MainWindow : Window
{
private void On_Time_Pad_Vers()
{
FileStream ein = null;
FileStream aus = null;
FileStream ausSchluessel = null;
Random Rn = new Random();
string TempFile = "";
try
{
#region TextBox_PfadWaehlen.Text
FileInfo FI1 = this.fi1;
FileInfo FI2 = new FileInfo(str_TextBox_SpeicherOrt);
//Endung darf nicht: "K_V_Pad_" haben!
if (FI1.Name.IndexOf("K_V_Pad_") != -1)
{
AbbruchFlag = true;
MessageBox.Show("Falsche Datei! :(");
return;
}
TempFile = FI2.FullName + "\\_" + Convert.ToString(Rn.Next(100000000, 999999999)) + FI1.Extension;
string File_K_V_Pad = FI2.FullName + "\\" + "K_V_Pad_" + FI1.Name;
string File_Schl_Pad = FI2.FullName + "\\" + "Schl_Pad_" + FI1.Name; ;
#endregion
#region %%%%%%%%%%%%%%%%%%%%% Komprimieren %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
this.Dispatcher.Invoke(new MeinDele(Meldungen), DispatcherPriority.Normal, "Komprimieren :/");
ein = new FileStream(FI1.FullName, FileMode.Open);
aus = new FileStream(TempFile, FileMode.Create);
byte[] byArr = new byte[ein.Length];
ein.Read(byArr, 0, byArr.Length);
//Zwischenschritt sonst alles gleich(einmal durch den DeflateStream Konstruktor lassen! :/ )
DeflateStream ausDef = new DeflateStream(aus, System.IO.Compression.CompressionLevel.Optimal);
ausDef.Write(byArr, 0, byArr.Length);
ausDef.Close();
ein.Close();
aus.Close();
#endregion %%%%%%%%%%%%%%%%%%%%% Ende Komprimieren %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#region %%%%%%%%%%%%%%%%%%%%% Verschlüsseln mit Pad %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
this.Dispatcher.Invoke(new MeinDele(Meldungen), DispatcherPriority.Normal, "Schlüssel Erzeugen :/");
ein = null;
aus = null;
//************************Neuer Teil RNGCryptoServiceProvider*****************************************
//Grösse des Byte Array festlegen mit Erzeugung.
byte[] schlusselArray = new byte[new FileInfo(TempFile).Length];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
//Byte Array Abfüllen
rng.GetBytes(schlusselArray);
//Resourcen wieder Freigeben
rng.Dispose();
//***************************************************************************************************
ein = new FileStream(TempFile, FileMode.Open);
aus = new FileStream(File_K_V_Pad, FileMode.Create);
ausSchluessel = new FileStream(File_Schl_Pad, FileMode.Create);
//Schlüssel gleich Rausschreiben ins File
ausSchluessel.Write(schlusselArray, 0, schlusselArray.Length);
this.Dispatcher.Invoke(new MeinDele(Meldungen), DispatcherPriority.Normal, "Verschlüsseln :/");
//Länge für for
long lan = (long)ein.Length;
byte by;
for (long x = 0; x < lan; ++x)
{
//Einzelne Bytes
by = (byte)ein.ReadByte();// Jedes Byte der Komprimierten Datei Einlesen.
////////Hier wird das Byte verändert/////////
by = (byte)((int)by ^ (int)schlusselArray[x]);
/////////////////////////////////////////////
aus.WriteByte(by);
}
this.Dispatcher.Invoke(new MeinDele(Meldungen), DispatcherPriority.Normal, "Fertig! :)");
#endregion %%%%%%%%%%%%%%%%%%%%% Ende Verschlüsseln Password %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
}
catch (Exception e)
{
AbbruchFlag = true;
this.Dispatcher.Invoke(new MeinDele(Meldungen), DispatcherPriority.Normal, "");
MessageBox.Show(e.Message);
}
finally// Wird immer Ausgeführt mit oder ohne Exception
{
if (ein != null)
ein.Close();
if (aus != null)
aus.Close();
if (ausSchluessel != null)
ausSchluessel.Close();
// Ojb Erzeugen um zu löschen! :/
if (TempFile != "")
new FileInfo(TempFile).Delete();
}
}
}
}