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;
|
||||
|
||||
@ -9,40 +10,24 @@ namespace anbs_cp.Classes.Encrypt;
|
||||
public static class StringEncrypt
|
||||
{
|
||||
/// <summary>
|
||||
/// Метод для шифрования строки <paramref name="text"/> с помощью ключа <paramref name="key"/>
|
||||
/// Метод для шифрования строки <paramref name="text"/>
|
||||
/// </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)
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Метод для дешифрования строки <paramref name="text"/> с помощью ключа <paramref name="key"/>
|
||||
/// Метод для дешифрования строки <paramref name="text"/>
|
||||
/// </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)
|
||||
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));
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Version>2023.121.1</Version>
|
||||
<Version>2023.122.1</Version>
|
||||
<Authors>Alexander Babaev</Authors>
|
||||
<Product>ANB Software Components Pack</Product>
|
||||
<Description>Library of some useful functions in C# language.</Description>
|
||||
@ -37,6 +37,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="gfoidl.Base64" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop" Version="17.4.33103.184" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user