Files
YandexMusic/YandexMusic.API/Common/Encryptor.cs
2026-04-10 12:12:33 +03:00

75 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Security.Cryptography;
using System.Text;
namespace YandexMusic.API.Common
{
/// <summary>
/// Класс для шифровки
/// </summary>
public class Encryptor
{
#region Поля
private readonly string IV = "encryption";
private readonly byte[] IVHash;
private readonly byte[] keyHash;
private readonly MD5 md5;
private readonly Aes aesAlg;
#endregion Поля
#region Вспомогательные функции
private byte[] GetHash(string value)
{
return md5.ComputeHash(Encoding.UTF8.GetBytes(value));
}
#endregion Вспомогательные функции
public Encryptor(string key)
{
md5 = MD5.Create();
aesAlg = Aes.Create();
aesAlg.BlockSize = 128;
aesAlg.Padding = PaddingMode.PKCS7;
keyHash = GetHash(key);
IVHash = GetHash(IV);
}
public byte[] Encrypt(byte[] data)
{
using MemoryStream ms = new();
using CryptoStream csEncrypt = new(ms, aesAlg.CreateEncryptor(keyHash, IVHash), CryptoStreamMode.Write);
csEncrypt.Write(data, 0, data.Length);
if (!csEncrypt.HasFlushedFinalBlock)
csEncrypt.FlushFinalBlock();
return ms.ToArray();
}
public byte[] Decrypt(byte[] data)
{
using MemoryStream ms = new();
using CryptoStream csDecrypt = new(ms, aesAlg.CreateDecryptor(keyHash, IVHash), CryptoStreamMode.Write);
csDecrypt.Write(data, 0, data.Length);
if (!csDecrypt.HasFlushedFinalBlock)
csDecrypt.FlushFinalBlock();
return ms.ToArray();
}
}
}