-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.cs
More file actions
128 lines (114 loc) · 4.79 KB
/
Encryption.cs
File metadata and controls
128 lines (114 loc) · 4.79 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Шифровщик
{
class Encryption
{
static Form1 form = new Form1();
static public string EnteredText;
public int hut;
public int FirstKey;
public int SecondKey;
public string EndText = null;
public void Rut()
{
EnteredText = EnteredText.ToUpper();
string alph = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";//алфавит
char[] Alphabet = alph.ToCharArray();//Заполенение массива алфвитом
char[] CryptedText = new char[100];
Regex regex = new Regex(@"\w*\S?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(EnteredText);
string[] Text = new string[EnteredText.Length];
int CryptionLetterInt;
char CryptionLetterChar;
int Index = 0;
foreach (Match match in matches)//Все слова
{
string g = match.Value;
char[] c = g.ToCharArray();
string Letter = null;
foreach (char u in c)
{
switch (u)
{
case ' ':
Letter += "";
break;
case '.':
Letter += ".";
break;
case ',':
Letter += ",";
break;
case '-':
Letter += "-";
break;
case '!':
Letter += "!";
break;
case '?':
Letter += "?";
break;
default:
for (int i = 0; i < 33; i++)//Ищем соответствие букв
{
if (Alphabet[i] == u)
{
if (hut == 1)
{
DecryptionLetter(i, FirstKey, SecondKey, out CryptionLetterInt);//Нашли числовое значение буквы
}
else
{
EncryptionLetter(i, FirstKey, SecondKey, out CryptionLetterInt);//Нашли числовое значение буквы
}
CryptionLetterChar = Alphabet[CryptionLetterInt];//Нашли буквенное значение цифры
CryptedText[Index] = CryptionLetterChar;//Записали букву в массив
Letter += Convert.ToString(CryptedText[Index]);
Index++; //Index++;
}
}
break;
}
}
if (Letter != null)
{
Letter += " ";
}
EndText += Letter;
Index = 0;
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Текстовый документ (*.txt)|*.txt|Все файлы (*.*)|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
StreamWriter streamWriter = new StreamWriter(saveFileDialog.FileName);
streamWriter.WriteLine(EndText);
streamWriter.Close();
}
void DecryptionLetter(int i, int FirstKey, int SecondKey, out int DecryptionLetterInt)
{
DecryptionLetterInt = (i - SecondKey);
while (DecryptionLetterInt % FirstKey != 0)
{
DecryptionLetterInt += 33;
}
DecryptionLetterInt /= FirstKey;
}
void EncryptionLetter(int i, int FirstKey, int SecondKey, out int EncryptionLetterInt)
{
EncryptionLetterInt = FirstKey * i + SecondKey;
while (EncryptionLetterInt >= 33)
{
EncryptionLetterInt -= 33;
}
}
}
}
}