-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEncryptKey.cs
More file actions
64 lines (60 loc) · 2.08 KB
/
EncryptKey.cs
File metadata and controls
64 lines (60 loc) · 2.08 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
using System;
using System.Text;
namespace ClipPurSEditionBuilder
{
public class EncryptKey
{
/// <summary>
/// Метод для шифрования строки
/// </summary>
/// <param name="input">Входные данные</param>
/// <param name="key">Ключ</param>
/// <returns>Зашифрованная строка</returns>
public static string Encrypt(string input, string key)
{
string result = string.Empty;
byte[] bytes = null;
if (string.IsNullOrWhiteSpace(input))
{
return result;
}
try
{
bytes = Encoding.UTF8.GetBytes(input);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)(bytes[i] ^ key[i % key.Length]);
}
result = $"#{Convert.ToBase64String(bytes)}";
}
catch { } return result;
}
/// <summary>
/// Метод для расшифрования строки
/// </summary>
/// <param name="input">Входные данные</param>
/// <param name="key">Ключ</param>
/// <returns>Расшифрованная строка</returns>
public static string Decrypt(string input, string key)
{
string Result = string.Empty;
if (!input.StartsWith("#") && string.IsNullOrWhiteSpace(input))
{
return Result;
}
try
{
input = input.Remove(0, 1);
//input = input.Replace("#", "");
byte[] bytes = Convert.FromBase64String(input);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)(bytes[i] ^ key[i % key.Length]);
}
Result = Encoding.UTF8.GetString(bytes);
}
catch { }
return Result;
}
}
}