vault unlocking WIP

This commit is contained in:
TheXamlGuy
2024-05-03 19:33:32 +01:00
parent 7582f63a68
commit abcff30da6
9 changed files with 92 additions and 43 deletions
+34 -12
View File
@@ -7,22 +7,44 @@ public class AesDecryptor :
{
private const int IvSize = 16;
public byte[] Decrypt(byte[] cipher, byte[] key)
public bool TryDecrypt(byte[] cipher,
byte[] key,
out byte[]? decryptedData)
{
Span<byte> iv = cipher.AsSpan(0, IvSize);
ReadOnlySpan<byte> encryptedContent = cipher.AsSpan(IvSize);
decryptedData = null;
using Aes aes = Aes.Create();
aes.Key = key;
aes.IV = iv.ToArray();
if (cipher is null || key is null || cipher.Length < IvSize)
{
return false;
}
using MemoryStream memoryStream = new(encryptedContent.ToArray());
using ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using CryptoStream cryptoStream = new(memoryStream, decryptor, CryptoStreamMode.Read);
try
{
Span<byte> iv = cipher.AsSpan(0, IvSize);
ReadOnlySpan<byte> encryptedContent = cipher.AsSpan(IvSize);
using MemoryStream resultStream = new();
cryptoStream.CopyTo(resultStream);
using Aes aes = Aes.Create();
aes.Key = key;
aes.IV = iv.ToArray();
return resultStream.ToArray();
using MemoryStream memoryStream = new(encryptedContent.ToArray());
using ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using CryptoStream cryptoStream = new(memoryStream, decryptor, CryptoStreamMode.Read);
using MemoryStream resultStream = new();
cryptoStream.CopyTo(resultStream);
decryptedData = resultStream.ToArray();
return true;
}
catch (CryptographicException)
{
return false;
}
catch (Exception)
{
return false;
}
}
}