From c89a6b2a7176e205e9d58ee2d50a66a8b0113eff Mon Sep 17 00:00:00 2001
From: Alexander <GoodBoyAlex@users.noreply.github.com>
Date: Fri, 20 Jan 2023 16:22:37 +0300
Subject: [PATCH] 20230120

---
 anbs_cp/Classes/Encrypt/EncriptKey.cs      | 48 ++++++++++++++++++++++
 anbs_cp/Classes/Encrypt/StringEncrypt.cs   | 48 ++++++++++++++++++++++
 anbs_cp/Interfaces/IEncryptKey.cs          | 12 ++++++
 anbs_cp/anbs_cp.csproj                     |  6 +--
 anbsoftware.componentspack.sln.DotSettings |  1 +
 5 files changed, 112 insertions(+), 3 deletions(-)
 create mode 100644 anbs_cp/Classes/Encrypt/EncriptKey.cs
 create mode 100644 anbs_cp/Classes/Encrypt/StringEncrypt.cs
 create mode 100644 anbs_cp/Interfaces/IEncryptKey.cs

diff --git a/anbs_cp/Classes/Encrypt/EncriptKey.cs b/anbs_cp/Classes/Encrypt/EncriptKey.cs
new file mode 100644
index 0000000..92b0cda
--- /dev/null
+++ b/anbs_cp/Classes/Encrypt/EncriptKey.cs
@@ -0,0 +1,48 @@
+using System.Text;
+
+using anbs_cp.Interfaces;
+
+namespace anbs_cp.Classes.Encrypt;
+
+/// <summary>
+/// Ключ шифровки / расшифровки
+/// </summary>
+public class EncryptKey: IEncryptKey
+{
+    /// <summary>
+    /// Конструктор по умолчанию
+    /// </summary>
+    /// <param name="key">Ключ в формате массива <see cref="byte"/></param>
+    public EncryptKey (byte[] key) => Key = key;
+
+    /// <summary>
+    /// Конструктор, принимающий строку и преобразующий в нужный формат
+    /// </summary>
+    /// <param name="key">Ключ в формате <see cref="string"/></param>
+    public EncryptKey (string key) => Key = Encoding.ASCII.GetBytes(key);
+
+    /// <summary>
+    /// Ключ
+    /// </summary>
+    public byte[] Key { get; set; }
+
+    /// <summary>
+    /// Получение ключа по умолчанию
+    /// </summary>
+    /// <returns><see cref="EncryptKey"/></returns>
+    public static EncryptKey GetDefault () => new(Enumerable.Range(0, 32).Select(static x => (byte)x).ToArray());
+
+    /// <summary>
+    /// Получение класса ключа
+    /// </summary>
+    /// <param name="key">Ключ в формате массива <see cref="byte"/></param>
+    /// <returns><see cref="EncryptKey"/></returns>
+    public static EncryptKey GetKey (byte[]? key) => key != null ? new(key) : GetDefault();
+
+    /// <summary>
+    /// Получение класса ключа
+    /// </summary>
+    /// <param name="key">Ключ в формате массива <see cref="byte"/></param>
+    /// <returns><see cref="EncryptKey"/></returns>
+    public static EncryptKey GetKey (string key) => !string.IsNullOrWhiteSpace(key) ? new(key) : GetDefault();
+}
\ No newline at end of file
diff --git a/anbs_cp/Classes/Encrypt/StringEncrypt.cs b/anbs_cp/Classes/Encrypt/StringEncrypt.cs
new file mode 100644
index 0000000..f0e7444
--- /dev/null
+++ b/anbs_cp/Classes/Encrypt/StringEncrypt.cs
@@ -0,0 +1,48 @@
+using System.Security.Cryptography;
+using System.Text;
+
+namespace anbs_cp.Classes.Encrypt;
+
+/// <summary>
+/// Класс для шифровки строк
+/// </summary>
+public static class StringEncrypt
+{
+    /// <summary>
+    /// Метод для шифрования строки <paramref name="text"/> с помощью ключа <paramref name="key"/>
+    /// </summary>
+    /// <param name="text">Строка, которая должна быть зашифрована</param>
+    /// <param name="key">Ключ шифрования (в случае <value>null</value>)</param>
+    /// <returns>Этот статический метод возвращает зашифрованную строку <paramref name="text"/> с помощью ключа шифрования <paramref name="key"/></returns>
+    public static string Encrypt (string text, EncryptKey? key = null)
+    {
+        using Aes aes = Aes.Create();
+        aes.Key = key?.Key ?? EncryptKey.GetDefault().Key;
+        using MemoryStream ms = new();
+        ms.Write(aes.IV);
+        using (CryptoStream cs = new(ms, aes.CreateEncryptor(), CryptoStreamMode.Write, true))
+            cs.Write(Encoding.UTF8.GetBytes(text));
+
+        return Convert.ToBase64String(ms.ToArray());
+    }
+
+    /// <summary>
+    /// Метод для дешифрования строки <paramref name="text"/> с помощью ключа <paramref name="key"/>
+    /// </summary>
+    /// <param name="text">Строка, которая должна быть дешифрована</param>
+    /// <param name="key">Ключ шифрования (в случае <value>null</value>)</param>
+    /// <returns>Этот статический метод возвращает дешифрованную строку <paramref name="text"/> с помощью ключа шифрования <paramref name="key"/></returns>
+    public static string Decrypt (string text, EncryptKey? key = null)
+    {
+        using MemoryStream ms = new(Convert.FromBase64String(text));
+        byte[] iv = new byte[16];
+        int _ = ms.Read(iv);
+        using Aes aes = Aes.Create();
+        aes.Key = key?.Key ?? EncryptKey.GetDefault().Key;
+        aes.IV = iv;
+        using CryptoStream cs = new(ms, aes.CreateDecryptor(), CryptoStreamMode.Read, true);
+        using MemoryStream output = new();
+        cs.CopyTo(output);
+        return Encoding.UTF8.GetString(output.ToArray());
+    }
+}
\ No newline at end of file
diff --git a/anbs_cp/Interfaces/IEncryptKey.cs b/anbs_cp/Interfaces/IEncryptKey.cs
new file mode 100644
index 0000000..fedd43a
--- /dev/null
+++ b/anbs_cp/Interfaces/IEncryptKey.cs
@@ -0,0 +1,12 @@
+namespace anbs_cp.Interfaces;
+
+/// <summary>
+/// Интерфейс ключа
+/// </summary>
+public interface IEncryptKey
+{
+    /// <summary>
+    /// Ключ
+    /// </summary>
+    public byte[] Key { get; set; }
+}
\ No newline at end of file
diff --git a/anbs_cp/anbs_cp.csproj b/anbs_cp/anbs_cp.csproj
index dc0c637..59766de 100644
--- a/anbs_cp/anbs_cp.csproj
+++ b/anbs_cp/anbs_cp.csproj
@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <TargetFramework>net7.0</TargetFramework>
-    <Version>2023.115.0</Version>
+    <Version>2023.120.0</Version>
     <Authors>Alexander Babaev</Authors>
     <Product>ANB Software Components Pack</Product>
     <Description>Library of some useful functions in C# language.</Description>
