Encriptación y Desencriptación de Campos en SQL Server con Funciones Definidas por el Usuario y Cookies en ASP.NET
Encriptación de Campos en SQL Server
--ENCRIPTAR CAMPOS
CREATE FUNCTION Encriptar(@Cadena Varchar(300))
Returns Varchar(300)
As
Begin
Declare @Resultado Varchar(300)
Declare @Contador Int
Set @Resultado=''
Set @Contador=Len(@Cadena) --CADENA='UTEC' CONTADOR=4
while(@Contador<>0)
Begin
Set @Resultado=@Resultado+Char(Ascii(SubString(@Cadena,@Contador,1))*2+@Contador) --EXTRAER EL CARÁCTER GRÁFICO DE 138
Set @Contador=@Contador- 1 -- SELECT ASCII('C')=67*2=134+4=138 --EXTRAE EL CÓDIGO ASCII DE LA LETRA "C"
End --=138 SELECT CHAR(138)
Return @Resultado
End
--update USUARIOS Set Password=dbo.Encriptar('UTEC2011') where Id_Usuarios=1
Desencriptación de Campos en SQL Server
--DESENCRIPTAR
CREATE FUNCTION Desencriptar(@Cadena Varchar(300))
Returns Varchar(300)
As
Begin
Declare @Resultado Varchar(300), @Contador Int, @Posicion Int, @Longitud Int
Set @Resultado=''
Set @Contador=1
Set @Posicion=Len(@Cadena)
Set @Longitud=Len(@Cadena)
while(@Contador <=@Longitud)
Begin
set @Resultado=@Resultado+CHAR((Ascii(substring(@Cadena,@Posicion,1))-@Contador)/2)
set @Contador=@Contador+1
set @Posicion=@Posicion-1
end
return @Resultado
end
--select dbo.Desencriptar(Password) from USUARIOS WHERE Id_USUARIOS=1
Gestión de Cookies con ASP.NET (C#)
Página principal: _Default
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
// Hashtable: no son las cookies; almacena una llave (libro) y su información (ISBN)
System.Collections.Hashtable Libros = new System.Collections.Hashtable();
protected void Page_Init(object sender, EventArgs e)
{
// La llave será el libro y el valor será el ISBN. El código internacional de los libros se conoce como ISBN
Libros.Add("Visual Basic", "0-13-186601-0");
Libros.Add("Visual C#", "0-13-186601-1");
Libros.Add("Java", "0-13-186601-2");
Libros.Add("C++", "0-13-186601-3");
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) // TRUE = la página ha enviado datos o se ha recargado (Refresh)
{
Label2.Visible = true;
HyperLink1.Visible = true;
HyperLink2.Visible = true;
Label1.Visible = false;
RadioButtonList1.Visible = false;
Button1.Visible = false;
if (RadioButtonList1.SelectedItem != null) // si el usuario seleccionó un libro
{
Label2.Text += "Usted seleccionó: " + RadioButtonList1.SelectedItem.Text;
}
else
{
Label2.Text += "No seleccionó un lenguaje";
}
// La variable IsPostBack cambia
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedItem != null) // validación
{
// Obtiene el nombre del libro a buscar (object key) — nombre de la cookie
string Lenguaje = RadioButtonList1.SelectedItem.ToString();
// Obtiene el número ISBN almacenado en la Hashtable (object value) — valor de la cookie
string ISBN = Libros[Lenguaje].ToString();
// Define la cookie en memoria con sus valores respectivos
HttpCookie cookie = new HttpCookie(Lenguaje, ISBN);
// Guarda la cookie en la computadora del cliente (físico). Las cookies son exclusivas para cada sitio web
Response.Cookies.Add(cookie);
}
}
}
Página de recomendaciones: Recomendaciones
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Recomendaciones : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
// Recupera las cookies del equipo del cliente
HttpCookieCollection cookies = Request.Cookies;
if (cookies.Count != 0) // si hay cookies
{
for (int i = 0; i < cookies.Count; i++)
{
ListBox1.Items.Add(cookies[i].Name + " como programar. ISBN: " + cookies[i].Value);
}
}
else
{
Label1.Text = "No hay recomendaciones...";
ListBox1.Items.Clear();
ListBox1.Visible = false;
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}