Tutoriales gratuitos para el aprendizaje de la programacion informatica! Recuerda que si lo puedes imaginar... lo puedes programar!

Inicio de sesion con clave encriptada SQL VB.NET

Crear un inicio de sesion con clave de usuario encriptada en una base de datos Sql desde Vb.net

En el form1:
  • ComboBox1
  • TextBox1
  • Button1
  • Label1
  • Label2




Diseño del formulario
Codigo

Imports System.ComponentModel
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Text
Imports System.Security.Cryptography

Public Class Form1
    Dim SQL As String
    Dim SERVIDOR As String = "NOMBRE DEL SERVIDOR"
    Dim BD As String = "BASE DE DATOS"
    Dim USUARIO As String = "USUSRIO"
    Dim CONTRASEÑA As String = "CLAVE"
    Dim VALIDAR_USUARIO As String
    Public CONEXION As New SqlConnection("Data Source=" & SERVIDOR & ";" &
                                     "Initial Catalog=" & BD & ";" &
                                     "User Id=" & USUARIO & ";Password=" & CONTRASEÑA & "")
    Sub FormatoTexto()
        TextBox1.BackColor = Color.Azure
        ComboBox1.BackColor = Color.Azure
        ComboBox1.ForeColor = Color.Teal
        TextBox1.ForeColor = Color.Teal
    End Sub
    Public Function Desencriptar(ByVal Input As String) As String
        Dim IV() As Byte = ASCIIEncoding.ASCII.GetBytes("qualityi")
        Dim EncryptionKey() As Byte = Convert.FromBase64String("rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5") 'No se puede alterar la cantidad de caracteres pero si la clave
        Dim buffer() As Byte = Convert.FromBase64String(Input)
        Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
        des.Key = EncryptionKey
        des.IV = IV
        Return Encoding.UTF8.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length()))
    End Function
    Public Function Encriptar(ByVal Input As String) As String
        Dim IV() As Byte = ASCIIEncoding.ASCII.GetBytes("qualityi")
        Dim EncryptionKey() As Byte = Convert.FromBase64String("rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5") 'No se puede alterar la cantidad de caracteres pero si la clave
        Dim buffer() As Byte = Encoding.UTF8.GetBytes(Input)
        Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
        des.Key = EncryptionKey
        des.IV = IV
        Return Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length()))
    End Function
    Private Sub getData(ByVal dataCollection As AutoCompleteStringCollection)
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter()
        Dim DataSet As New DataSet()
        SQL = "SELECT DISTINCT [USUARIO] FROM [USUARIO]"
        Try
            CONEXION.Open()
            command = New SqlCommand(SQL, CONEXION)
            adapter.SelectCommand = command
            adapter.Fill(DataSet)
            adapter.Dispose()
            command.Dispose()
            CONEXION.Close()
            For Each row As DataRow In DataSet.Tables(0).Rows
                dataCollection.Add(row(0).ToString())
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Sub Verificacion()
        Dim verificado As String = ComboBox1.Text & TextBox1.Text
        Dim Comando As New SqlCommand("SELECT * FROM USUARIO WHERE USUARIO =  '" & ComboBox1.Text & "' AND CLAVE =  '" & Encriptar(TextBox1.Text) & "'", CONEXION)
        ' crear DataReader
        Dim DataReader As SqlDataReader
        CONEXION.Open()
        DataReader = Comando.ExecuteReader() ' obtener DataReader
        ' recorrer filas
        While DataReader.Read()
            VALIDAR_USUARIO = DataReader("USUARIO") & "" & Desencriptar(DataReader("CLAVE"))
        End While
        DataReader.Close()
        CONEXION.Close()
        If verificado = VALIDAR_USUARIO Then
            Form2.Show()
            Me.Hide()
            'limpiar las cajas de texto
            TextBox1.Text = ""
            ComboBox1.Text = ""
        Else
            TextBox1.BackColor = Color.Beige
            ComboBox1.BackColor = Color.Beige
            ComboBox1.ForeColor = Color.DarkSalmon
            TextBox1.ForeColor = Color.DarkSalmon
            MsgBox("Contraseña y/o usuario incorrecta", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Validación")
        End If
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Call FormatoTexto()
    End Sub
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Call FormatoTexto()
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call FormatoTexto()
        ComboBox1.Select()
        ComboBox1.AutoCompleteMode = AutoCompleteMode.Append
        ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        Dim combData As New AutoCompleteStringCollection()
        getData(combData)
        ComboBox1.AutoCompleteCustomSource = combData
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Call Verificacion()
    End Sub

End Class

No hay comentarios:

Publicar un comentario