20230122
This commit is contained in:
parent
5b82e8034d
commit
480e884733
@ -1,34 +0,0 @@
|
|||||||
using System.Text;
|
|
||||||
|
|
||||||
using anbs_cp.Interfaces;
|
|
||||||
|
|
||||||
namespace anbs_cp.Classes.Encrypt;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ключ шифровки / расшифровки
|
|
||||||
/// </summary>
|
|
||||||
public sealed class EncryptKey: IEncryptKey
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Конструктор по умолчанию
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">Ключ в формате массива <see cref="byte"/></param>
|
|
||||||
public EncryptKey (byte[] key) => Key = key;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Конструктор, принимающий строку и преобразующий в нужный формат
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="utf8Key">Ключ в формате <see cref="string"/> и в кодировке utf8</param>
|
|
||||||
public EncryptKey (string utf8Key) => Key = Encoding.UTF8.GetBytes(utf8Key);
|
|
||||||
|
|
||||||
/// <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());
|
|
||||||
}
|
|
@ -1,5 +1,6 @@
|
|||||||
using System.Security.Cryptography;
|
using System.Text;
|
||||||
using System.Text;
|
|
||||||
|
using gfoidl.Base64;
|
||||||
|
|
||||||
namespace anbs_cp.Classes.Encrypt;
|
namespace anbs_cp.Classes.Encrypt;
|
||||||
|
|
||||||
@ -9,40 +10,24 @@ namespace anbs_cp.Classes.Encrypt;
|
|||||||
public static class StringEncrypt
|
public static class StringEncrypt
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Метод для шифрования строки <paramref name="text"/> с помощью ключа <paramref name="key"/>
|
/// Метод для шифрования строки <paramref name="text"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">Строка, которая должна быть зашифрована</param>
|
/// <param name="text">Строка, которая должна быть зашифрована</param>
|
||||||
/// <param name="key">Ключ шифрования (в случае <value>null</value>)</param>
|
|
||||||
/// <returns>Этот статический метод возвращает зашифрованную строку <paramref name="text"/> с помощью ключа шифрования <paramref name="key"/></returns>
|
/// <returns>Этот статический метод возвращает зашифрованную строку <paramref name="text"/> с помощью ключа шифрования <paramref name="key"/></returns>
|
||||||
public static string Encrypt (string text, EncryptKey? key = null)
|
public static string Encrypt(string text)
|
||||||
{
|
{
|
||||||
using Aes aes = Aes.Create();
|
byte[] byteText = Encoding.UTF8.GetBytes(text);
|
||||||
aes.Key = key?.Key ?? EncryptKey.GetDefault().Key;
|
return Base64.Url.Encode(byteText);
|
||||||
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>
|
/// <summary>
|
||||||
/// Метод для дешифрования строки <paramref name="text"/> с помощью ключа <paramref name="key"/>
|
/// Метод для дешифрования строки <paramref name="text"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">Строка, которая должна быть дешифрована</param>
|
/// <param name="text">Строка, которая должна быть дешифрована</param>
|
||||||
/// <param name="key">Ключ шифрования (в случае <value>null</value>)</param>
|
|
||||||
/// <returns>Этот статический метод возвращает дешифрованную строку <paramref name="text"/> с помощью ключа шифрования <paramref name="key"/></returns>
|
/// <returns>Этот статический метод возвращает дешифрованную строку <paramref name="text"/> с помощью ключа шифрования <paramref name="key"/></returns>
|
||||||
public static string Decrypt (string text, EncryptKey? key = null)
|
public static string Decrypt (string text)
|
||||||
{
|
{
|
||||||
using MemoryStream ms = new(Convert.FromBase64String(text));
|
string guidBase64Url = text.Replace('+', '-').Replace('/', '_').TrimEnd('=');
|
||||||
byte[] iv = new byte[16];
|
return Encoding.UTF8.GetString(Base64.Url.Decode(guidBase64Url));
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
<Version>2023.121.1</Version>
|
<Version>2023.122.1</Version>
|
||||||
<Authors>Alexander Babaev</Authors>
|
<Authors>Alexander Babaev</Authors>
|
||||||
<Product>ANB Software Components Pack</Product>
|
<Product>ANB Software Components Pack</Product>
|
||||||
<Description>Library of some useful functions in C# language.</Description>
|
<Description>Library of some useful functions in C# language.</Description>
|
||||||
@ -37,6 +37,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="gfoidl.Base64" Version="2.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
|
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop" Version="17.4.33103.184" />
|
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop" Version="17.4.33103.184" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user