123 lines
3.8 KiB
C#
123 lines
3.8 KiB
C#
using DAL;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace TLL
|
|
{
|
|
public static class DatabaseHelper
|
|
{
|
|
// ===== SALVAR =====
|
|
public static void Salvar(string caminhoArquivo, string senha)
|
|
{
|
|
var config = new ConfigBanco
|
|
{
|
|
Host = DadosDaConexao.Host,
|
|
Port = DadosDaConexao.Port,
|
|
Banco = DadosDaConexao.Banco,
|
|
Usuario = DadosDaConexao.Usuario,
|
|
Senha = DadosDaConexao.Senha,
|
|
ConnectTimeout = DadosDaConexao.ConnectTimeout,
|
|
Encrypt = DadosDaConexao.Encrypt,
|
|
TrustServerCertificate = DadosDaConexao.TrustServerCertificate
|
|
};
|
|
|
|
string json = JsonSerializer.Serialize(config);
|
|
byte[] dados = Criptografar(json, senha);
|
|
|
|
File.WriteAllBytes(caminhoArquivo, dados);
|
|
}
|
|
|
|
// ===== CARREGAR =====
|
|
public static void Carregar(string caminhoArquivo, string senha)
|
|
{
|
|
if (!File.Exists(caminhoArquivo))
|
|
return;
|
|
|
|
byte[] dados = File.ReadAllBytes(caminhoArquivo);
|
|
string json = Descriptografar(dados, senha);
|
|
|
|
var config = JsonSerializer.Deserialize<ConfigBanco>(json);
|
|
|
|
if (config == null)
|
|
return;
|
|
|
|
// Preenche a classe static
|
|
DadosDaConexao.Host = config.Host;
|
|
DadosDaConexao.Port = config.Port;
|
|
DadosDaConexao.Banco = config.Banco;
|
|
DadosDaConexao.Usuario = config.Usuario;
|
|
DadosDaConexao.Senha = config.Senha;
|
|
DadosDaConexao.ConnectTimeout = config.ConnectTimeout;
|
|
DadosDaConexao.Encrypt = config.Encrypt;
|
|
DadosDaConexao.TrustServerCertificate = config.TrustServerCertificate;
|
|
}
|
|
|
|
// ===== CRIPTO =====
|
|
private static byte[] Criptografar(string texto, string senha)
|
|
{
|
|
using var aes = Aes.Create();
|
|
|
|
aes.Key = GerarChave(senha);
|
|
aes.GenerateIV();
|
|
|
|
using var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
|
|
using var ms = new MemoryStream();
|
|
|
|
// salva IV no início
|
|
ms.Write(aes.IV, 0, aes.IV.Length);
|
|
|
|
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
|
|
using (var sw = new StreamWriter(cs))
|
|
{
|
|
sw.Write(texto);
|
|
}
|
|
|
|
return ms.ToArray();
|
|
}
|
|
|
|
private static string Descriptografar(byte[] dados, string senha)
|
|
{
|
|
using var aes = Aes.Create();
|
|
|
|
aes.Key = GerarChave(senha);
|
|
|
|
byte[] iv = new byte[16];
|
|
Array.Copy(dados, iv, iv.Length);
|
|
aes.IV = iv;
|
|
|
|
using var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
|
|
using var ms = new MemoryStream(dados, iv.Length, dados.Length - iv.Length);
|
|
using var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
|
|
using var sr = new StreamReader(cs);
|
|
|
|
return sr.ReadToEnd();
|
|
}
|
|
|
|
private static byte[] GerarChave(string senha)
|
|
{
|
|
using var sha = SHA256.Create();
|
|
return sha.ComputeHash(Encoding.UTF8.GetBytes(senha));
|
|
}
|
|
|
|
|
|
// ===== MODEL INTERNO =====
|
|
private class ConfigBanco
|
|
{
|
|
public string Host { get; set; }
|
|
public int Port { get; set; }
|
|
public string Banco { get; set; }
|
|
public string Usuario { get; set; }
|
|
public string Senha { get; set; }
|
|
public int ConnectTimeout { get; set; }
|
|
public bool Encrypt { get; set; }
|
|
public bool TrustServerCertificate { get; set; }
|
|
}
|
|
}
|
|
|
|
}
|