diff --git a/anbs_cp/Classes/Encrypt/EncriptKey.cs b/anbs_cp/Classes/Encrypt/EncriptKey.cs deleted file mode 100644 index 134c69f..0000000 --- a/anbs_cp/Classes/Encrypt/EncriptKey.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Text; - -using anbs_cp.Interfaces; - -namespace anbs_cp.Classes.Encrypt; - -/// -/// Ключ шифровки / расшифровки -/// -public sealed class EncryptKey: IEncryptKey -{ - /// - /// Конструктор по умолчанию - /// - /// Ключ в формате массива - public EncryptKey (byte[] key) => Key = key; - - /// - /// Конструктор, принимающий строку и преобразующий в нужный формат - /// - /// Ключ в формате и в кодировке utf8 - public EncryptKey (string utf8Key) => Key = Encoding.UTF8.GetBytes(utf8Key); - - /// - /// Ключ - /// - public byte[] Key { get; set; } - - /// - /// Получение ключа по умолчанию - /// - /// - public static EncryptKey GetDefault () => new(Enumerable.Range(0, 32).Select(static x => (byte)x).ToArray()); -} \ No newline at end of file diff --git a/anbs_cp/Classes/Encrypt/StringEncrypt.cs b/anbs_cp/Classes/Encrypt/StringEncrypt.cs index f0e7444..87c46c2 100644 --- a/anbs_cp/Classes/Encrypt/StringEncrypt.cs +++ b/anbs_cp/Classes/Encrypt/StringEncrypt.cs @@ -1,5 +1,6 @@ -using System.Security.Cryptography; -using System.Text; +using System.Text; + +using gfoidl.Base64; namespace anbs_cp.Classes.Encrypt; @@ -9,40 +10,24 @@ namespace anbs_cp.Classes.Encrypt; public static class StringEncrypt { /// - /// Метод для шифрования строки с помощью ключа + /// Метод для шифрования строки /// /// Строка, которая должна быть зашифрована - /// Ключ шифрования (в случае null) /// Этот статический метод возвращает зашифрованную строку с помощью ключа шифрования - public static string Encrypt (string text, EncryptKey? key = null) + public static string Encrypt(string text) { - 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()); + byte[] byteText = Encoding.UTF8.GetBytes(text); + return Base64.Url.Encode(byteText); } /// - /// Метод для дешифрования строки с помощью ключа + /// Метод для дешифрования строки /// /// Строка, которая должна быть дешифрована - /// Ключ шифрования (в случае null) /// Этот статический метод возвращает дешифрованную строку с помощью ключа шифрования - public static string Decrypt (string text, EncryptKey? key = null) + public static string Decrypt (string text) { - 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()); + string guidBase64Url = text.Replace('+', '-').Replace('/', '_').TrimEnd('='); + return Encoding.UTF8.GetString(Base64.Url.Decode(guidBase64Url)); } } \ No newline at end of file diff --git a/anbs_cp/anbs_cp.csproj b/anbs_cp/anbs_cp.csproj index b115968..7c28528 100644 --- a/anbs_cp/anbs_cp.csproj +++ b/anbs_cp/anbs_cp.csproj @@ -1,8 +1,8 @@ - + net7.0 - 2023.121.1 + 2023.122.1 Alexander Babaev ANB Software Components Pack Library of some useful functions in C# language. @@ -37,6 +37,7 @@ +