This commit is contained in:
2024-01-23 15:41:59 +01:00
Unverified
parent 5d5a69ccf7
commit 3b2b4c9b7e
76 changed files with 4100 additions and 888 deletions

View File

@@ -0,0 +1,32 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SecureBank.API.Encryption
{
public class EncryptionConfiguration
{
#region PROPERTIES
public byte[] Key { get; private set; }
public byte[] IV { get; private set; }
#endregion
#region CONSTRUCTORS
public EncryptionConfiguration(IConfiguration configuration)
{
Key = Encoding.UTF8.GetBytes(configuration.GetSection("Encryption")["Key"]);
IV = Encoding.UTF8.GetBytes(configuration.GetSection("Encryption")["IV"]);
}
#endregion
}
}

View File

@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace SecureBank.API.Encryption
{
public class EncryptionHelper
{
#region SERVICES
private EncryptionConfiguration _configuration;
#endregion
#region FIELDS
private Aes _aes;
#endregion
#region CONSTRUCTORS
public EncryptionHelper(EncryptionConfiguration configuration)
{
_configuration = configuration;
_aes = Aes.Create();
_aes.Key = _configuration.Key;
_aes.IV = _configuration.IV;
}
#endregion
#region PUBLIC METHODS
public byte[] Encrypt(string data)
{
ICryptoTransform encryptor = _aes.CreateEncryptor(_aes.Key, _aes.IV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(data);
}
return memoryStream.ToArray();
}
}
public string Decrypt(byte[] data)
{
ICryptoTransform decryptor = _aes.CreateDecryptor(_configuration.Key, _configuration.IV);
using (MemoryStream memoryStream = new MemoryStream(data))
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] outputBytes = new byte[data.Length];
int decryptedByteCount = cryptoStream.Read(outputBytes, 0, outputBytes.Length);
return Encoding.UTF8.GetString(outputBytes.Take(decryptedByteCount).ToArray());
}
}
#endregion
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
</ItemGroup>
</Project>