Descifra un valor encriptado con la función CifrarValor


Referencias usadas:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;


Descifra un valor encriptado con la función anterior, 
debemos pasarles las mismas claves.


*********************************************************************************


public string DescifrarValor(string sValor)
{
  string sResultado = string.Empty;
  TripleDESCryptoServiceProvider oTripleDESC = 
    new TripleDESCryptoServiceProvider();
  try
  {
    byte[] bIN = Convert.FromBase64String(sValor);
    using (MemoryStream oOutStream = new MemoryStream())
    {
      CryptoStream oCryptoStream =
        new CryptoStream(oOutStream, 
         oTripleDESC.CreateDecryptor(bClave, bVector), 
          CryptoStreamMode.Write);

      oCryptoStream.Write(bIN, 0, bIN.Length);
      oCryptoStream.FlushFinalBlock();

      sResultado = Encoding.UTF8.GetString(oOutStream.ToArray());
    }
  }
  catch (Exception ex)
  {
    throw new System.Exception(ex.Message, ex.InnerException);
  }

  return sResultado;
}

Encriptar un texto

Referencias usadas:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;



Debemos declarar y no perder estas dos variables:

byte[] bClave = 
  Encoding.UTF8.GetBytes("b%`^x&amH~a!x@Y#c$p;J*M-e^L_");
byte[] bVector = 
  Encoding.UTF8.GetBytes("p$ctuW^%");

La función encripta una cadena de texto:

***********************************************************************
public string CifrarValor(string sValor)
{
  string sResultado = string.Empty;
  TripleDESCryptoServiceProvider oTripleDESC = 
     new TripleDESCryptoServiceProvider();
           
  try
  {
    byte[] bIN = Encoding.UTF8.GetBytes(sValor);
    using (MemoryStream oOutStream = new MemoryStream())
    {
      CryptoStream oCryptoStream =
        new CryptoStream(oOutStream, 
             oTripleDESC.CreateEncryptor(bClave, bVector),
                 CryptoStreamMode.Write);
                   
      oCryptoStream.Write(bIN, 0, bIN.Length);
      oCryptoStream.FlushFinalBlock();

      sResultado = Convert.ToBase64String(oOutStream.ToArray());
    }
  }
  catch (Exception ex)
  {
    throw new System.Exception(ex.Message, ex.InnerException);
   }

  return sResultado;
}

Leer una clave en el registro


Referencias usadas:

using System;
using Microsoft.Win32;


Devuelve el valor de una clave determinada del registro de windows.


*******************************************************************************


public string DevuelveClaveDelRegistro(string sRuta, string sClave)
{
  string sResultado = string.Empty;
  RegistryKey obReg = 
        Registry.LocalMachine.OpenSubKey("SOFTWARE")
                      .OpenSubKey(sRuta, true);
             
  if (obReg != null)
  {
    if (obReg.GetValue(sClave) != null)
      sResultado = obReg.GetValue(sClave).ToString();
  }

  return sResultado;
}

Crear una clave en el registro


Referencias usadas:

using System;
using Microsoft.Win32;


Crear una clave con su valor en la carpeta de destino
dentro del registro de windows.

Deberemos incluir:
using Microsoft.Win32;

**********************************************************************************

public bool CrearClave(string sRuta, string sClave, string sValor)
{
  bool Result = false;
  RegistryKey obReg =
    Registry.LocalMachine.OpenSubKey("SOFTWARE")
                                               .OpenSubKey(sRuta, true);
  if (obReg == null)
    obReg =
      Registry.LocalMachine.OpenSubKey("SOFTWARE", true)
                                                     .CreateSubKey(sRuta);

  try
  {
    obReg.SetValue(sClave, sValor);
    obReg.Close();

    Result = true;
  }
  catch (Exception ex)
  {
    throw new System.Exception(ex.Message, ex.InnerException);
  }
  return Result;
}

Validar un string


Referencias usadas:

using System.Text.RegularExpressions;


Esta función recibe dos strings como parámetros, el primero 
es el string a validar, el segundo es la cadena origen.


**************************************************************************


public bool ValidaString(string sTexto, string sPattern)
{
   Regex reg = new Regex(sPattern);
   bool Respuesta = false;
          
   try
   {
      if (reg.IsMatch(sTexto))
         Respuesta = true;
   }
   catch
   {
      Respuesta =  false;
   }
   finally
   {
      reg = null;
   }

   return Respuesta;
}

***************************************************************************


Para validar una dirección de correo electrónico (mail):


*************************************************************************

public bool ComprobarMail(string sTexto)

   string sModelo = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
   return ValidaString(sTexto, sModelo);
}

Comprobar controles

Referencias usadas:

using System.Windows.Forms;

Recorre la colección de controles del objeto contenedor
para comprobar si la propiedad Text es vacia o es nula.
true: si todos tienen la propiedad Text rellena; false: en caso contrario

************************************************************************

public bool ComprobarDatosControles(Control contenedor)
{
   foreach (Control control in contenedor.Controls)
   {
      if (control.GetType() == typeof(TextBox) ||
            control.GetType() == typeof(ComboBox))
      {
         if (string.IsNullOrEmpty(control.Text))
            return false;
       }
    }
    return true;
}

FolderBrowserDialog


Referencias usadas:


using System.Windows.Forms;

Devuelve la carpeta de destino donde guardar un fichero determinado,
seleccionado en un objeto FolderBrowserDialog recibido como parámetro.

**********************************************************************************

public string SeleccionaCarpeta(FolderBrowserDialog fbdCarpeta)
{
   string Result = string.Empty;
   DialogResult Resultado = fbdCarpeta.ShowDialog();
   if (Resultado == DialogResult.OK)
      Result = fbdCarpeta.SelectedPath;
   return Result;
}

OpenFileDialog


Referencias usadas:


using System.Windows.Forms;

Devuelve la ruta completa con el nombre del fichero, seleccionado en un objeto OpenFileDialog recibido como parámetro

***************************************************************************

public string SeleccionaArchivo(OpenFileDialog ofdOrigen)
{
   string Result = string.Empty;
   DialogResult Resultado = ofdOrigen.ShowDialog();
   if (Resultado == DialogResult.OK)
      Result = ofdOrigen.FileName;
   return Result;
}