72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
|
|
namespace Helper
|
|
{
|
|
using System.Configuration;
|
|
using System.Text;
|
|
|
|
/// <summary>
|
|
/// Clase para la encriptación.
|
|
/// </summary>
|
|
public sealed class CryptographyManager : CryptographyFoundation
|
|
{
|
|
/// <summary>
|
|
/// The global secret key.
|
|
/// </summary>
|
|
private readonly byte[] globalSecretKey;
|
|
|
|
/// <summary>
|
|
/// The iv.
|
|
/// </summary>
|
|
private readonly byte[] iv;
|
|
|
|
/// <summary>
|
|
/// The global initialization vector.
|
|
/// </summary>
|
|
private readonly string globalInitializationVector;
|
|
|
|
/// <summary>
|
|
/// Inicializa una nueva instancia de la clase <see cref="CryptographyManager"/>.
|
|
/// </summary>
|
|
public CryptographyManager()
|
|
{
|
|
this.globalSecretKey = Encoding.ASCII.GetBytes(ConfigurationManager.AppSettings["globalSecretKey"].ToString());// Encoding.ASCII.GetBytes("12EstaClave34es56dificil489ssswf");
|
|
|
|
this.globalInitializationVector = ConfigurationManager.AppSettings["globalInitializationVector"].ToString(); // Debe ser de 16 letras vector de inicialización ( IV ) para el algoritmo simétrico
|
|
this.iv = Encoding.ASCII.GetBytes(this.globalInitializationVector);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Encripta el texto enviado usando el algoritmo "Rijndael"
|
|
/// </summary>
|
|
/// <param name="textToEncrypt">
|
|
/// El texto sin codificar.
|
|
/// </param>
|
|
/// <returns>
|
|
/// El texto codificado. Tipo <see cref="string"/>.
|
|
/// </returns>
|
|
public string Encriptar(string textToEncrypt)
|
|
{
|
|
var encryptor = this.GetEncryptor(this.globalSecretKey, this.iv);
|
|
|
|
var result = this.Encriptador(textToEncrypt, encryptor);
|
|
|
|
return this.ReverseCase(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decodifica el texto el algoritmo "Rijndael"
|
|
/// </summary>
|
|
/// <param name="encryptedText">
|
|
/// El texto decodificado.
|
|
/// </param>
|
|
/// <returns>
|
|
/// Texto decodificado. Tipo <see cref="string"/>.
|
|
/// </returns>
|
|
public string Desencriptar(string encryptedText)
|
|
{
|
|
var decryptor = this.GetDecryptor(this.globalSecretKey, this.iv);
|
|
|
|
return this.Desencriptador(this.ReverseCase(encryptedText), decryptor);
|
|
}
|
|
}
|
|
}
|