-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
163 lines (129 loc) · 5.07 KB
/
Program.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace CaesarEncryption
{
class Program
{
/// <summary>
///
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
int MenueAuswahl = SetStartMenue();
while (MenueAuswahl != 0)
{
switch (MenueAuswahl)
{
case 1:
Console.WriteLine("Bitte den Verschlüsselungstext eingeben: ");
string klartext = Console.ReadLine();
klartext = klartext.ToUpper();
char[] secretMessage = klartext.ToCharArray();
Console.WriteLine("Bitte Positions Key eingeben: ");
int Key = Convert.ToInt32(Console.ReadLine());
//Aufruf der statischen Encryption Methode
Encryption(secretMessage, Key);
string secret = Encryption(secretMessage, Key);
Console.WriteLine(secret);
//Aufruf der Menue Methode
MenueAuswahl = SetStartMenue();
break;
case 2:
Console.WriteLine("Bitte den Verschlüsselten Text eingeben: ");
string vklartext = Console.ReadLine();
vklartext = vklartext.ToUpper();
char[] vsecretMessage = vklartext.ToCharArray();
Console.WriteLine("Bitte Positions Key eingeben: ");
int vKey = Convert.ToInt32(Console.ReadLine());
//Aufruf der statischen Decryption Methode
Decryption(vsecretMessage, vKey);
string vksecret = Decryption(vsecretMessage, vKey);
Console.WriteLine(vksecret);
//Aufruf der Menue Methode
MenueAuswahl = SetStartMenue();
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine("Ungültige Eingabe");
Console.ReadKey();
//Aufruf der Menue Methode
MenueAuswahl = SetStartMenue();
break;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="secretMessage"></param>
/// <param name="key"></param>
/// <returns></returns>
static public string Encryption(char[] secretMessage, int key)
{
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
string verschluesselteNachricht = "";
for (int i = 0; i < secretMessage.Length; i++)
{
char c = secretMessage[i];
if (alphabet.Contains(c))
{
int position = Array.IndexOf(alphabet, c);
int position_new = position + key;
int rest = position_new % 26;
verschluesselteNachricht += alphabet[rest];
}
else
{
verschluesselteNachricht += c;
}
}
return verschluesselteNachricht;
}
/// <summary>
///
/// </summary>
/// <param name="vsecretMessage"></param>
/// <param name="vkey"></param>
/// <returns></returns>
static public string Decryption(char[] vsecretMessage, int vkey)
{
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
string verschluesselteNachricht = "";
for (int i = 0; i < vsecretMessage.Length; i++)
{
char c = vsecretMessage[i];
if (alphabet.Contains(c))
{
int position = Array.IndexOf(alphabet, c);
int position_new = position - vkey;
int rest = position_new % 26;
verschluesselteNachricht += alphabet[rest];
}
else
{
verschluesselteNachricht += c;
}
}
return verschluesselteNachricht;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
static public int SetStartMenue()
{
Console.WriteLine("Willkommen zur Cäsar Verschlüsselung");
Console.WriteLine("1. Für Text Verschlüsseln:");
Console.WriteLine("2. Für Text Entschlüsseln:");
Console.WriteLine("0. Für Programmende:");
return Convert.ToInt32(Console.ReadLine());
}
}
}