20230120
This commit is contained in:
parent
d3171a1164
commit
c89a6b2a71
48
anbs_cp/Classes/Encrypt/EncriptKey.cs
Normal file
48
anbs_cp/Classes/Encrypt/EncriptKey.cs
Normal 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();
|
||||
}
|
48
anbs_cp/Classes/Encrypt/StringEncrypt.cs
Normal file
48
anbs_cp/Classes/Encrypt/StringEncrypt.cs
Normal 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());
|
||||
}
|
||||
}
|
12
anbs_cp/Interfaces/IEncryptKey.cs
Normal file
12
anbs_cp/Interfaces/IEncryptKey.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace anbs_cp.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Интерфейс ключа
|
||||
/// </summary>
|
||||
public interface IEncryptKey
|
||||
{
|
||||
/// <summary>
|
||||
/// Ключ
|
||||
/// </summary>
|
||||
public byte[] Key { get; set; }
|
||||
}
|
@ -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>
|
||||
|
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user