· 7 years ago · Jul 18, 2018, 08:46 PM
1/// <summary>
2 /// загрузить наÑтройки Ñервера из файла
3 /// </summary>
4 public void Load()
5 {
6 if (!File.Exists(@"socket" + @"api.txt"))
7 {
8 return;
9 }
10
11 try
12 {
13 using (StreamReader reader = new StreamReader(@"socket" + @"api.txt"))
14 {
15 UserId = reader.ReadLine();
16 UserKey = reader.ReadLine();
17 _countDaysTickNeadToSave = Convert.ToInt32(reader.ReadLine());
18 _neadToSaveTicks = Convert.ToBoolean(reader.ReadLine());
19 IsDemo = Convert.ToBoolean(reader.ReadLine());
20
21 reader.Close();
22 }
23 }
24 catch (Exception)
25 {
26 // ignored
27 }
28 }
29
30 /// <summary>
31 /// Ñохранить наÑтройки Ñервера в файл
32 /// </summary>
33 public void Save()
34 {
35 try
36 {
37 using (StreamWriter writer = new StreamWriter(@"socket" + @"api.txt", false))
38 {
39 writer.WriteLine(UserId);
40 writer.WriteLine(UserKey);
41 writer.WriteLine(CountDaysTickNeadToSave);
42 writer.WriteLine(NeadToSaveTicks);
43 writer.WriteLine(IsDemo);
44
45 writer.Close();
46 }
47 }
48 catch (Exception)
49 {
50 // ignored
51 }
52 }
53
54static void Main(string[] args)
55 {
56
57 ushort secretKey = 0x0088; // Секретный ключ (длина - 16 bit).
58
59
60 string str = "Hello World"; //Ñто Ñтрока которую мы зашифруем
61
62 str = EncodeDecrypt(str, secretKey); //производим шифрование
63 Console.WriteLine(str); //выводим в конÑоль зашифрованную Ñтроку
64
65 str = EncodeDecrypt(str, secretKey); //производим раÑÑшифровку
66 Console.WriteLine(str); //выводим в конÑоль раÑшифрованную Ñтроку
67 Console.ReadKey();
68
69 }
70
71 public static string EncodeDecrypt(string str, ushort secretKey)
72 {
73 var ch = str.ToArray(); //преобразуем Ñтроку в Ñимволы
74 string newStr = ""; //Ð¿ÐµÑ€ÐµÐ¼ÐµÐ½Ð½Ð°Ñ ÐºÐ¾Ñ‚Ð¾Ñ€Ð°Ñ Ð±ÑƒÐ´ÐµÑ‚ Ñодержать зашифрованную Ñтроку
75 foreach (var c in ch) //выбираем каждый Ñлемент из маÑÑива Ñимволов нашей Ñтроки
76 newStr += TopSecret(c, secretKey); //производим шифрование каждого отдельного Ñлемента и ÑохранÑем его в Ñтроку
77 return newStr;
78 }
79
80 public static char TopSecret(char character, ushort secretKey)
81 {
82 character = (char)(character ^ secretKey); //Производим XOR операцию
83 return character;
84 }
85
86// Шифруем текÑÑ‚, и запиÑываем его в файл
87
88FileStream stream = new FileStream("C:\mytext.txt", FileMode.OpenOrCreate, FileAccess.Write);
89
90DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
91
92cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
93cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
94
95CryptoStream crStream = new CryptoStream(stream,
96 cryptic.CreateEncryptor(),CryptoStreamMode.Write);
97
98
99byte[] data = ASCIIEncoding.ASCII.GetBytes("Hello World!");
100
101crStream.Write(data,0,data.Length);
102
103crStream.Close();
104stream.Close();
105//Дешифруем текÑÑ‚ и выводим результат дешифрации в конÑоль
106
107FileStream stream = new FileStream("C:\mytext.txt",
108 FileMode.Open,FileAccess.Read);
109
110DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
111
112cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
113cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
114
115CryptoStream crStream = new CryptoStream(stream,
116 cryptic.CreateDecryptor(),CryptoStreamMode.Read);
117
118StreamReader reader = new StreamReader(crStream);
119
120string data = reader.ReadToEnd();
121Console.WriteLine(data);
122Console.ReadKey();
123
124reader.Close();
125stream.Close();