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; + +/// +/// Ключ шифровки / расшифровки +/// +public class EncryptKey: IEncryptKey +{ + /// + /// Конструктор по умолчанию + /// + /// Ключ в формате массива + public EncryptKey (byte[] key) => Key = key; + + /// + /// Конструктор, принимающий строку и преобразующий в нужный формат + /// + /// Ключ в формате + public EncryptKey (string key) => Key = Encoding.ASCII.GetBytes(key); + + /// + /// Ключ + /// + public byte[] Key { get; set; } + + /// + /// Получение ключа по умолчанию + /// + /// + public static EncryptKey GetDefault () => new(Enumerable.Range(0, 32).Select(static x => (byte)x).ToArray()); + + /// + /// Получение класса ключа + /// + /// Ключ в формате массива + /// + public static EncryptKey GetKey (byte[]? key) => key != null ? new(key) : GetDefault(); + + /// + /// Получение класса ключа + /// + /// Ключ в формате массива + /// + 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; + +/// +/// Класс для шифровки строк +/// +public static class StringEncrypt +{ + /// + /// Метод для шифрования строки с помощью ключа + /// + /// Строка, которая должна быть зашифрована + /// Ключ шифрования (в случае null) + /// Этот статический метод возвращает зашифрованную строку с помощью ключа шифрования + 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()); + } + + /// + /// Метод для дешифрования строки с помощью ключа + /// + /// Строка, которая должна быть дешифрована + /// Ключ шифрования (в случае null) + /// Этот статический метод возвращает дешифрованную строку с помощью ключа шифрования + 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; + +/// +/// Интерфейс ключа +/// +public interface IEncryptKey +{ + /// + /// Ключ + /// + 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 @@ net7.0 - 2023.115.0 + 2023.120.0 Alexander Babaev ANB Software Components Pack Library of some useful functions in C# language. @@ -15,8 +15,8 @@ True https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack - 2023.115.0 - 2023.115.0 + + ANBSoftware.ComponentsPack MIT 6.0 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 @@ True True True + True True True True