@@ -15,8 +15,8 @@
     <SignAssembly>True</SignAssembly>
     <PackageProjectUrl>https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack</PackageProjectUrl>
     <RepositoryUrl>https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack</RepositoryUrl>
-    <AssemblyVersion>2023.115.0</AssemblyVersion>
-    <FileVersion>2023.115.0</FileVersion>
+    <AssemblyVersion></AssemblyVersion>
+    <FileVersion></FileVersion>
     <PackageId>ANBSoftware.ComponentsPack</PackageId>
     <PackageLicenseExpression>MIT</PackageLicenseExpression>
     <AnalysisLevel>6.0</AnalysisLevel>
diff --git a/anbsoftware.componentspack.sln.DotSettings b/anbsoftware.componentspack.sln.DotSettings
index f451ee2..ebbe1a7 100644
--- a/anbsoftware.componentspack.sln.DotSettings
+++ b/anbsoftware.componentspack.sln.DotSettings
@@ -11,6 +11,7 @@
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=_0421_043E_0437_0434_0430_0451_043C/@EntryIndexedValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=_0432_0432_0435_0434_0451_043D/@EntryIndexedValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=_0432_0438_0434_0435_043E_043A_0430_0440_0442_0435/@EntryIndexedValue">True</s:Boolean>
+	<s:Boolean x:Key="/Default/UserDictionary/Words/=_0434_0435_0448_0438_0444_0440_043E_0432_0430_043D_0438_044F/@EntryIndexedValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=_0438_043C_0451_043D/@EntryIndexedValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=_0441_0447_0451_0442/@EntryIndexedValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=_0441_0447_0451_0442_0447_0438_043A/@EntryIndexedValue">True</s:Boolean>