194 lines
5.7 KiB
C#
194 lines
5.7 KiB
C#
using Nini.Config;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace Security
|
|
{
|
|
public class Encripter
|
|
{
|
|
public static int _SEMILLA { get; private set; } = -1;
|
|
public static string? HashKey { get; private set; }
|
|
private static string? PwdKey { get; set; }
|
|
|
|
// private static readonly HttpClient client = new();
|
|
public Encripter()
|
|
{
|
|
string? mode = Environment.GetEnvironmentVariable("Mode");
|
|
|
|
if (mode == "Prod")
|
|
{
|
|
ReadCredentials();
|
|
}
|
|
else
|
|
{
|
|
ReadCredentials();
|
|
/*string DevKey = Environment.GetEnvironmentVariable("MySecretKey")!;
|
|
HashKey = DevKey;
|
|
PwdKey = DevKey;
|
|
_SEMILLA = 1262;*/
|
|
}
|
|
}
|
|
|
|
public bool IsValid
|
|
{
|
|
get
|
|
{
|
|
return HashKey != null && PwdKey != null && _SEMILLA != -1;
|
|
}
|
|
}
|
|
|
|
public int getSemilla()
|
|
{
|
|
return _SEMILLA;
|
|
}
|
|
|
|
private void ReadCredentials()
|
|
{
|
|
try
|
|
{
|
|
|
|
//var folder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
|
|
|
var filePath = Path.Combine("Z:\\Users\\Administrator", ".hims", "info");
|
|
//var filePath = Path.Combine("C:\\Users\\Admin\\Desktop\\sqsas\\usuarios", "info");
|
|
|
|
//Validacion manual antes de leer
|
|
if (!File.Exists(filePath)) //Exist = false
|
|
{
|
|
//Devolver error
|
|
HashKey = null;
|
|
PwdKey = null;
|
|
_SEMILLA = -1;
|
|
return;
|
|
}
|
|
|
|
IConfigSource source = new IniConfigSource(filePath);
|
|
var security = source.Configs["hash"];
|
|
var defecto = source.Configs["default"];
|
|
|
|
HashKey = security.Get("tkn_key");
|
|
PwdKey = security.Get("hash_key");
|
|
_SEMILLA = Int32.Parse(defecto.Get("seed"));
|
|
|
|
Console.WriteLine();
|
|
|
|
}
|
|
catch (IOException ioEx)
|
|
{
|
|
Console.WriteLine(ioEx.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
}
|
|
|
|
public string EncryptPwd(string text)
|
|
{
|
|
return Encrypt(text, PwdKey);
|
|
}
|
|
public string DecryptPwd(string text)
|
|
{
|
|
return Decrypt(text, PwdKey);
|
|
}
|
|
|
|
public string EncryptHashTkn(string text)
|
|
{
|
|
return Encrypt(text, HashKey);
|
|
}
|
|
public string DecryptHashTkn(string text)
|
|
{
|
|
return Decrypt(text, HashKey);
|
|
}
|
|
|
|
private static string Encrypt(string text, string hash)
|
|
{
|
|
try
|
|
{
|
|
byte[] bytesHash = UTF8Encoding.UTF8.GetBytes(hash);
|
|
byte[] bytes = UTF8Encoding.UTF8.GetBytes(text);
|
|
|
|
MD5 md5 = MD5.Create();
|
|
TripleDES trpl = TripleDES.Create();
|
|
|
|
trpl.Key = md5.ComputeHash(bytesHash);
|
|
trpl.Mode = CipherMode.ECB;
|
|
|
|
ICryptoTransform transformer = trpl.CreateEncryptor();
|
|
byte[] res = transformer.TransformFinalBlock(bytes, 0, bytes.Length);
|
|
|
|
|
|
return Convert.ToBase64String(res);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
|
|
private static string Decrypt(string encText, string hash)
|
|
{
|
|
try
|
|
{
|
|
|
|
//string hash = Environment.GetEnvironmentVariable("MySecretKey")!;
|
|
|
|
byte[] bytesHash = UTF8Encoding.UTF8.GetBytes(hash);
|
|
byte[] bytes = Convert.FromBase64String(encText);
|
|
|
|
MD5 md5 = MD5.Create();
|
|
TripleDES trpl = TripleDES.Create();
|
|
|
|
trpl.Key = md5.ComputeHash(bytesHash);
|
|
trpl.Mode = CipherMode.ECB;
|
|
|
|
ICryptoTransform transformer = trpl.CreateDecryptor();
|
|
byte[] res = transformer.TransformFinalBlock(bytes, 0, bytes.Length);
|
|
|
|
return UTF8Encoding.UTF8.GetString(res);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
|
|
public static string Cypher(string value)
|
|
{
|
|
string encryptedText = "";
|
|
for (int i = 0; i < value.Length; i++)
|
|
{
|
|
int txtUser = (int)value[i];
|
|
int encryption = txtUser + _SEMILLA;
|
|
encryptedText += Char.ConvertFromUtf32(encryption);
|
|
}
|
|
return Base64Encode(encryptedText);
|
|
}
|
|
|
|
public static string Descypher(string value)
|
|
{
|
|
string b64Decrypted = Base64Decode(value);
|
|
string decryptedText = "";
|
|
for (int i = 0; i < b64Decrypted.Length; i++)
|
|
{
|
|
int encrypted = (int)b64Decrypted[i];
|
|
int decryption = encrypted - _SEMILLA;
|
|
decryptedText += Char.ConvertFromUtf32(decryption);
|
|
}
|
|
return decryptedText;
|
|
}
|
|
|
|
|
|
public static string Base64Decode(string base64EncodedData)
|
|
{
|
|
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
|
|
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
|
|
}
|
|
public static string Base64Encode(string plainText)
|
|
{
|
|
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
|
|
return System.Convert.ToBase64String(plainTextBytes);
|
|
}
|
|
}
|
|
} |