This commit is contained in:
Alexander 2023-01-20 16:22:37 +03:00
parent d3171a1164
commit c89a6b2a71
5 changed files with 112 additions and 3 deletions

View File

@ -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();
}

View File

@ -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());
}
}

View File

@ -0,0 +1,12 @@
namespace anbs_cp.Interfaces;
/// <summary>
/// Интерфейс ключа
/// </summary>
public interface IEncryptKey
{
/// <summary>
/// Ключ
/// </summary>
public byte[] Key { get; set; }
}

View File

@ -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>

View File

@ -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>